diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-05-15 12:34:57 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-05-15 12:34:57 +0300 |
| commit | ae36f44de7b9adbfa56525dd0046311ea7181288 (patch) | |
| tree | 2cf714e28240204dd95e5b35bcde608d6f135d65 /femtoqueue.py | |
| parent | 8d4fa3b038493df8f2b6108cef669d5434b1736e (diff) | |
Run formatter, add ruff config
Diffstat (limited to 'femtoqueue.py')
| -rw-r--r-- | femtoqueue.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/femtoqueue.py b/femtoqueue.py index bdb9298..faa6a5b 100644 --- a/femtoqueue.py +++ b/femtoqueue.py @@ -3,11 +3,13 @@ from dataclasses import dataclass import time from typing import Generator + @dataclass class FemtoTask: id: str data: bytes + class FemtoQueue: RESERVED_NAMES = [ "creating", @@ -101,8 +103,12 @@ class FemtoQueue: # Only run this every `timeout_stale_ms` milliseconds because iterating # through all tasks is slow timeout_sec = self.timeout_stale_ms / 1000.0 - if self.latest_stale_check_ts is not None and now - self.latest_stale_check_ts < timeout_sec: + if ( + self.latest_stale_check_ts is not None + and now - self.latest_stale_check_ts < timeout_sec + ): return + self.latest_stale_check_ts = now for dir_name in listdir(self.data_dir): @@ -138,14 +144,19 @@ class FemtoQueue: pass # If cache empty, then check assigned tasks in progress (aborted) - self.todo_cache = (path.join(self.dir_in_progress, x) for x in sorted(listdir(self.dir_in_progress))) + self.todo_cache = ( + path.join(self.dir_in_progress, x) + for x in sorted(listdir(self.dir_in_progress)) + ) try: return next(self.todo_cache) except StopIteration: pass # Then check pending tasks - self.todo_cache = (path.join(self.dir_pending, x) for x in sorted(listdir(self.dir_pending))) + self.todo_cache = ( + path.join(self.dir_pending, x) for x in sorted(listdir(self.dir_pending)) + ) try: return next(self.todo_cache) except StopIteration: @@ -166,7 +177,8 @@ class FemtoQueue: while True: task = self._pop_task_path() - if task is None: return None + if task is None: + return None id = path.basename(task) in_progress_path = path.join(self.dir_in_progress, id) @@ -179,7 +191,7 @@ class FemtoQueue: with open(in_progress_path, "rb") as f: content = f.read() - return FemtoTask(id = id, data = content) + return FemtoTask(id=id, data=content) def done(self, task: FemtoTask): """ @@ -196,7 +208,9 @@ class FemtoQueue: try: rename(in_progress_path, done_path) except FileNotFoundError as e: - raise Exception(f"Tried to complete a task that is not in progress, id={task.id}") from e + raise Exception( + f"Tried to complete a task that is not in progress, id={task.id}" + ) from e def fail(self, task: FemtoTask): """ @@ -213,4 +227,6 @@ class FemtoQueue: try: rename(in_progress_path, failed_path) except FileNotFoundError as e: - raise Exception(f"Tried to fail a task that is not in progress, id={task.id}") from e + raise Exception( + f"Tried to fail a task that is not in progress, id={task.id}" + ) from e |
