On Mon, Jan 25, 2021 at 08:26:13PM +0800, Wu Zhende wrote:
Managing the life cycles of jobs and testboxs. Includes a series of query interfaces and data maintenance interfaces.
Signed-off-by: Wu Zhende wuzhende666@163.com
src/lib/lifecycle.cr | 39 ++++++++++++++++++++++++++++++++++++++ src/lib/web_env.cr | 9 +++++++++ src/lifecycle.cr | 16 ++++++++++++++++ src/lifecycle/constants.cr | 4 ++++ src/lifecycle/lifecycle.cr | 32 +++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 src/lib/lifecycle.cr create mode 100644 src/lifecycle.cr create mode 100644 src/lifecycle/constants.cr create mode 100644 src/lifecycle/lifecycle.cr
diff --git a/src/lib/lifecycle.cr b/src/lib/lifecycle.cr new file mode 100644 index 0000000..a577b3e --- /dev/null +++ b/src/lib/lifecycle.cr @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+require "kemal" +require "yaml"
+require "./web_env" +require "../scheduler/elasticsearch_client"
+class Lifecycle
- property es
- def initialize(env : HTTP::Server::Context)
- @es = Elasticsearch::Client.new
- @env = env
- @log = env.log.as(JSONLogger)
- end
- def alive(version)
- "LKP Alive! The time is #{Time.local}, version = #{version}"
LKP => Lifecycle?
Thanks, Xueliang
- rescue e
- @log.warn(e)
- end
- def get_running_testbox
- size = @env.params.query["size"]? || 20
- from = @env.params.query["from"]? || 0
- query = {
"size" => size,
"from" => from,
"query" => {
"terms" => {
"state" => ["booting", "running"]
}
}
- }
- @es.search("testbox", query)
- end
+end diff --git a/src/lib/web_env.cr b/src/lib/web_env.cr index d33b4c0..b3a90a8 100644 --- a/src/lib/web_env.cr +++ b/src/lib/web_env.cr @@ -2,6 +2,7 @@ # Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
require "./sched" +require "./lifecycle" require "./json_logger"
class HTTP::Server @@ -22,5 +23,13 @@ class HTTP::Server def log @log ||= create_log end
- def lifecycle
@lifecycle ||= create_lifecycle
- end
- def create_lifecycle
@lifecycle = Lifecycle.new(self)
- end end
end diff --git a/src/lifecycle.cr b/src/lifecycle.cr new file mode 100644 index 0000000..a864621 --- /dev/null +++ b/src/lifecycle.cr @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+require "lifecycle/lifecycle" +require "./lifecycle/constants.cr" +require "./lib/json_logger"
+module Cycle
- log = JSONLogger.new
- begin
- Kemal.run(LIFECYCLE_PORT)
- rescue e
- log.error(e)
- end
+end diff --git a/src/lifecycle/constants.cr b/src/lifecycle/constants.cr new file mode 100644 index 0000000..a428251 --- /dev/null +++ b/src/lifecycle/constants.cr @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+LIFECYCLE_PORT = (ENV.has_key?("LIFECYCLE_PORT") ? ENV["LIFECYCLE_PORT"] : 11311).to_i32 diff --git a/src/lifecycle/lifecycle.cr b/src/lifecycle/lifecycle.cr new file mode 100644 index 0000000..4154a27 --- /dev/null +++ b/src/lifecycle/lifecycle.cr @@ -0,0 +1,32 @@ +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+require "kemal"
+require "../lib/web_env" +require "../lib/lifecycle" +require "../lib/json_logger"
+module Cycle
- VERSION = "0.1.0"
- add_context_storage_type(Time::Span)
- before_all do |env|
- env.set "start_time", Time.monotonic
- env.response.headers["Connection"] = "close"
- env.create_log
- env.create_lifecycle
- end
- # echo alive
- get "/" do |env|
- env.lifecycle.alive(VERSION)
- end
- # find the testbox that are performing jobs
- # curl http://localhost:11311/get_running_testbox?size=10&from=0
- get "/get_running_testbox" do |env|
- env.lifecycle.get_running_testbox.to_json
- end
+end
2.23.0