-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
208 lines (176 loc) · 7.71 KB
/
scheduler.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# coding: utf-8
import pymongo
import datetime
from urlparse import urlparse
from celery import current_app, schedules
from celery.beat import Scheduler, ScheduleEntry
from celery.utils.log import get_logger
from celery.five import items
logger = get_logger(__name__)
debug, info, error, warning = (logger.debug, logger.info, logger.error, logger.warning)
class MongodbEntry(ScheduleEntry):
def __init__(self, *args, **kwargs):
self.enabled = kwargs.pop("enabled", True)
super(MongodbEntry, self).__init__(*args, **kwargs)
@classmethod
def loads(cls, period_task):
options = dict()
options["name"] = period_task["name"]
options["task"] = period_task["task"]
options["args"] = period_task.get("args", ())
options["kwargs"] = period_task.get("kwargs", {})
options["total_run_count"] = period_task.get("total_run_count", 0)
options["last_run_at"] = period_task.get("last_run_at", None)
options["enabled"] = period_task.get("enabled", True)
options["options"] = period_task.get("options", {})
schedule = None
if "interval" in period_task.keys():
interval = period_task["interval"]
time_delta = datetime.timedelta(**{interval["period"]: interval["every"]})
schedule = schedules.schedule(time_delta)
if "crontab" in period_task.keys():
crontab = period_task["crontab"]
schedule = schedules.crontab(**crontab)
schedule.tz = current_app.conf.get("CELERY_TIMEZONE", None)
schedule.utc_enabled = current_app.conf.get("CELERY_ENABLE_UTC", False)
options["schedule"] = schedule
options["app"] = current_app
entry = cls(**options)
return entry
def dumps(self):
data = {
'name': self.name,
'task': self.task,
'args': self.args,
'kwargs': self.kwargs,
'options': self.options,
"total_run_count": self.total_run_count,
"last_run_at": self.last_run_at,
'enabled': self.enabled
}
schedule = self.schedule_dumps(self.schedule)
data.update(schedule)
return data
@classmethod
def schedule_dumps(cls, schedule):
if isinstance(schedule, schedules.crontab):
return {"crontab": {
'minute': schedule._orig_minute,
'hour': schedule._orig_hour,
'day_of_week': schedule._orig_day_of_week,
'day_of_month': schedule._orig_day_of_month,
'month_of_year': schedule._orig_month_of_year,
}}
elif isinstance(schedule, schedules.schedule):
run_every = schedule.run_every
if run_every.days != 0:
period = "days"
every = run_every.days
else:
period = "seconds"
every = run_every.seconds
return {"interval": {
"every": every,
"period": period
}}
else:
raise ValueError("Schedule is wrong!")
def is_due(self):
if not self.enabled:
return False, 5.0 # 5 second delay for re-enable.
return self.schedule.is_due(self.last_run_at or datetime.datetime(datetime.MINYEAR, 1, 1, tzinfo=self.schedule.tz))
class MongodbScheduler(Scheduler):
Entry = MongodbEntry
def __init__(self, *args, **kwargs):
self._schedule = {}
self.mongo = self.get_mongodb()
Scheduler.__init__(self, *args, **kwargs)
@classmethod
def get_mongodb(cls):
mongo_uri = current_app.conf.get("CELERY_MONGO_URI", None)
if mongo_uri:
parsed = urlparse(mongo_uri)
host = parsed.hostname or "localhost"
port = parsed.port or 27017
db_name = parsed.path.strip('/')
username = parsed.username
password = parsed.password
else:
host = current_app.conf.get("CELERY_MONGO_HOST", "localhost")
port = current_app.conf.get("CELERY_MONGO_PORT", 27017)
db_name = current_app.conf.get("CELERY_MONGO_DBNAME", None)
username = current_app.conf.get("CELERY_MONGO_USERNAME", None)
password = current_app.conf.get("CELERY_MONGO_PASSWORD", None)
auth_source = current_app.conf.get("CELERY_MONGO_AUTH_SOURCE", "admin")
collection = current_app.conf.get("CELERY_MONGO_COLLECTION", None)
client = pymongo.MongoClient(host=host,
port=port,
authSource=auth_source,
username=username,
password=password,
tz_aware=True
)
return client[db_name][collection]
# 初始化
def setup_schedule(self):
self.install_default_entries(self.schedule)
self.merge_inplace(self.app.conf.CELERYBEAT_SCHEDULE)
def merge_inplace(self, b):
# 预配置任务计划
self.update_from_dict(self.app.conf.CELERYBEAT_SCHEDULE)
for name in self.app.conf.CELERYBEAT_SCHEDULE:
if not self.mongo.find_one({"name": name}):
entry = self.Entry(**dict(b[name], name=name, app=self.app))
self.save_entry(entry)
debug(">>>Merge:{}".format(name))
# 从字典更新
def update_from_dict(self, dict_):
for name, entry in items(dict_):
entry = self._maybe_entry(name, entry)
self.save_entry(entry)
@property
def schedule(self):
debug('Selecting tasks ...')
self.sync()
return self._schedule
# 同步数据到保存位置
def sync(self):
query = {"$or": [{'interval': {'$exists': True}}, {'crontab': {'$exists': True}}]}
mongo_schedule = {}
for period_task in self.mongo.find(query):
entry = self.Entry.loads(period_task)
mongo_schedule[entry.name] = entry
if not self._schedule:
self._schedule = mongo_schedule
debug(u">>>Return:{}".format(self._schedule))
return True
"""[0, 1] [1, 2]"""
local_schedule_keys = self._schedule.keys()
mongo_schedule_keys = mongo_schedule.keys()
for name in set(local_schedule_keys) - set(mongo_schedule_keys):
self._schedule.pop(name)
debug(u">>>Delete Name:{} - {}".format(name, self._schedule))
for name in set(mongo_schedule_keys) - set(local_schedule_keys):
self._schedule[name] = mongo_schedule[name]
debug(u">>>Add Name:{} - {}".format(name, self._schedule))
for name in set(mongo_schedule_keys) & set(local_schedule_keys):
s = self._schedule[name]
entry = mongo_schedule[name]
entry.last_run_at = s.last_run_at
entry.total_run_count = s.total_run_count
self._schedule[name] = entry
debug(u">>>Update Name:{} - {}".format(name, self._schedule))
for entry in self._schedule.values():
self.save_entry(entry)
debug(u">>>Sync:{} ".format(entry.name))
def save_entry(self, entry):
data = entry.dumps()
self.mongo.update({"name": entry.name}, {"$set": data}, True)
def close(self):
self.sync()
@property
def info(self):
return ' . db -> {self}'.format(self=self.mongo.full_name)
@property
def __repr__(self):
return "<MongodbScheduler:{}>".format(self.mongo.full_name)