Skip to content

Commit

Permalink
feat: post stop return task_id
Browse files Browse the repository at this point in the history
fix #439
  • Loading branch information
MistEO committed Dec 3, 2024
1 parent 8e004f6 commit d0c6162
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 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>(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

0 comments on commit d0c6162

Please sign in to comment.