Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sleep-for-days sample #160

Merged
merged 6 commits into from
Jan 30, 2025
Merged

sleep-for-days sample #160

merged 6 commits into from
Jan 30, 2025

Conversation

THardy98
Copy link
Contributor

This sample demonstrates how to use Temporal to run a workflow that periodically sleeps for a number of days.

What was changed

added sleep-for-days sample

Any docs updates needed?

I believe so @webchick

@THardy98 THardy98 requested a review from a team January 27, 2025 16:10

@workflow.run
async def run(self, input: SleepForDaysInput) -> str:
while(not self.is_complete):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit JS-y :)

await workflow.execute_activity(send_email, SendEmailInput(f"{input.numOfDays} until the next email"), start_to_close_timeout=timedelta(seconds=10))
await workflow.wait([
asyncio.create_task(workflow.sleep(input.numOfDays * 24 * 60 * 60)),
asyncio.create_task(workflow.wait_condition(lambda: self.is_complete))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be simpler to use the timeout argument of wait_condition instead of racing these two tasks.

from datetime import timedelta
from temporalio import workflow
from dataclasses import dataclass
from sleep_for_days.activities import send_email, SendEmailInput
Copy link
Contributor

@dandavison dandavison Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In workflow code we import activity code with the sandbox disabled, as follows:

with workflow.unsafe.imports_passed_through():
    from ...

It's hard to remember to do it though... would be good to improve that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I noticed that with the other samples, but the unsafe scared me so I imported it normally.

@dandavison
Copy link
Contributor

One more thing -- can you add a test? (I'd like us to have tests for all samples)

@THardy98
Copy link
Contributor Author

One more thing -- can you add a test? (I'd like us to have tests for all samples)

Added a test

# Expect no more activity calls to have been made - workflow is complete.
assert num_activity_executions == 4
# Expect more than 90 days to have passed.
assert (await env.get_current_time() - start_time) > timedelta(days=90)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor

@dandavison dandavison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@THardy98 THardy98 merged commit 2f3f2ac into main Jan 30, 2025
10 checks passed
@THardy98 THardy98 deleted the sleep-for-days branch January 30, 2025 19:23
Comment on lines +24 to +32
await workflow.wait(
[
asyncio.create_task(workflow.sleep(timedelta(days=30))),
asyncio.create_task(
workflow.wait_condition(lambda: self.is_complete)
),
],
return_when=asyncio.FIRST_COMPLETED,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait_condition has a timeout which I think would have been clearer here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants