Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a dataclass for indexer worker TaskStatus #4601

Merged
merged 4 commits into from
Jul 19, 2024
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
37 changes: 27 additions & 10 deletions indexer_worker/indexer_worker/tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Simple in-memory tracking of executed tasks."""

from __future__ import annotations

import datetime
from dataclasses import dataclass
krysal marked this conversation as resolved.
Show resolved Hide resolved
from multiprocessing.sharedctypes import Synchronized
from typing import Any


def _time_fmt(timestamp: int) -> str | None:
def _time_fmt(timestamp: float) -> str | None:
"""
Format the timestamp into a human-readable date and time notation.

Expand All @@ -16,6 +21,16 @@ def _time_fmt(timestamp: int) -> str | None:
return str(datetime.datetime.utcfromtimestamp(timestamp))


@dataclass
class TaskInfo:
task: Any
start_time: float
model: str
target_index: str
finish_time: Synchronized[float]
progress: Synchronized[float]


class TaskTracker:
def __init__(self):
self.tasks = {}
Expand All @@ -27,9 +42,11 @@ def add_task(self, task_id: str, **kwargs):
:param task: the task being performed
:param task_id: the UUID of the task
"""
self.tasks[task_id] = {
"start_time": datetime.datetime.utcnow().timestamp(),
} | kwargs
task_info = TaskInfo(
start_time=datetime.datetime.utcnow().timestamp(), **kwargs
)

self.tasks[task_id] = task_info

def get_task_status(self, task_id: str) -> dict:
"""
Expand All @@ -39,12 +56,12 @@ def get_task_status(self, task_id: str) -> dict:
:return: response dictionary containing all relevant info about the task
"""
task_info = self.tasks[task_id]
active = task_info["task"].is_alive()
model = task_info["model"]
target_index = task_info["target_index"]
start_time = task_info["start_time"]
finish_time = task_info["finish_time"].value
progress = task_info["progress"].value
active = task_info.task.is_alive()
model = task_info.model
target_index = task_info.target_index
start_time = task_info.start_time
finish_time = task_info.finish_time.value
progress = task_info.progress.value

return {
"task_id": task_id,
Expand Down