Skip to content

Commit

Permalink
feat: post stop return task_id (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO authored Dec 3, 2024
1 parent cc75236 commit e085972
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion include/MaaFramework/Instance/MaaTasker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions source/MaaFramework/API/MaaTasker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ MaaBool MaaTaskerRunning(const MaaTasker* tasker)
return tasker->running();
}

MaaBool MaaTaskerPostStop(MaaTasker* tasker)
MaaTaskId MaaTaskerPostStop(MaaTasker* tasker)
{
LogFunc << VAR_VOIDP(tasker);

Expand All @@ -128,8 +128,7 @@ MaaBool MaaTaskerPostStop(MaaTasker* tasker)
return false;
}

tasker->post_stop();
return true;
return tasker->post_stop();
}

MaaResource* MaaTaskerGetResource(const MaaTasker* tasker)
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/API/MaaTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions source/MaaFramework/Task/EmptyTask.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 11 additions & 9 deletions source/MaaFramework/Tasker/Tasker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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<MAA_TASK_NS::PipelineTask>(entry, this);
return post_task(std::move(task_ptr), pipeline_override);
}
Expand Down Expand Up @@ -103,7 +108,7 @@ bool Tasker::running() const
&& !running_task_;
}

void Tasker::post_stop()
MaaTaskId Tasker::post_stop()
{
LogFunc;

Expand All @@ -121,6 +126,9 @@ void Tasker::post_stop()
if (controller_) {
controller_->post_stop();
}

auto task_ptr = std::make_shared<MAA_TASK_NS::EmptyTask>(std::string(MAA_FUNCTION), this);
return post_task(std::move(task_ptr), {});
}

MAA_RES_NS::ResourceMgr* Tasker::resource() const
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Tasker/Tasker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 6 additions & 13 deletions source/binding/Python/maa/tasker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e085972

Please sign in to comment.