-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_server.py
More file actions
46 lines (38 loc) · 1.38 KB
/
example_server.py
File metadata and controls
46 lines (38 loc) · 1.38 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
import signal
from cronken import Cronken
test_jobs = {
"minutely": {
"cron_args": {
"cronstring": "* * * * *"
},
"job_args": {
"cmd": "echo 'minutely job'",
"lock": True,
"ttl": 10
}
}
}
async def main():
loop = asyncio.get_running_loop()
cronken = Cronken(redis_info={"host": "localhost", "port": 6379})
async def graceful_shutdown(sig: signal.Signals):
cronken.logger.error(f"Received signal {sig}, gracefully shutting down...")
await cronken.cleanup()
cronken.logger.error("Finished cleanup")
# Jobs persist in redis and are automatically loaded on cronken startup
cronken.logger.info("Starting cronken...")
cronken_lifetime = await cronken.start()
# Schedule cleanup on SIGINT
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, lambda: asyncio.create_task(graceful_shutdown(sig)))
# For the purposes of this example, we'll overwrite the jobs in redis with the example above
cronken.logger.info("Setting jobs...")
await cronken.set_jobs(test_jobs)
# Now that we've overwritten the jobs, reload them
cronken.logger.info("Reloading jobs...")
await cronken.reload_jobs()
cronken.logger.info("Running...")
await cronken_lifetime
if __name__ == '__main__':
asyncio.run(main())