Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

send EventTaskFailed and EventTaskStopped once the task is stopped #651

Merged
4 commits merged into from
Mar 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/api-service/__app__/onefuzzlib/tasks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def stopping(self) -> None:
ProxyForward.remove_forward(self.task_id)
delete_queue(str(self.task_id), StorageType.corpus)
Node.stop_task(self.task_id)
self.set_state(TaskState.stopped, send=False)
self.set_state(TaskState.stopped)

job = Job.get(self.job_id)
if job:
Expand Down Expand Up @@ -188,43 +188,25 @@ def get_tasks_by_pool_name(cls, pool_name: PoolName) -> List["Task"]:
return pool_tasks

def mark_stopping(self) -> None:
if self.state in [TaskState.stopped, TaskState.stopping]:
if self.state in TaskState.shutting_down():
logging.debug(
"ignoring post-task stop calls to stop %s:%s", self.job_id, self.task_id
)
return

self.set_state(TaskState.stopping, send=False)
send_event(
EventTaskStopped(
job_id=self.job_id,
task_id=self.task_id,
user_info=self.user_info,
config=self.config,
)
)
self.set_state(TaskState.stopping)

def mark_failed(
self, error: Error, tasks_in_job: Optional[List["Task"]] = None
) -> None:
if self.state in [TaskState.stopped, TaskState.stopping]:
if self.state in TaskState.shutting_down():
logging.debug(
"ignoring post-task stop failures for %s:%s", self.job_id, self.task_id
)
return

self.error = error
self.set_state(TaskState.stopping, send=False)

send_event(
EventTaskFailed(
job_id=self.job_id,
task_id=self.task_id,
error=error,
user_info=self.user_info,
config=self.config,
)
)
self.set_state(TaskState.stopping)

self.mark_dependants_failed(tasks_in_job=tasks_in_job)

Expand Down Expand Up @@ -320,7 +302,7 @@ def on_start(self) -> None:
def key_fields(cls) -> Tuple[str, str]:
return ("job_id", "task_id")

def set_state(self, state: TaskState, send: bool = True) -> None:
def set_state(self, state: TaskState) -> None:
if self.state == state:
return

Expand All @@ -330,12 +312,33 @@ def set_state(self, state: TaskState, send: bool = True) -> None:

self.save()

send_event(
EventTaskStateUpdated(
job_id=self.job_id,
task_id=self.task_id,
state=self.state,
end_time=self.end_time,
config=self.config,
if self.state == TaskState.stopped:
if self.error:
send_event(
EventTaskFailed(
job_id=self.job_id,
task_id=self.task_id,
error=self.error,
user_info=self.user_info,
config=self.config,
)
)
else:
send_event(
EventTaskStopped(
job_id=self.job_id,
task_id=self.task_id,
user_info=self.user_info,
config=self.config,
)
)
else:
send_event(
EventTaskStateUpdated(
job_id=self.job_id,
task_id=self.task_id,
state=self.state,
end_time=self.end_time,
config=self.config,
)
)
)