Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c09a59c

Browse files
committed
sleep-for-days sample
1 parent 8179fdc commit c09a59c

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

sleep_for_days/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Sleep for Days
2+
3+
This sample demonstrates how to use Temporal to run a workflow that periodically sleeps for a number of days.
4+
5+
To run, first see the main [README.md](../../README.md) for prerequisites.
6+
7+
Then create two terminals and `cd` to this directory.
8+
9+
Run the worker in one terminal:
10+
11+
poetry run python worker.py
12+
13+
And execute the workflow in the other terminal:
14+
15+
poetry run python starter.py
16+
17+
This sample will run indefinitely until you send a signal to `complete`. See how to send a signal via Temporal CLI [here](https://docs.temporal.io/cli/workflow#signal).
18+

sleep_for_days/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TASK_QUEUE = "sleep-for-days-task-queue"

sleep_for_days/activities.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from dataclasses import dataclass
2+
from temporalio import activity
3+
4+
@dataclass
5+
class SendEmailInput:
6+
email_msg: str
7+
8+
@activity.defn(name="send_email")
9+
async def send_email(input: SendEmailInput) -> str:
10+
"""
11+
A stub Activity for sending an email.
12+
"""
13+
result = f"Email message: {input.email_msg}, sent"
14+
print(result)
15+
return result

sleep_for_days/starter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import asyncio
2+
from typing import Optional
3+
import uuid
4+
5+
from temporalio.client import Client
6+
7+
from sleep_for_days import TASK_QUEUE
8+
from sleep_for_days.workflows import (
9+
SleepForDaysWorkflow,
10+
SleepForDaysInput,
11+
)
12+
13+
async def main(client: Optional[Client] = None):
14+
client = client or await Client.connect("localhost:7233")
15+
wf_handle = await client.start_workflow(
16+
SleepForDaysWorkflow.run,
17+
SleepForDaysInput(numOfDays=3),
18+
id=f"sleep-for-days-workflow-id-{uuid.uuid4()}",
19+
task_queue=TASK_QUEUE,
20+
)
21+
# Wait for workflow completion (runs indefinitely until it receives a signal)
22+
print(await wf_handle.result());
23+
24+
if __name__ == "__main__":
25+
asyncio.run(main())

sleep_for_days/worker.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
3+
from temporalio.client import Client
4+
from temporalio.worker import Worker
5+
6+
from sleep_for_days import TASK_QUEUE
7+
from sleep_for_days.activities import send_email
8+
from sleep_for_days.workflows import SleepForDaysWorkflow
9+
10+
async def main():
11+
client = await Client.connect("localhost:7233")
12+
13+
worker = Worker(
14+
client,
15+
task_queue=TASK_QUEUE,
16+
workflows=[SleepForDaysWorkflow],
17+
activities=[send_email],
18+
)
19+
20+
await worker.run()
21+
22+
if __name__ == "__main__":
23+
asyncio.run(main())

sleep_for_days/workflows.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from datetime import timedelta
2+
from temporalio import workflow
3+
from dataclasses import dataclass
4+
from sleep_for_days.activities import send_email, SendEmailInput
5+
6+
@dataclass
7+
class SleepForDaysInput:
8+
numOfDays: int
9+
10+
@workflow.defn(name="SleepForDaysWorkflow")
11+
class SleepForDaysWorkflow:
12+
13+
def __init__(self) -> None:
14+
self.is_complete = False
15+
16+
@workflow.run
17+
async def run(self, input: SleepForDaysInput) -> str:
18+
while(not self.is_complete):
19+
await workflow.execute_activity(send_email, SendEmailInput(f"{input.numOfDays} until the next email"), start_to_close_timeout=timedelta(seconds=10))
20+
await workflow.sleep(timedelta(days=input.numOfDays))
21+
return "done!"
22+
23+
@workflow.signal
24+
def complete(self):
25+
self.is_complete = True

0 commit comments

Comments
 (0)