forked from mining/mining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.py
More file actions
executable file
·122 lines (101 loc) · 3.87 KB
/
Copy pathscheduler.py
File metadata and controls
executable file
·122 lines (101 loc) · 3.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os import sys, path
import schedule
from time import sleep
from bottle.ext.mongo import MongoPlugin
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from mining.bin.cube import run
from mining.utils import conf, log_it
log_it("START", "bin-scheduler")
onrun = {}
register = []
def job(slug):
log_it("START JOB: {}".format(slug), "bin-scheduler")
run(slug)
log_it("END JOB: {}".format(slug), "bin-scheduler")
def rules(cube, scheduler_type='minutes', scheduler_interval=59,
dashboard=None):
if scheduler_type:
scheduler_type = cube.get('scheduler_type', 'minutes')
if scheduler_interval:
scheduler_interval = cube.get('scheduler_interval', 59)
log_it("START REGISTER", "bin-scheduler")
log_it("cube: {}".format(cube.get('slug')), "bin-scheduler")
log_it("type: {}".format(scheduler_type), "bin-scheduler")
log_it("interval: {}".format(scheduler_interval), "bin-scheduler")
log_it("END REGISTER", "bin-scheduler")
t = {}
if scheduler_type == 'minutes':
env = schedule.every(int(scheduler_interval))
t = env.minutes
elif scheduler_type == 'hour':
env = schedule.every()
t = env.hour
elif scheduler_type == 'day':
env = schedule.every()
t = env.day
else:
return False
jobn = cube.get("slug")
try:
t.do(job, slug=cube.get('slug'))
if dashboard:
jobn = u"{}-{}".format(cube.get("slug"), dashboard)
onrun[jobn] = env
register.append(jobn)
if cube.get('run') != 'run':
run(cube.get('slug'))
except Exception, e:
if jobn in register:
register.remove(jobn)
if onrun.get(jobn):
del onrun[jobn]
log_it("ERROR {}: {}".format(cube.get('slug'), e))
return True
mongo = MongoPlugin(
uri=conf("mongodb")["uri"],
db=conf("mongodb")["db"],
json_mongo=True).get_mongo()
for cube in mongo['cube'].find({'scheduler_status': True}):
rules(cube)
for dashboard in mongo['dashboard'].find({'scheduler_status': True}):
elements = [e['id'] for e in dashboard['element']]
for e in elements:
element = mongo['element'].find_one({'slug': e})
cube = mongo['cube'].find_one({'slug': element['cube']})
rules(cube, dashboard['scheduler_type'],
dashboard['scheduler_interval'])
while True:
for cube in mongo['cube'].find({'scheduler_status': True}):
if cube['slug'] not in register:
rules(cube)
for dashboard in mongo['dashboard'].find({'scheduler_status': True}):
elements = [e['id'] for e in dashboard['element']]
for e in elements:
element = mongo['element'].find_one({'slug': e})
cube = mongo['cube'].find_one({'slug': element['cube']})
if cube['slug'] not in register:
rules(cube, dashboard['scheduler_type'],
dashboard['scheduler_interval'],
dashboard['slug'])
for cube in mongo['cube'].find({'scheduler_status': False}):
if cube['slug'] in register:
schedule.cancel_job(onrun[cube['slug']])
del onrun[cube['slug']]
register.remove(cube['slug'])
for dashboard in mongo['dashboard'].find({'scheduler_status': False}):
elements = [e['id'] for e in dashboard['element']]
for e in elements:
try:
element = mongo['element'].find_one({'slug': e})
cube = mongo['cube'].find_one({'slug': element['cube']})
jobn = u"{}-{}".format(cube['slug'], dashboard['slug'])
if jobn in register:
schedule.cancel_job(onrun[jobn])
del onrun[jobn]
register.remove(jobn)
except:
pass
schedule.run_pending()
sleep(1)