This repository was archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathschedulers.py
More file actions
161 lines (131 loc) · 5.59 KB
/
Copy pathschedulers.py
File metadata and controls
161 lines (131 loc) · 5.59 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
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
# Copyright 2013 Regents of the University of Michigan
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
import mongoengine
import traceback
import datetime
from celerybeatmongo.models import PeriodicTask
from celery.beat import Scheduler, ScheduleEntry
from celery.utils.log import get_logger
from celery import current_app
class MongoScheduleEntry(ScheduleEntry):
def __init__(self, task):
self._task = task
self.app = current_app._get_current_object()
self.name = self._task.name
self.task = self._task.task
self.schedule = self._task.schedule
self.args = self._task.args
self.kwargs = self._task.kwargs
self.options = {
'queue': self._task.queue,
'exchange': self._task.exchange,
'routing_key': self._task.routing_key,
'expires': self._task.expires,
'soft_time_limit': self._task.soft_time_limit
}
if self._task.total_run_count is None:
self._task.total_run_count = 0
self.total_run_count = self._task.total_run_count
if not self._task.last_run_at:
self._task.last_run_at = self._default_now()
self.last_run_at = self._task.last_run_at
def _default_now(self):
return self.app.now()
def next(self):
self._task.last_run_at = self.app.now()
self._task.total_run_count += 1
self._task.run_immediately = False
return self.__class__(self._task)
__next__ = next
def is_due(self):
if not self._task.enabled:
return False, 5.0 # 5 second delay for re-enable.
if hasattr(self._task, 'start_after') and self._task.start_after:
if datetime.datetime.now() < self._task.start_after:
return False, 5.0
if hasattr(self._task, 'max_run_count') and self._task.max_run_count:
if (self._task.total_run_count or 0) >= self._task.max_run_count:
return False, 5.0
if self._task.run_immediately:
# figure out when the schedule would run next anyway
_, n = self.schedule.is_due(self.last_run_at)
return True, n
return self.schedule.is_due(self.last_run_at)
def __repr__(self):
return (u'<{0} ({1} {2}(*{3}, **{4}) {{5}})>'.format(
self.__class__.__name__,
self.name, self.task, self.args,
self.kwargs, self.schedule,
))
def reserve(self, entry):
new_entry = Scheduler.reserve(self, entry)
return new_entry
def save(self):
if self.total_run_count > self._task.total_run_count:
self._task.total_run_count = self.total_run_count
if self.last_run_at and self._task.last_run_at and self.last_run_at > self._task.last_run_at:
self._task.last_run_at = self.last_run_at
self._task.run_immediately = False
try:
self._task.save(save_condition={})
except Exception:
get_logger(__name__).error(traceback.format_exc())
except mongoengine.errors.NotUniqueError:
pass
class MongoScheduler(Scheduler):
#: how often should we sync in schedule information
#: from the backend mongo database
UPDATE_INTERVAL = datetime.timedelta(seconds=5)
Entry = MongoScheduleEntry
Model = PeriodicTask
def __init__(self, *args, **kwargs):
if hasattr(current_app.conf, "CELERY_MONGODB_SCHEDULER_DB"):
db = current_app.conf.CELERY_MONGODB_SCHEDULER_DB
else:
db = "celery"
if hasattr(current_app.conf, "CELERY_MONGODB_SCHEDULER_URL"):
self._mongo = mongoengine.connect(db, host=current_app.conf.CELERY_MONGODB_SCHEDULER_URL)
get_logger(__name__).info("backend scheduler using %s/%s:%s",
current_app.conf.CELERY_MONGODB_SCHEDULER_URL,
db, self.Model._get_collection().name)
else:
self._mongo = mongoengine.connect(db)
get_logger(__name__).info("backend scheduler using %s/%s:%s",
"mongodb://localhost",
db, self.Model._get_collection().name)
self._schedule = {}
self._last_updated = None
Scheduler.__init__(self, *args, **kwargs)
self.max_interval = (kwargs.get('max_interval')
or self.app.conf.CELERYBEAT_MAX_LOOP_INTERVAL or 5)
def setup_schedule(self):
pass
def requires_update(self):
"""check whether we should pull an updated schedule
from the backend database"""
if not self._last_updated:
return True
return self._last_updated + self.UPDATE_INTERVAL < datetime.datetime.now()
def get_from_database(self):
self.sync()
d = {}
for doc in self.Model.objects():
d[doc.name] = self.Entry(doc)
return d
@property
def schedule(self):
if self.requires_update():
self._schedule = self.get_from_database()
self._last_updated = datetime.datetime.now()
return self._schedule
def sync(self):
for entry in self._schedule.values():
try:
entry.kwargs.update({
'date_ref': datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
})
entry.save()
except mongoengine.errors.NotUniqueError:
pass