Skip to content

Commit

Permalink
Workflow: Add timedelta to create_timer (#636)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Verst <github@bernd.dev>
  • Loading branch information
berndverst authored Nov 15, 2023
1 parent 61c8b31 commit bd11de1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/workflow/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def status_monitor_workflow(ctx: wf.DaprWorkflowContext, job: JobStatus):
ctx.call_activity(send_alert, input=f"Job '{job.job_id}' is unhealthy!")
next_sleep_interval = 5 # check more frequently when unhealthy

yield ctx.create_timer(fire_at=ctx.current_utc_datetime + timedelta(seconds=next_sleep_interval))
yield ctx.create_timer(fire_at=timedelta(seconds=next_sleep_interval))

# restart from the beginning with a new JobStatus input
ctx.continue_as_new(job)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
limitations under the License.
"""

from typing import Any, Callable, List, Optional, TypeVar
from datetime import datetime
from typing import Any, Callable, List, Optional, TypeVar, Union
from datetime import datetime, timedelta

from durabletask import task

Expand Down Expand Up @@ -48,7 +48,7 @@ def current_utc_datetime(self) -> datetime:
def is_replaying(self) -> bool:
return self.__obj.is_replaying

def create_timer(self, fire_at: datetime) -> task.Task:
def create_timer(self, fire_at: Union[datetime, timedelta]) -> task.Task:
return self.__obj.create_timer(fire_at)

def call_activity(self, activity: Callable[[WorkflowActivityContext, TInput], TOutput], *,
Expand Down
8 changes: 4 additions & 4 deletions ext/dapr-ext-workflow/dapr/ext/workflow/workflow_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from __future__ import annotations
from abc import ABC, abstractmethod
from datetime import datetime
from datetime import datetime, timedelta
from typing import Any, Callable, Generator, Optional, TypeVar, Union

from durabletask import task
Expand Down Expand Up @@ -85,13 +85,13 @@ def is_replaying(self) -> bool:
pass

@abstractmethod
def create_timer(self, fire_at: datetime) -> task.Task:
def create_timer(self, fire_at: Union[datetime, timedelta]) -> task.Task:
"""Create a Timer Task to fire after at the specified deadline.
Parameters
----------
fire_at: datetime.datetime
The time for the timer to trigger
fire_at: datetime.datetime | datetime.timedelta
The time for the timer to trigger. Can be specified as a `datetime` or a `timedelta`.
Returns
-------
Expand Down

0 comments on commit bd11de1

Please sign in to comment.