use before_all to replace the same content in each API
Signed-off-by: Wu Zhende wuzhende666@163.com --- src/scheduler/scheduler.cr | 18 ++++-------------- src/taskqueue/taskqueue.cr | 10 ++++------ 2 files changed, 8 insertions(+), 20 deletions(-)
diff --git a/src/scheduler/scheduler.cr b/src/scheduler/scheduler.cr index a946ea0..5f97567 100644 --- a/src/scheduler/scheduler.cr +++ b/src/scheduler/scheduler.cr @@ -32,6 +32,10 @@ module Scheduler
sched = Sched.new
+ before_all do |env| + env.response.headers["Connection"] = "close" + end + # for debug (maybe kemal debug|logger does better) def self.debug_message(env, response) puts %({"from": "#{env.request.remote_address}", "response": #{response.to_json}}) @@ -39,7 +43,6 @@ module Scheduler
# echo alive get "/" do |env| - env.response.headers["Connection"] = "close" debug_message(env, "Env= {\n#{`export`}}") "LKP Alive! The time is #{Time.local}, version = #{VERSION}" end @@ -50,7 +53,6 @@ module Scheduler # /boot.xxx/host/${hostname} # /boot.yyy/mac/${mac} get "/boot.:boot_type/:parameter/:value" do |env| - env.response.headers["Connection"] = "close" response = sched.find_job_boot(env)
job_id = response[/tmpfs/(.*)/job.cgz/, 1]? @@ -61,7 +63,6 @@ module Scheduler
# /~lkp/cgi-bin/gpxelinux.cgi?hostname=:hostname&mac=:mac&last_kernel=:last_kernel get "/~lkp/cgi-bin/gpxelinux.cgi" do |env| - env.response.headers["Connection"] = "close" response = sched.find_next_job_boot(env)
job_id = response[/tmpfs/(.*)/job.cgz/, 1]? @@ -74,7 +75,6 @@ module Scheduler # - echo job_id to caller # -- job_id = "0" ? means failed post "/submit_job" do |env| - env.response.headers["Connection"] = "close" job_messages = sched.submit_job(env)
job_messages.each do |job_message| @@ -86,7 +86,6 @@ module Scheduler
# file download server get "/job_initrd_tmpfs/:job_id/:job_package" do |env| - env.response.headers["Connection"] = "close" job_id = env.params.url["job_id"] job_package = env.params.url["job_package"] file_path = ::File.join [Kemal.config.public_folder, job_id, job_package] @@ -103,7 +102,6 @@ module Scheduler # # curl -X PUT "http://localhost:3000/set_host_mac?hostname=wfg&mac=00-01-02-03-04-05" put "/set_host_mac" do |env| - env.response.headers["Connection"] = "close" if (client_hostname = env.params.query["hostname"]?) && (client_mac = env.params.query["mac"]?) sched.set_host_mac(client_mac, client_hostname)
@@ -115,7 +113,6 @@ module Scheduler
# curl -X PUT "http://localhost:3000/set_host2queues?queues=vm-2p8g.aarch64&host=vm-2p8..." put "/set_host2queues" do |env| - env.response.headers["Connection"] = "close" if (client_queues = env.params.query["queues"]?) && (client_host = env.params.query["host"]?) sched.set_host2queues(client_host, client_queues)
@@ -127,7 +124,6 @@ module Scheduler
# curl -X PUT "http://localhost:3000/del_host_mac?mac=00-01-02-03-04-05" put "/del_host_mac" do |env| - env.response.headers["Connection"] = "close" if client_mac = env.params.query["mac"]? sched.del_host_mac(client_mac)
@@ -139,7 +135,6 @@ module Scheduler
# curl -X PUT "http://localhost:3000/del_host2queues?host=vm-2p8g.aarch64" put "/del_host2queues" do |env| - env.response.headers["Connection"] = "close" if client_host = env.params.query["host"]? sched.del_host2queues(client_host)
@@ -155,7 +150,6 @@ module Scheduler # ?job_file=/lkp/scheduled/job.yaml&job_state=post_run&job_id=10 # ?job_file=/lkp/scheduled/job.yaml&loadavg=0.28 0.82 0.49 1/105 3389&start_time=1587725398&end_time=1587725698&job_id=10 get "/~lkp/cgi-bin/lkp-jobfile-append-var" do |env| - env.response.headers["Connection"] = "close" puts sched.update_job_parameter(env)
"Done" @@ -179,7 +173,6 @@ module Scheduler # response: get "server ip" from cluster state, # return "server=<server ip>". get "/~lkp/cgi-bin/lkp-cluster-sync" do |env| - env.response.headers["Connection"] = "close" response = sched.request_cluster_state(env)
debug_message(env, response) @@ -191,7 +184,6 @@ module Scheduler # /~lkp/cgi-bin/lkp-post-run?job_file=/lkp/scheduled/job.yaml&job_id=40 # curl "http://localhost:3000/~lkp/cgi-bin/lkp-post-run?job_file=/lkp/scheduled/job...." get "/~lkp/cgi-bin/lkp-post-run" do |env| - env.response.headers["Connection"] = "close" # get job_id from request job_id = env.params.query["job_id"]? if job_id @@ -202,14 +194,12 @@ module Scheduler end
get "/~lkp/cgi-bin/lkp-wtmp" do |env| - env.response.headers["Connection"] = "close" puts sched.update_tbox_wtmp(env)
"Done" end
get "/~lkp/cgi-bin/report_ssh_port" do |env| - env.response.headers["Connection"] = "close" testbox = env.params.query["tbox_name"] ssh_port = env.params.query["ssh_port"].to_s job_id = env.params.query["job_id"].to_s diff --git a/src/taskqueue/taskqueue.cr b/src/taskqueue/taskqueue.cr index f41cfa8..0c82333 100644 --- a/src/taskqueue/taskqueue.cr +++ b/src/taskqueue/taskqueue.cr @@ -10,6 +10,10 @@ require "./queue" class TaskQueue VERSION = "0.0.2"
+ before_all do |env| + env.response.headers["Connection"] = "close" + end + # l2pH: limit 2 / hours # l100pms: limit 100 / ms @@rate_limiter = RateLimiter(String).new @@ -42,7 +46,6 @@ class TaskQueue # # response: TaskQueue@v0.0.1 is alive. get "/" do |env| - env.response.headers["Connection"] = "close" response = "TaskQueue@v#{VERSION} is alive." debug_message(env, response, Time.utc)
@@ -61,7 +64,6 @@ class TaskQueue # 400 "Missing parameter <queue>" # 400 "Missing http body" post "/add" do |env| - env.response.headers["Connection"] = "close" response = queue_respond_add(env) debug_message(env, response, Time.utc) response if env.response.status_code == 200 @@ -75,7 +77,6 @@ class TaskQueue # 201 ## when there has no task in queue (scheduler/$tbox_group) # 400 "Missing parameter <queue>" put "/consume" do |env| - env.response.headers["Connection"] = "close" response = queue_respond_consume(env) debug_message(env, response, Time.utc) response if env.response.status_code == 200 @@ -89,7 +90,6 @@ class TaskQueue # 400 "Missing parameter <from|to|id>" # 409 "Can not find id <$id> in queue <scheduler/$tbox_group>" put "/hand_over" do |env| - env.response.headers["Connection"] = "close" response = queue_respond_hand_over(env) debug_message(env, response, Time.utc) nil @@ -103,7 +103,6 @@ class TaskQueue # 400 "Missing parameter <queue|id>" # 409 "Can not find id <$id> in queue <scheduler/$tbox_group>" put "/delete" do |env| - env.response.headers["Connection"] = "close" response = queue_respond_delete(env) debug_message(env, response, Time.utc) nil @@ -119,7 +118,6 @@ class TaskQueue # 400 "Missing parameter <queue>" # 413 "Query results too large keys" get "/keys" do |env| - env.response.headers["Connection"] = "close" response = queue_respond_keys(env) # debug_message(env, response, Time.utc) # maybe too large response.to_json unless env.response.status_code == 201