From 2d0b16a624832c7321be9ee893797f97301796b1 Mon Sep 17 00:00:00 2001 From: zhaobangyu <763098346@QQ.com> Date: Fri, 20 Oct 2023 18:12:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/autotest/client.lua | 101 +++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 15 deletions(-) diff --git a/server/autotest/client.lua b/server/autotest/client.lua index 29239921..1f49dbf7 100644 --- a/server/autotest/client.lua +++ b/server/autotest/client.lua @@ -1,20 +1,29 @@ -- 终端 -local log_info = logger.info -local log_err = logger.err -local json_encode = json.encode +local log_info = logger.info +local log_err = logger.err +local json_encode = json.encode +local json_decode = json.decode +local tinsert = table.insert -local update_mgr = quanta.get("update_mgr") -local http = quanta.get("http_client") +local update_mgr = quanta.get("update_mgr") +local http = quanta.get("http_client") -local AUTO_TEST_API = environ.get("AUTO_TEST_API") +local BASE_API = environ.get("AUTO_TEST_API") +local USER_LOGIN = "/testtask/user_login" +local USER_HEART = "/testtask/user_heart" +local USER_TASK_LIST = "/testtask/user_task_list" +local TEST_ALLOT = "/testtask/task_allot" +local CASE_INFO = "/testcase/info" -local Client = singleton() -local prop = property(Client) -prop:reader("login", false) +local Client = singleton() +local prop = property(Client) +prop:reader("id", nil) +prop:reader("task_list", nil) function Client:__init() log_info("[Client][__init]...") update_mgr:attach_second5(self) + update_mgr:attach_second30(self) end -- 获取主机名称 @@ -47,31 +56,93 @@ function Client:git_ver() return git_version or "" end +-- 请求地址 +function Client:request_url(url) + return BASE_API .. url +end + +function Client:is_login() + if not self.id or self.id == "" then + return false + end + return true +end + +-- 加载验证 +function Client:can_load_task() + if not self.task_list then + return true + end + return false +end + +-- 分配验证 +function Client:can_allot_task() + if not next(self.task_list) then + return true + end + return false +end + function Client:on_second5() self:update() end +function Client:on_second30() + -- self:task() +end + -- 账号逻辑 function Client:account() local host_name = self:host_name() - if not self.login then + if not self:is_login() then local git_ver = self:git_ver() - local ok, status, res = http:call_post(AUTO_TEST_API .. "/pressclt/login", {}, {}, + local ok, status, res = http:call_post(self:request_url(USER_LOGIN), {}, {}, { host_name = host_name, gitver = git_ver, host_ip = self:host_ip() }) if not ok or status >= 300 then - log_err("[Client][on_second5] login post failed! code: {}, err: {}", status, res) + log_err("[Client][account] login post failed! code: {}, err: {}", status, res) return end - self.login = true - log_info("[Client][on_second5] login success,host_name:{} git_ver:{}", host_name, git_ver) + local resdata = json_decode(res) + self.id = resdata.id + log_info("[Client][account] login success,host_name:{} git_ver:{} res:{} id:{}", host_name, git_ver, res, self + .id) else - http:call_post(AUTO_TEST_API .. "/pressclt/heart", {}, {}, { host_name = host_name }) + http:call_post(self:request_url(USER_HEART), {}, {}, { id = self.id }) end end -- 任务逻辑 function Client:task() + if not self:is_login() then + return + end + -- 获取任务 + if self:can_load_task() then + local ok, status, res = http:call_post(self:request_url(USER_TASK_LIST), {}, {}, { id = self.id }) + if not ok or status >= 300 then + log_err("[Client][task] task_list post failed! code: {}, err: {}", status, res) + return + end + local resdata = json_decode(res) + self.task_list = resdata.list or {} + -- 分配任务 + elseif self:can_allot_task() then + local ok, status, res = http:call_post(self:request_url(TEST_ALLOT), {}, {}, { id = self.id }) + if not ok or status >= 300 then + log_err("[Client][task] task_list post failed! code: {}, err: {}", status, res) + return + end + local resdata = json_decode(res) + for _, task in pairs(resdata.list or {}) do + tinsert(self.task_list, task) + end + else + if next(self.task_list)then + + end + end end -- 更新定时器