fix: synchronize job loop with development instance

Rafael Uzarowski committed May 27, 2025 at 17:32 UTC 64aa0a1f7ee3c052472a0acdd10782fa52e76c1a
1 file changed +39 -6
python/helpers/job_loop.py
+39 -6
@@ -1,20 +1,53 @@
1 import asyncio
2 +from datetime import datetime
3 +import time
4 from python.helpers.task_scheduler import TaskScheduler
5 from python.helpers.print_style import PrintStyle
6 from python.helpers import errors
7 +from python.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:
9 - try:
10 - await scheduler_tick()
11 - except Exception as e:
12 - PrintStyle().error(errors.format_error(e))
13 - await asyncio.sleep(60) # TODO! - if we lower it under 1min, it can run a 5min job multiple times in it's target minute
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
20 - await scheduler.tick()
\ No newline at end of file
41 + await scheduler.tick()
42 +
43 +
44 +def pause_loop():
45 + global keep_running, pause_time
46 + keep_running = False
47 + pause_time = time.time()
48 +
49 +
50 +def resume_loop():
51 + global keep_running, pause_time
52 + keep_running = True
53 + pause_time = 0