Skip to content

Commit

Permalink
feat: exp backoff (#279)
Browse files Browse the repository at this point in the history
* feat: exp backoff

* chore: lint

* bump version and add poetry script
  • Loading branch information
abelanger5 authored Dec 5, 2024
1 parent bc5cacb commit e504fda
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 119 deletions.
37 changes: 37 additions & 0 deletions examples/retries_with_backoff/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from hatchet_sdk import Context, Hatchet

hatchet = Hatchet(debug=True)


# ❓ Backoff
@hatchet.workflow()
class BackoffWorkflow:
# 👀 Backoff configuration
@hatchet.step(
retries=10,
# 👀 Maximum number of seconds to wait between retries
backoff_max_seconds=60,
# 👀 Factor to increase the wait time between retries.
# This sequence will be 2s, 4s, 8s, 16s, 32s, 60s... due to the maxSeconds limit
backoff_factor=2.0,
)
def step1(self, context: Context):
if context.retry_count() < 3:
raise Exception("step1 failed")

return {"status": "success"}


# ‼️


def main():
workflow = BackoffWorkflow()
worker = hatchet.worker("backoff-worker", max_runs=4)
worker.register_workflow(workflow)

worker.start()


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ cp $tmp_dir/hatchet_sdk/clients/rest/api/__init__.py $dst_dir/api/__init__.py
# remove tmp folder
rm -rf $tmp_dir

poetry run python -m grpc_tools.protoc --proto_path=hatchet/api-contracts/dispatcher --python_out=./hatchet_sdk/contracts --pyi_out=./hatchet_sdk/contracts --grpc_python_out=./hatchet_sdk/contracts dispatcher.proto
poetry run python -m grpc_tools.protoc --proto_path=hatchet/api-contracts/events --python_out=./hatchet_sdk/contracts --pyi_out=./hatchet_sdk/contracts --grpc_python_out=./hatchet_sdk/contracts events.proto
poetry run python -m grpc_tools.protoc --proto_path=hatchet/api-contracts/workflows --python_out=./hatchet_sdk/contracts --pyi_out=./hatchet_sdk/contracts --grpc_python_out=./hatchet_sdk/contracts workflows.proto
poetry run python -m grpc_tools.protoc --proto_path=../oss/api-contracts/dispatcher --python_out=./hatchet_sdk/contracts --pyi_out=./hatchet_sdk/contracts --grpc_python_out=./hatchet_sdk/contracts dispatcher.proto
poetry run python -m grpc_tools.protoc --proto_path=../oss/api-contracts/events --python_out=./hatchet_sdk/contracts --pyi_out=./hatchet_sdk/contracts --grpc_python_out=./hatchet_sdk/contracts events.proto
poetry run python -m grpc_tools.protoc --proto_path=../oss/api-contracts/workflows --python_out=./hatchet_sdk/contracts --pyi_out=./hatchet_sdk/contracts --grpc_python_out=./hatchet_sdk/contracts workflows.proto

# Fix relative imports in _grpc.py files
if [[ "$OSTYPE" == "darwin"* ]]; then
Expand Down
138 changes: 71 additions & 67 deletions hatchet_sdk/contracts/dispatcher_pb2.py

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions hatchet_sdk/contracts/dispatcher_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ from typing import ClassVar as _ClassVar, Iterable as _Iterable, Mapping as _Map

DESCRIPTOR: _descriptor.FileDescriptor

class SDKS(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
__slots__ = ()
UNKNOWN: _ClassVar[SDKS]
GO: _ClassVar[SDKS]
PYTHON: _ClassVar[SDKS]
TYPESCRIPT: _ClassVar[SDKS]

class ActionType(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
__slots__ = ()
START_STEP_RUN: _ClassVar[ActionType]
Expand Down Expand Up @@ -49,6 +56,10 @@ class ResourceEventType(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
class WorkflowRunEventType(int, metaclass=_enum_type_wrapper.EnumTypeWrapper):
__slots__ = ()
WORKFLOW_RUN_EVENT_TYPE_FINISHED: _ClassVar[WorkflowRunEventType]
UNKNOWN: SDKS
GO: SDKS
PYTHON: SDKS
TYPESCRIPT: SDKS
START_STEP_RUN: ActionType
CANCEL_STEP_RUN: ActionType
START_GET_GROUP_KEY: ActionType
Expand Down Expand Up @@ -81,8 +92,22 @@ class WorkerLabels(_message.Message):
intValue: int
def __init__(self, strValue: _Optional[str] = ..., intValue: _Optional[int] = ...) -> None: ...

class RuntimeInfo(_message.Message):
__slots__ = ("sdkVersion", "language", "languageVersion", "os", "extra")
SDKVERSION_FIELD_NUMBER: _ClassVar[int]
LANGUAGE_FIELD_NUMBER: _ClassVar[int]
LANGUAGEVERSION_FIELD_NUMBER: _ClassVar[int]
OS_FIELD_NUMBER: _ClassVar[int]
EXTRA_FIELD_NUMBER: _ClassVar[int]
sdkVersion: str
language: SDKS
languageVersion: str
os: str
extra: str
def __init__(self, sdkVersion: _Optional[str] = ..., language: _Optional[_Union[SDKS, str]] = ..., languageVersion: _Optional[str] = ..., os: _Optional[str] = ..., extra: _Optional[str] = ...) -> None: ...

class WorkerRegisterRequest(_message.Message):
__slots__ = ("workerName", "actions", "services", "maxRuns", "labels", "webhookId")
__slots__ = ("workerName", "actions", "services", "maxRuns", "labels", "webhookId", "runtimeInfo")
class LabelsEntry(_message.Message):
__slots__ = ("key", "value")
KEY_FIELD_NUMBER: _ClassVar[int]
Expand All @@ -96,13 +121,15 @@ class WorkerRegisterRequest(_message.Message):
MAXRUNS_FIELD_NUMBER: _ClassVar[int]
LABELS_FIELD_NUMBER: _ClassVar[int]
WEBHOOKID_FIELD_NUMBER: _ClassVar[int]
RUNTIMEINFO_FIELD_NUMBER: _ClassVar[int]
workerName: str
actions: _containers.RepeatedScalarFieldContainer[str]
services: _containers.RepeatedScalarFieldContainer[str]
maxRuns: int
labels: _containers.MessageMap[str, WorkerLabels]
webhookId: str
def __init__(self, workerName: _Optional[str] = ..., actions: _Optional[_Iterable[str]] = ..., services: _Optional[_Iterable[str]] = ..., maxRuns: _Optional[int] = ..., labels: _Optional[_Mapping[str, WorkerLabels]] = ..., webhookId: _Optional[str] = ...) -> None: ...
runtimeInfo: RuntimeInfo
def __init__(self, workerName: _Optional[str] = ..., actions: _Optional[_Iterable[str]] = ..., services: _Optional[_Iterable[str]] = ..., maxRuns: _Optional[int] = ..., labels: _Optional[_Mapping[str, WorkerLabels]] = ..., webhookId: _Optional[str] = ..., runtimeInfo: _Optional[_Union[RuntimeInfo, _Mapping]] = ...) -> None: ...

class WorkerRegisterResponse(_message.Message):
__slots__ = ("tenantId", "workerId", "workerName")
Expand Down
Loading

0 comments on commit e504fda

Please sign in to comment.