12 std::shared_ptr<std::atomic_bool>
cancelled = std::make_shared<std::atomic_bool>(
false);
15TaskContext::TaskContext(std::shared_ptr<std::atomic_bool>
cancelled, std::function<
void(
float)> progress)
19 return cancelled_ && cancelled_->load(std::memory_order_relaxed);
23 if (progress_) progress_(std::clamp(progress, 0.0F, 1.0F));
28 worker_ = std::thread([
this] { workerLoop(); });
33 std::lock_guard lock(mutex_);
35 for (
auto& [
id, task] : tasks_) {
37 task->cancelled->store(
true, std::memory_order_relaxed);
40 workReady_.notify_all();
41 if (worker_.joinable()) worker_.join();
45 if (
name.empty() || !work)
46 return eve::editing::failed<TaskId>(Status::Rejected,
RuleId(
"editing.task.invalid"),
47 "Task name and worker are required");
48 auto task = std::make_shared<TaskRecord>();
49 task->snapshot.name = std::move(
name);
50 task->work = std::move(work);
53 std::lock_guard lock(mutex_);
55 return eve::editing::failed<TaskId>(Status::Rejected,
RuleId(
"editing.task.stopping"),
56 "Task service is stopping");
57 task->snapshot.id =
TaskId(
"editor.task." + std::to_string(++sequence_));
58 id = task->snapshot.id;
59 tasks_.emplace(
id, task);
60 queue_.push_back(std::move(task));
62 workReady_.notify_one();
63 return eve::editing::applied<TaskId>(
id);
67 std::lock_guard lock(mutex_);
68 auto found = tasks_.find(task);
69 if (
found == tasks_.end())
70 return eve::editing::failed<void>(Status::NotFound,
RuleId(
"editing.task.not-found"),
71 "Task does not exist");
75 return eve::editing::failed<void>(Status::Conflict,
RuleId(
"editing.task.already-finished"),
76 "Finished tasks cannot be cancelled");
77 record.cancelled->store(
true, std::memory_order_relaxed);
79 workReady_.notify_all();
80 return eve::editing::applied<void>();
84 std::shared_ptr<TaskRecord> released;
86 std::lock_guard lock(mutex_);
87 auto found = tasks_.find(
id);
88 if (
found == tasks_.end())
89 return editing::failed<void>(Status::NotFound,
RuleId(
"editing.task.not-found"),
"Task does not exist");
91 record.cancelled->store(
true, std::memory_order_relaxed);
94 released =
found->second;
95 std::erase(queue_, released);
102 return editing::applied<void>();
106 std::lock_guard lock(mutex_);
107 auto found = tasks_.find(task);
108 if (
found == tasks_.end())
109 return eve::editing::failed<TaskSnapshot>(Status::NotFound,
RuleId(
"editing.task.not-found"),
110 "Task does not exist");
111 return eve::editing::applied<TaskSnapshot>(
found->second->snapshot);
115 std::unique_lock lock(mutex_);
116 const bool idle = idle_.wait_for(lock, timeout, [
this] {
117 if (!queue_.empty())
return false;
118 return std::none_of(tasks_.begin(), tasks_.end(),
119 [](
const auto& entry) { return entry.second->snapshot.state == TaskState::Running; });
123 RuleId(
"editing.task.wait-timeout"),
124 "Task service did not become idle before the deadline");
125 return eve::editing::applied<void>();
128void TaskService::workerLoop() {
130 std::shared_ptr<TaskRecord> task;
132 std::unique_lock lock(mutex_);
133 workReady_.wait(lock, [
this] {
return stopping_ || !queue_.empty(); });
134 if (stopping_ && queue_.empty())
return;
135 task = queue_.front();
137 if (task->cancelled->load(std::memory_order_relaxed)) {
147 TaskContext
context(task->cancelled, [
this, weak = std::weak_ptr<TaskRecord>(task)](
float value) {
148 if (auto record = weak.lock()) {
149 std::lock_guard lock(mutex_);
150 record->snapshot.progress = value;
154 }
catch (
const std::exception&
error) {
155 outcome.status = Status::Failed;
157 RuleId(
"editor.task.exception"),
158 DiagnosticSeverity::Error,
error.what()));
160 outcome.status = Status::Failed;
162 RuleId(
"editor.task.exception"),
163 DiagnosticSeverity::Error,
164 "Unknown worker exception"));
168 std::lock_guard lock(mutex_);
169 task->snapshot.output = std::move(outcome.output);
170 task->snapshot.diagnostics = std::move(outcome.diagnostics);
171 task->snapshot.progress = 1.0F;
172 if (task->cancelled->load(std::memory_order_relaxed) || outcome.status == Status::Cancelled)
174 else if (outcome.status == Status::Applied || outcome.status == Status::NoOp)
178 if (task->discarded) tasks_.erase(task->snapshot.id);
ScriptComponentRecord record
std::shared_ptr< std::atomic< bool > > cancelled
const VegetationPresetContext & context
Move-only operation result carrying either a value or Status.
UUID-backed identifier adapter for legacy textual boundaries.
void reportProgress(float progress) const
Publish normalized progress in the inclusive range 0..1.
bool isCancellationRequested() const
True after cancellation was requested.
Result< TaskId > submit(std::string name, Work work)
Queue background work and return its stable task identity.
Result< void > cancel(const TaskId &task)
Request cooperative cancellation of queued or running work.
TaskService()
Start the editor background worker.
std::function< TaskOutcome(const TaskContext &)> Work
Result< void > waitIdle(std::chrono::milliseconds timeout)
Wait until no task is queued or running; intended for shutdown and tests.
Result< void > discard(const TaskId &task)
Cancel and release an owned task, including already finished tasks. @ownership Caller relinquishes it...
~TaskService()
Cooperatively cancel pending work and join the worker.
Result< TaskSnapshot > snapshot(const TaskId &task) const
Return a thread-safe task snapshot.
StrongId< RuleIdTag > RuleId
StrongId< TaskIdTag > TaskId
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build an owning common diagnostic carrying an open editing rule identity.