Skip to content

Commit 7be3b84

Browse files
committed
c
1 parent a5a5efe commit 7be3b84

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

pynamodb_mate/patterns/status_tracker/impl.py

+34-7
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ def is_locked(
744744
def start(
745745
cls,
746746
task_id: str,
747+
allowed_status: T.Optional[T.List[int]] = None,
747748
detailed_error: bool = False,
748749
debug: bool = False,
749750
):
@@ -781,6 +782,14 @@ def start(
781782
)
782783

783784
try:
785+
is_valid_status = (cls.status == cls.config.pending_status) | (
786+
cls.status == cls.config.failed_status
787+
)
788+
if allowed_status is None:
789+
allowed_status = []
790+
else:
791+
for status in allowed_status:
792+
is_valid_status |= cls.status == status
784793
res = task.update(
785794
actions=[
786795
cls.value.set(
@@ -809,10 +818,7 @@ def start(
809818
)
810819
)
811820
)
812-
& (
813-
(cls.status == cls.config.pending_status)
814-
| (cls.status == cls.config.failed_status)
815-
)
821+
& is_valid_status
816822
),
817823
)
818824
except UpdateError as e:
@@ -855,6 +861,27 @@ def start(
855861
)
856862
if debug: # pragma: no cover # pragma: no cover
857863
print("❌ task failed to get lock, because it is ignored.")
864+
elif task_.status not in allowed_status:
865+
if allowed_status:
866+
error = TaskIsNotReadyToStartError(
867+
f"{TaskIsNotReadyToStartError.to_task(cls.config.use_case_id, task_id)} is not ready to start, "
868+
f"the status {task_.status} is not in the allowed status {allowed_status}."
869+
)
870+
if debug:
871+
print(
872+
f"❌ task is not ready to start, "
873+
f"the status {task_.status} is not in the allowed status: {allowed_status}."
874+
)
875+
else:
876+
error = TaskIsNotReadyToStartError.make(
877+
use_case_id=cls.config.use_case_id,
878+
task_id=task_id,
879+
)
880+
if debug:
881+
print(
882+
"❌ task is not ready to start yet, "
883+
"either it is locked or status is not in 'pending' or 'failed'."
884+
)
858885
else: # pragma: no cover
859886
error = NotImplementedError(
860887
f"You found a bug! This error should be handled but not implemented yet, "
@@ -868,7 +895,7 @@ def start(
868895
if debug: # pragma: no cover
869896
print(
870897
"❌ task is not ready to start yet, "
871-
"either it is locked or status is not in 'pending' or 'failed'."
898+
"either it is locked or status is not in 'pending' or 'failed' or allowed status."
872899
)
873900
else: # pragma: no cover
874901
error = e
@@ -962,9 +989,9 @@ def _get_status_index(cls, _is_test: bool = False) -> StatusAndUpdateTimeIndex:
962989
"""
963990
Detect the status index object.
964991
"""
965-
if cls._status_and_update_time_index is None: # pragma: no cover
992+
if cls._status_and_update_time_index is None: # pragma: no cover
966993
# just for local unit test, keep it in the source code intentionally
967-
if _is_test: # pragma: no cover
994+
if _is_test: # pragma: no cover
968995
print("call _get_status_index() ...")
969996
for k, v in inspect.getmembers(cls):
970997
if isinstance(v, StatusAndUpdateTimeIndex):

release-history.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Backlog
2222
**Minor Improvements**
2323

2424
- Add ``s3_put_object_kwargs`` parameter to :meth:`pynamodb_mate.patterns.large_attribute.impl.LargeAttributeMixin.create_large_attribute_item` and :meth:`pynamodb_mate.patterns.large_attribute.impl.LargeAttributeMixin.update_large_attribute_item`. So that user can pass additional arguments to the S3 put object requests.
25+
- Allow ``allowed_status`` parameter to :meth:`pynamodb_mate.patterns.status_tracker.impl.BaseStatusTracker.start`. Allow user to specify the allowed status other than "pending" / "failed".
2526

2627
**Bugfixes**
2728

0 commit comments

Comments
 (0)