Skip to content

Commit 41e3a61

Browse files
committed
rework status tracker pattern and update the document
1 parent 860ddaf commit 41e3a61

File tree

10 files changed

+2692
-1552
lines changed

10 files changed

+2692
-1552
lines changed

docs/source/06-Status-Tracker/index.ipynb

+1,491-786
Large diffs are not rendered by default.

docs/source/pynamodb_mate/attributes/__init__.rst

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ sub packages and modules
1313
api <api>
1414
compressed <compressed>
1515
encrypted <encrypted>
16-
s3backed <s3backed>
1716

docs/source/pynamodb_mate/attributes/s3backed.rst

-5
This file was deleted.

docs/source/pynamodb_mate/patterns/status_tracker/__init__.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ sub packages and modules
1111
:maxdepth: 1
1212

1313
api <api>
14+
exc <exc>
1415
impl <impl>
1516

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exc
2+
===
3+
4+
.. automodule:: pynamodb_mate.patterns.status_tracker.exc
5+
:members:
+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3+
from .exc import TaskExecutionError
4+
from .exc import TaskIsNotInitializedError
5+
from .exc import TaskIsNotReadyToStartError
6+
from .exc import TaskLockedError
7+
from .exc import TaskAlreadySucceedError
8+
from .exc import TaskIgnoredError
9+
from .impl import StatusNameEnum
310
from .impl import BaseStatusEnum
11+
from .impl import TrackerConfig
412
from .impl import StatusAndUpdateTimeIndex
5-
from .impl import TaskLockedError
6-
from .impl import TaskIgnoredError
7-
from .impl import BaseData
8-
from .impl import BaseErrors
9-
from .impl import BaseStatusTracker
13+
from .impl import BaseTask
14+
from .impl import ExecutionContext
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
class TaskExecutionError(Exception):
5+
"""
6+
The base class for errors raised during task execution.
7+
"""
8+
9+
@staticmethod
10+
def to_task(use_case_id: str, task_id: str) -> str:
11+
return f"Task(use_case_id={use_case_id!r}, task_id={task_id!r})"
12+
13+
14+
class TaskIsNotInitializedError(TaskExecutionError):
15+
"""
16+
Raised when a task is not initialized in DynamoDB table.
17+
"""
18+
19+
@classmethod
20+
def make(cls, use_case_id: str, task_id: str):
21+
return cls(
22+
f"{cls.to_task(use_case_id, task_id)} is not found in DynamoDB table, "
23+
"You have to run ``Task.make_and_save(task_id=...)`` to "
24+
"create an initial tracker item first."
25+
)
26+
27+
28+
class TaskIsNotReadyToStartError(TaskExecutionError):
29+
"""
30+
Raised when a task is not ready to start.
31+
32+
There are two possible reasons:
33+
34+
1. Task is locked.
35+
2. Task status is not pending or failed.
36+
"""
37+
38+
@classmethod
39+
def make(cls, use_case_id: str, task_id: str):
40+
return cls(
41+
f"{cls.to_task(use_case_id, task_id)} is not ready to start, "
42+
"either it is locked or status is not in 'pending' or 'failed'. "
43+
"You may use ``with Task.start(task_id=..., detailed_error=True) as execution_context:`` "
44+
"to get more details."
45+
)
46+
47+
48+
class TaskLockedError(TaskExecutionError):
49+
"""
50+
Raised when a task worker is trying to work on a locked task.
51+
"""
52+
53+
@classmethod
54+
def make(cls, use_case_id: str, task_id: str):
55+
return cls(f"{cls.to_task(use_case_id, task_id)} is locked")
56+
57+
58+
class TaskAlreadySucceedError(TaskExecutionError):
59+
"""
60+
Raised when a task is already in "succeeded" status.
61+
"""
62+
63+
@classmethod
64+
def make(cls, use_case_id: str, task_id: str):
65+
return cls(f"{cls.to_task(use_case_id, task_id)} is already succeeded.")
66+
67+
68+
class TaskIgnoredError(TaskExecutionError):
69+
"""
70+
Raised when a task is already in "ignored" status.
71+
"""
72+
73+
@classmethod
74+
def make(cls, use_case_id: str, task_id: str):
75+
return cls(f"{cls.to_task(use_case_id, task_id)} is ignored.")

0 commit comments

Comments
 (0)