-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountdown.py
More file actions
36 lines (25 loc) · 912 Bytes
/
countdown.py
File metadata and controls
36 lines (25 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from resonate import Resonate, Context
from typing import Generator, Any
from threading import Event
import requests
resonate = Resonate.remote()
@resonate.register
def countdown(ctx: Context, count: int, interval, url: str) -> Generator[Any, Any, Any]:
for i in range(count, 0, -1):
# Send notification
message = f"Countdown: {i}"
yield ctx.run(notify, message=message, url=url)
# Sleep for the specified interval
yield ctx.sleep(interval)
# Final notification
yield ctx.run(notify, message="Countdown complete", url=url)
def notify(_: Context, message: str, url: str) -> None:
print(f"notify: {message}", flush=True)
response = requests.post(url, json={"message": message})
response.raise_for_status()
print("countdown worker running", flush=True)
resonate.start()
try:
Event().wait()
except KeyboardInterrupt:
resonate.stop()