-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (49 loc) · 1.38 KB
/
main.py
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
60
61
62
63
import json
import logging
import os
import sys
import container_scheduler
from dotenv import load_dotenv
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(levelname)-8s [%(name)s] %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger("docker_container_scheduler")
schedules: list = list()
"""
CONTAINER_SCHEDULES environment variable structure
[
{
"container": "container_name_1",
"crontab": "*/1 * * * *"
}
]
"""
def load_environment() -> bool:
global schedules
logger.info("Loading schedules from environment")
env_schedules = os.environ.get('CONTAINER_SCHEDULES')
if env_schedules is None:
logger.error("No schedules found in the environment")
return False
try:
schedules = json.loads(env_schedules)
except Exception as e:
logger.error("The scheduler configuration is not a valid json format", e)
return False
if type(schedules) is not list:
logger.error("The scheduler configuration is not valid")
return False
if len(schedules) == 0:
logger.error("The scheduler configuration doesn't contain any schedule")
return False
return True
if __name__ == '__main__':
load_dotenv()
if not load_environment():
exit(1)
if not container_scheduler.start(schedules):
exit(1)