Use methods in env.cr to simplify the obtaining of sched and log. Replace get_sched_from_env(env) with env.sched. Replace log(env) with env.log.
Signed-off-by: Wu Zhende wuzhende666@163.com --- src/scheduler/scheduler.cr | 51 ++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 30 deletions(-)
diff --git a/src/scheduler/scheduler.cr b/src/scheduler/scheduler.cr index 17a6f15..4fde6e3 100644 --- a/src/scheduler/scheduler.cr +++ b/src/scheduler/scheduler.cr @@ -3,6 +3,7 @@
require "kemal"
+require "../lib/env" require "../lib/sched" require "../lib/json_logger"
@@ -31,28 +32,18 @@ require "../lib/json_logger" module Scheduler VERSION = "0.2.0"
- add_context_storage_type(Sched) - add_context_storage_type(JSONLogger) add_context_storage_type(Time::Span)
before_all do |env| env.set "start_time", Time.monotonic - env.set "log", JSONLogger.new(env: env) - env.set "sched", Sched.new(env) env.response.headers["Connection"] = "close" - end - - def self.get_sched_from_env(env) - env.get("sched").as(Sched) - end - - def self.log(env) - env.get("log").as(JSONLogger) + env.create_log + env.create_sched end
# for debug (maybe kemal debug|logger does better) def self.debug_message(env, response) - log(env).info(%({"from": "#{env.request.remote_address}", "response": #{response.to_json}})) + env.log.info(%({"from": "#{env.request.remote_address}", "response": #{response.to_json}})) end
# echo alive @@ -67,20 +58,20 @@ module Scheduler # /boot.xxx/host/${hostname} # /boot.yyy/mac/${mac} get "/boot.:boot_type/:parameter/:value" do |env| - response = get_sched_from_env(env).find_job_boot + response = env.sched.find_job_boot
job_id = response[/tmpfs/(.*)/job.cgz/, 1]? - log(env).info(%({"job_id": "#{job_id}", "job_state": "boot"})) if job_id + env.log.info(%({"job_id": "#{job_id}", "job_state": "boot"})) if job_id
response end
# /~lkp/cgi-bin/gpxelinux.cgi?hostname=:hostname&mac=:mac&last_kernel=:last_kernel get "/~lkp/cgi-bin/gpxelinux.cgi" do |env| - response = get_sched_from_env(env).find_next_job_boot + response = env.sched.find_next_job_boot
job_id = response[/tmpfs/(.*)/job.cgz/, 1]? - log(env).info(%({"job_id": "#{job_id}", "job_state": "boot"})) if job_id + env.log.info(%({"job_id": "#{job_id}", "job_state": "boot"})) if job_id
response end @@ -89,10 +80,10 @@ module Scheduler # - echo job_id to caller # -- job_id = "0" ? means failed post "/submit_job" do |env| - job_messages = get_sched_from_env(env).submit_job + job_messages = env.sched.submit_job
job_messages.each do |job_message| - log(env).info(job_message.to_json) + env.log.info(job_message.to_json) end
job_messages.to_json @@ -104,7 +95,7 @@ module Scheduler job_package = env.params.url["job_package"] file_path = ::File.join [Kemal.config.public_folder, job_id, job_package]
- log(env).info(%({"job_id": "#{job_id}", "job_state": "download"})) + env.log.info(%({"job_id": "#{job_id}", "job_state": "download"}))
send_file env, file_path end @@ -117,7 +108,7 @@ 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| if (client_hostname = env.params.query["hostname"]?) && (client_mac = env.params.query["mac"]?) - get_sched_from_env(env).set_host_mac(client_mac, client_hostname) + env.sched.set_host_mac(client_mac, client_hostname)
"Done" else @@ -128,7 +119,7 @@ module Scheduler # curl -X PUT "http://localhost:3000/set_host2queues?queues=vm-2p8g.aarch64&host=vm-2p8..." put "/set_host2queues" do |env| if (client_queues = env.params.query["queues"]?) && (client_host = env.params.query["host"]?) - get_sched_from_env(env).set_host2queues(client_host, client_queues) + env.sched.set_host2queues(client_host, client_queues)
"Done" else @@ -139,7 +130,7 @@ module Scheduler # curl -X PUT "http://localhost:3000/del_host_mac?mac=00-01-02-03-04-05" put "/del_host_mac" do |env| if client_mac = env.params.query["mac"]? - get_sched_from_env(env).del_host_mac(client_mac) + env.sched.del_host_mac(client_mac)
"Done" else @@ -150,7 +141,7 @@ module Scheduler # curl -X PUT "http://localhost:3000/del_host2queues?host=vm-2p8g.aarch64" put "/del_host2queues" do |env| if client_host = env.params.query["host"]? - get_sched_from_env(env).del_host2queues(client_host) + env.sched.del_host2queues(client_host)
"Done" else @@ -164,7 +155,7 @@ 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| - log(env).info(get_sched_from_env(env).update_job_parameter) + env.log.info(env.sched.update_job_parameter)
"Done" end @@ -187,7 +178,7 @@ module Scheduler # response: get "server ip" from cluster state, # return "server=<server ip>". get "/~lkp/cgi-bin/lkp-cluster-sync" do |env| - response = get_sched_from_env(env).request_cluster_state + response = env.sched.request_cluster_state
debug_message(env, response)
@@ -201,14 +192,14 @@ module Scheduler # get job_id from request job_id = env.params.query["job_id"]? if job_id - log(env).info(get_sched_from_env(env).close_job(job_id)) + env.log.info(env.sched.close_job(job_id)) end
"Done" end
get "/~lkp/cgi-bin/lkp-wtmp" do |env| - log(env).info(get_sched_from_env(env).update_tbox_wtmp) + env.log.info(env.sched.update_tbox_wtmp)
"Done" end @@ -219,10 +210,10 @@ module Scheduler job_id = env.params.query["job_id"].to_s
if testbox && ssh_port - get_sched_from_env(env).report_ssh_port(testbox, ssh_port) + env.sched.report_ssh_port(testbox, ssh_port) end
- log(env).info(%({"job_id": "#{job_id}", "state": "set ssh port", "ssh_port": "#{ssh_port}", "tbox_name": "#{testbox}"})) + env.log.info(%({"job_id": "#{job_id}", "state": "set ssh port", "ssh_port": "#{ssh_port}", "tbox_name": "#{testbox}"}))
"Done" end