-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (50 loc) · 1.83 KB
/
app.py
File metadata and controls
59 lines (50 loc) · 1.83 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
47
48
49
50
51
52
53
54
55
56
57
58
59
import logging
import time
from job_executor.adapter import datastore_api
from job_executor.adapter.fs import LocalStorageAdapter
from job_executor.common.exceptions import StartupException
from job_executor.config import environment
from job_executor.config.log import setup_logging
from job_executor.domain import rollback
from job_executor.domain.manager import Manager
logger = logging.getLogger()
setup_logging()
def initialize_app() -> Manager:
"""
Initializing the datastore by rolling back any unfinished jobs in any
datastore, and checking if any datastores have unresolved temporary backups.
Returns a manager if all datastores appear healthy.
"""
try:
rollback.fix_interrupted_jobs()
for rdn in datastore_api.get_datastores():
local_storage = LocalStorageAdapter(
datastore_api.get_datastore_directory(rdn), rdn
)
if local_storage.datastore_dir.temporary_backup_exists():
raise StartupException(f"tmp directory exists for {rdn}")
return Manager(
max_workers=environment.number_of_workers,
max_bytes_all_workers=(
environment.max_gb_all_workers * 1024**3
), # Covert from GB to bytes
)
except Exception as e:
raise StartupException("Exception when initializing") from e
def main() -> None:
manager = initialize_app()
try:
while True:
time.sleep(5)
job_query_result = datastore_api.query_for_jobs()
manager.handle_jobs(job_query_result)
except Exception as e:
raise e
finally:
manager.close_logging_thread()
if __name__ == "__main__":
logger.info("Polling for jobs...")
try:
main()
except Exception as e:
logger.exception("Service stopped by exception", exc_info=e)