main
py 57 lines 1.69 KB
Raw
1 import asyncio
2 from datetime import datetime
3 import time
4 from helpers.task_scheduler import TaskScheduler
5 from helpers.print_style import PrintStyle
6 from helpers import errors
7 from helpers import runtime
8
9
10 SLEEP_TIME = 60
11
12 keep_running = True
13 pause_time = 0
14
15
16 async def run_loop():
17 global pause_time, keep_running
18
19 while True:
20 if runtime.is_development():
21 # Signal to container that the job loop should be paused
22 # if we are runing a development instance to avoid duble-running the jobs
23 try:
24 await runtime.call_development_function(pause_loop)
25 except Exception as e:
26 PrintStyle().error("Failed to pause job loop by development instance: " + errors.error_text(e))
27 if not keep_running and (time.time() - pause_time) > (SLEEP_TIME * 2):
28 resume_loop()
29 if keep_running:
30 try:
31 await scheduler_tick()
32 except Exception as e:
33 PrintStyle().error(errors.format_error(e))
34 await asyncio.sleep(SLEEP_TIME) # TODO! - if we lower it under 1min, it can run a 5min job multiple times in it's target minute
35
36
37 async def scheduler_tick():
38 # Get the task scheduler instance and print detailed debug info
39 scheduler = TaskScheduler.get()
40 # Run the scheduler tick
41 await scheduler.tick()
42
43 # Run job_loop extensions (e.g. email polling)
44 from helpers.extension import call_extensions_async
45 await call_extensions_async("job_loop")
46
47
48 def pause_loop():
49 global keep_running, pause_time
50 keep_running = False
51 pause_time = time.time()
52
53
54 def resume_loop():
55 global keep_running, pause_time
56 keep_running = True
57 pause_time = 0