GraziePrego commited on
Commit
4ae0823
·
verified ·
1 Parent(s): 102a0f7

Upload helpers/job_loop.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. helpers/job_loop.py +57 -3
helpers/job_loop.py CHANGED
@@ -1,3 +1,57 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:aadfd3159bac11951e097b9bbf269228c5303a09cc83276934ab7d66e27d48ab
3
- size 1728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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