From e085972537ea67b1513dfd30300f5412813786a1 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 3 Dec 2024 23:29:50 +0800 Subject: [PATCH] feat: post stop return task_id (#441) --- include/MaaFramework/Instance/MaaTasker.h | 2 +- source/MaaFramework/API/MaaTasker.cpp | 5 ++--- source/MaaFramework/API/MaaTypes.h | 2 +- source/MaaFramework/Task/EmptyTask.h | 20 ++++++++++++++++++++ source/MaaFramework/Tasker/Tasker.cpp | 20 +++++++++++--------- source/MaaFramework/Tasker/Tasker.h | 2 +- source/binding/Python/maa/tasker.py | 19 ++++++------------- 7 files changed, 42 insertions(+), 28 deletions(-) create mode 100644 source/MaaFramework/Task/EmptyTask.h diff --git a/include/MaaFramework/Instance/MaaTasker.h b/include/MaaFramework/Instance/MaaTasker.h index ec1c25853..25a86cd24 100644 --- a/include/MaaFramework/Instance/MaaTasker.h +++ b/include/MaaFramework/Instance/MaaTasker.h @@ -46,7 +46,7 @@ extern "C" MAA_FRAMEWORK_API MaaBool MaaTaskerRunning(const MaaTasker* tasker); - MAA_FRAMEWORK_API MaaBool MaaTaskerPostStop(MaaTasker* tasker); + MAA_FRAMEWORK_API MaaTaskId MaaTaskerPostStop(MaaTasker* tasker); MAA_FRAMEWORK_API MaaResource* MaaTaskerGetResource(const MaaTasker* tasker); diff --git a/source/MaaFramework/API/MaaTasker.cpp b/source/MaaFramework/API/MaaTasker.cpp index c004e9264..6da6be393 100644 --- a/source/MaaFramework/API/MaaTasker.cpp +++ b/source/MaaFramework/API/MaaTasker.cpp @@ -119,7 +119,7 @@ MaaBool MaaTaskerRunning(const MaaTasker* tasker) return tasker->running(); } -MaaBool MaaTaskerPostStop(MaaTasker* tasker) +MaaTaskId MaaTaskerPostStop(MaaTasker* tasker) { LogFunc << VAR_VOIDP(tasker); @@ -128,8 +128,7 @@ MaaBool MaaTaskerPostStop(MaaTasker* tasker) return false; } - tasker->post_stop(); - return true; + return tasker->post_stop(); } MaaResource* MaaTaskerGetResource(const MaaTasker* tasker) diff --git a/source/MaaFramework/API/MaaTypes.h b/source/MaaFramework/API/MaaTypes.h index e9ef3370c..6f245699b 100644 --- a/source/MaaFramework/API/MaaTypes.h +++ b/source/MaaFramework/API/MaaTypes.h @@ -83,7 +83,7 @@ struct MaaTasker virtual MaaStatus wait(MaaTaskId task_id) const = 0; virtual bool running() const = 0; - virtual void post_stop() = 0; + virtual MaaTaskId post_stop() = 0; virtual MaaResource* resource() const = 0; virtual MaaController* controller() const = 0; diff --git a/source/MaaFramework/Task/EmptyTask.h b/source/MaaFramework/Task/EmptyTask.h new file mode 100644 index 000000000..e2777fe5a --- /dev/null +++ b/source/MaaFramework/Task/EmptyTask.h @@ -0,0 +1,20 @@ +#pragma once + +#include "TaskBase.h" + +MAA_TASK_NS_BEGIN + +// for MaaTaskerPostStop, as a stop mark +class EmptyTask : public TaskBase +{ +public: + using TaskBase::TaskBase; + + virtual ~EmptyTask() override = default; + + virtual bool run() override { return true; } + + virtual void post_stop() override {} +}; + +MAA_TASK_NS_END diff --git a/source/MaaFramework/Tasker/Tasker.cpp b/source/MaaFramework/Tasker/Tasker.cpp index ec4c2d824..df9114a8f 100644 --- a/source/MaaFramework/Tasker/Tasker.cpp +++ b/source/MaaFramework/Tasker/Tasker.cpp @@ -5,6 +5,7 @@ #include "Controller/ControllerAgent.h" #include "MaaFramework/MaaMsg.h" #include "Resource/ResourceMgr.h" +#include "Task/EmptyTask.h" #include "Task/PipelineTask.h" #include "Utils/Logger.h" @@ -72,6 +73,10 @@ MaaTaskId Tasker::post_pipeline(const std::string& entry, const json::value& pip { LogInfo << VAR(entry) << VAR(pipeline_override); + if (!check_stop()) { + return MaaInvalidId; + } + auto task_ptr = std::make_shared(entry, this); return post_task(std::move(task_ptr), pipeline_override); } @@ -103,7 +108,7 @@ bool Tasker::running() const && !running_task_; } -void Tasker::post_stop() +MaaTaskId Tasker::post_stop() { LogFunc; @@ -121,6 +126,9 @@ void Tasker::post_stop() if (controller_) { controller_->post_stop(); } + + auto task_ptr = std::make_shared(std::string(MAA_FUNCTION), this); + return post_task(std::move(task_ptr), {}); } MAA_RES_NS::ResourceMgr* Tasker::resource() const @@ -189,10 +197,6 @@ MaaTaskId Tasker::post_task(TaskPtr task_ptr, const json::value& pipeline_overri } #endif - if (!check_stop()) { - return MaaInvalidId; - } - MaaTaskId task_id = task_ptr->task_id(); bool ov = task_ptr->override_pipeline(pipeline_override); if (!ov) { @@ -224,10 +228,8 @@ bool Tasker::run_task(RunnerId runner_id, TaskPtr task_ptr) running_task_ = task_ptr; OnScopeLeave([&] { running_task_ = nullptr; }); - // 考虑 post_stop 的时序问题,这里需要先给 running_task_ 赋值,再检查 need_to_stop_ - if (!check_stop()) { - LogError << "stopping, ignore new task"; - return false; + if (need_to_stop_) { + running_task_->post_stop(); } MaaTaskId task_id = task_ptr->task_id(); diff --git a/source/MaaFramework/Tasker/Tasker.h b/source/MaaFramework/Tasker/Tasker.h index d965e57f3..5211a700f 100644 --- a/source/MaaFramework/Tasker/Tasker.h +++ b/source/MaaFramework/Tasker/Tasker.h @@ -36,7 +36,7 @@ class Tasker : public MaaTasker virtual MaaStatus wait(MaaTaskId task_id) const override; virtual bool running() const override; - virtual void post_stop() override; + virtual MaaTaskId post_stop() override; virtual MAA_RES_NS::ResourceMgr* resource() const override; virtual MAA_CTRL_NS::ControllerAgent* controller() const override; diff --git a/source/binding/Python/maa/tasker.py b/source/binding/Python/maa/tasker.py index 1b7d204df..b7e16c0ed 100644 --- a/source/binding/Python/maa/tasker.py +++ b/source/binding/Python/maa/tasker.py @@ -92,8 +92,8 @@ def running(self) -> bool: return bool(Library.framework.MaaTaskerRunning(self._handle)) def post_stop(self) -> Job: - Library.framework.MaaTaskerPostStop(self._handle) - return Job(MaaId(0), self._stop_status, self._stop_wait) + taskid = Library.framework.MaaTaskerPostStop(self._handle) + return self._gen_task_job(taskid) def get_latest_node(self, name: str) -> Optional[NodeDetail]: c_node_id = MaaNodeId() @@ -201,15 +201,6 @@ def _task_status(self, id: int) -> ctypes.c_int32: def _task_wait(self, id: int) -> ctypes.c_int32: return Library.framework.MaaTaskerWait(self._handle, id) - def _stop_status(self, id: int) -> MaaStatusEnum: - return MaaStatusEnum.succeeded if not self.running else MaaStatusEnum.running - - def _stop_wait(self, id: int) -> MaaStatusEnum: - # TODO: 这个应该由 callback 来处理 - while self.running: - time.sleep(0.1) - return MaaStatusEnum.succeeded - def get_recognition_detail(self, reco_id: int) -> Optional[RecognitionDetail]: name = StringBuffer() algorithm = StringBuffer() @@ -316,7 +307,9 @@ def get_task_detail(self, task_id: int) -> Optional[TaskDetail]: detail = self.get_node_detail(int(c_node_id_list[i])) nodes.append(detail) - return TaskDetail(task_id=task_id, entry=entry.get(), nodes=nodes, status=Status(status)) + return TaskDetail( + task_id=task_id, entry=entry.get(), nodes=nodes, status=Status(status) + ) _api_properties_initialized: bool = False @@ -398,7 +391,7 @@ def _set_api_properties(): Library.framework.MaaTaskerRunning.restype = MaaBool Library.framework.MaaTaskerRunning.argtypes = [MaaTaskerHandle] - Library.framework.MaaTaskerPostStop.restype = MaaBool + Library.framework.MaaTaskerPostStop.restype = MaaTaskId Library.framework.MaaTaskerPostStop.argtypes = [MaaTaskerHandle] Library.framework.MaaTaskerGetResource.restype = MaaResourceHandle