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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
# This file is part of eventmq.
#
# eventmq is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# eventmq is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with eventmq. If not, see <http://www.gnu.org/licenses/>.
"""
:mod:`scheduler` -- Scheduler
=============================
Handles cron and other scheduled tasks
"""
from hashlib import sha1 as emq_hash
import json
from json import dumps as serialize
from json import loads as deserialize
import logging
from croniter import croniter
import redis
from six import next
from eventmq.log import setup_logger
from . import __version__
from . import conf, constants
from .client.messages import send_request
from .constants import KBYE
from .poller import Poller, POLLIN
from .sender import Sender
from .utils.classes import EMQPService, HeartbeatMixin
from .utils.messages import send_emqp_message as sendmsg
from .utils.settings import import_settings
from .utils.timeutils import IntervalIter, monotonic, seconds_until, timestamp
logger = logging.getLogger(__name__)
INFINITE_RUN_COUNT = -1
class Scheduler(HeartbeatMixin, EMQPService):
"""
Keeper of time, master of schedules
"""
SERVICE_TYPE = constants.CLIENT_TYPE.scheduler
def __init__(self, *args, **kwargs):
self.name = kwargs.get('name', None)
logger.info('EventMQ Version {}'.format(__version__))
logger.info('Initializing Scheduler...')
import_settings()
super(Scheduler, self).__init__(*args, **kwargs)
self.outgoing = Sender()
self._redis_server = None
# contains dict of 4-item lists representing cron jobs key of this
# dictionary is a hash of arguments, path, and callable from the
# message of the SCHEDULE command received
# IDX Description
# 0 = the next ts this job should be executed in
# 1 = the function to be executed
# 2 = the croniter iterator for this job
# 3 = the queue to execute the job in
self.cron_jobs = {}
# contains dict of 5-item lists representing jobs based on an interval
# key of this dictionary is a hash of arguments, path, and callable
# from the message of the SCHEDULE command received
# values of this list follow this format:
# IDX Descriptions
# 0 = the next (monotonic) ts that this job should be executed in
# 1 = the function to be executed
# 2 = the interval iter for this job
# 3 = the queue to execute the job in
# 4 = run_count: # of times to execute this job
self.interval_jobs = {}
self.poller = Poller()
self.load_jobs()
self._setup()
def load_jobs(self):
"""
Loads the jobs that need to be scheduled
"""
try:
interval_job_list = self.redis_server.lrange(
'interval_jobs', 0, -1)
if interval_job_list is not None:
for i in interval_job_list:
logger.debug('Restoring job with hash %s' % i)
if (self.redis_server.get(i)):
self.load_job_from_redis(
message=deserialize(self.redis_server.get(i)))
else:
logger.warning('Expected scheduled job in redis,' +
'but none was found with hash %s' % i)
except redis.ConnectionError:
logger.warning('Could not contact redis server')
except Exception as e:
logger.warning(str(e))
def _start_event_loop(self):
"""
Starts the actual event loop. Usually called by :meth:`Scheduler.start`
"""
while True:
if self.received_disconnect:
break
ts_now = int(timestamp())
m_now = monotonic()
events = self.poller.poll()
if events.get(self.outgoing) == POLLIN:
msg = self.outgoing.recv_multipart()
self.process_message(msg)
# TODO: distribute me!
for hash_, cron in self.cron_jobs.items():
# If the time is now, or passed
if cron[0] <= ts_now:
msg = cron[1]
queue = cron[3]
# Run the msg
logger.debug("Time is: %s; Schedule is: %s - Running %s"
% (ts_now, cron[0], msg))
self.send_request(msg, queue=queue)
# Update the next time to run
cron[0] = next(cron[2])
logger.debug("Next execution will be in %ss" %
seconds_until(cron[0]))
cancel_jobs = []
for k, v in self.interval_jobs.iteritems():
# TODO: Refactor this entire loop to be readable by humankind
# The schedule time has elapsed
if v[0] <= m_now:
msg = v[1]
queue = v[3]
logger.debug("Time is: {}; Schedule is: {} - Running {} "
"({})".format(m_now, v[0], k, msg))
# v[4] is the current remaining run_count
if v[4] != INFINITE_RUN_COUNT:
# If run_count was 0, we cancel the job
if v[4] <= 0:
cancel_jobs.append(k)
else:
# Decrement run_count
v[4] -= 1
if v[4] > 0:
# Update the next run time
v[0] = next(v[2])
# Persist the change to redis if there are run
# counts still left
try:
message = deserialize(
self.redis_server.get(k))
new_headers = []
for header in message[1].split(','):
if 'run_count:' in header:
new_headers.append(
'run_count:{}'.format(v[4]))
else:
new_headers.append(header)
message[1] = ",".join(new_headers)
self.redis_server.set(
k, serialize(message))
except Exception as e:
logger.warning(
'Unable to update key in redis '
'server: {}'.format(e))
else:
cancel_jobs.append(k)
# Perform the job since run_count was still > 0
self.send_request(msg, queue=queue)
else:
# Scheduled job is in running infinitely
# Send job and update next schedule time
self.send_request(msg, queue=queue)
v[0] = next(v[2])
for job_id in cancel_jobs:
self.cancel_job(job_id)
if not self.maybe_send_heartbeat(events):
break
@property
def redis_server(self):
# Open connection to redis server for persistance
if self._redis_server is None:
try:
self._redis_server = \
redis.StrictRedis(host=conf.RQ_HOST,
port=conf.RQ_PORT,
db=conf.RQ_DB,
password=conf.RQ_PASSWORD)
return self._redis_server
except Exception as e:
logger.warning('Unable to connect to redis server: {}'.format(
e))
else:
return self._redis_server
def send_request(self, jobmsg, queue=None):
"""
Send a request message to the broker
Args:
jobmsg: The message to send to the broker
queue: The name of the queue to use_impersonation
Returns:
str: ID of the message
"""
jobmsg = json.loads(jobmsg)
msgid = send_request(self.outgoing, jobmsg, queue=queue,
reply_requested=True)
return msgid
def on_disconnect(self, msgid, message):
logger.info("Received DISCONNECT request: {}".format(message))
self._redis_server.connection_pool.disconnect()
sendmsg(self.outgoing, KBYE)
self.outgoing.unbind(conf.SCHEDULER_ADDR)
super(Scheduler, self).on_disconnect(msgid, message)
def on_kbye(self, msgid, msg):
if not self.is_heartbeat_enabled:
self.reset()
def on_unschedule(self, msgid, message):
"""
Unschedule an existing schedule job, if it exists
"""
logger.info("Received new UNSCHEDULE request: {}".format(message))
schedule_hash = self.schedule_hash(message)
# TODO: Notify router whether or not this succeeds
self.cancel_job(schedule_hash)
def cancel_job(self, schedule_hash):
"""
Cancels a job if it exists
Args:
schedule_hash (str): The schedule's unique hash. See
:meth:`Scheduler.schedule_hash`
"""
if schedule_hash in self.interval_jobs:
# If the hash wasn't found in either `cron_jobs` or `interval_jobs`
# then it's safe to assume it's already deleted.
try:
del self.interval_jobs[schedule_hash]
except KeyError:
pass
try:
del self.cron_jobs[schedule_hash]
except KeyError:
pass
# Double check the redis server even if we didn't find the hash
# in memory
try:
if (self.redis_server.get(schedule_hash)):
self.redis_server.delete(schedule_hash)
self.redis_server.lrem('interval_jobs', 0, schedule_hash)
self.redis_server.save()
except redis.ConnectionError as e:
logger.warning('Could not contact redis server: {}'.format(e))
except Exception as e:
logger.warning(str(e), exc_info=True)
def load_job_from_redis(self, message):
"""
"""
from .utils.timeutils import IntervalIter
queue = message[0].encode('utf-8')
headers = message[1]
interval = int(message[2])
inter_iter = IntervalIter(monotonic(), interval)
schedule_hash = self.schedule_hash(message)
cron = message[4] if interval == -1 else ""
ts = int(timestamp())
# Positive intervals are valid
if interval >= 0:
self.interval_jobs[schedule_hash] = [
next(inter_iter),
message[3],
inter_iter,
queue,
self.get_run_count_from_headers(headers)
]
# Non empty strings are valid
# Expecting '* * * * *' etc.
elif cron and cron != "":
# Create the croniter iterator
c = croniter(cron)
# Get the next time this job should be run
c_next = next(c)
if ts >= c_next:
# If the next execution time has passed move the iterator to
# the following time
c_next = next(c)
self.cron_jobs[schedule_hash] = [c_next, message[3], c, queue]
def on_schedule(self, msgid, message):
"""
"""
logger.info("Received new SCHEDULE request: {}".format(message))
queue = message[0]
headers = message[1]
interval = int(message[2])
cron = str(message[4])
run_count = self.get_run_count_from_headers(headers)
schedule_hash = self.schedule_hash(message)
# Notify if this is updating existing, or new
if (schedule_hash in self.cron_jobs or
schedule_hash in self.interval_jobs):
logger.debug('Update existing scheduled job with %s'
% schedule_hash)
else:
logger.debug('Creating a new scheduled job with %s'
% schedule_hash)
# If interval is negative, cron MUST be populated
if interval >= 0:
inter_iter = IntervalIter(monotonic(), interval)
self.interval_jobs[schedule_hash] = [
next(inter_iter),
message[3],
inter_iter,
queue,
run_count
]
if schedule_hash in self.cron_jobs:
self.cron_jobs.pop(schedule_hash)
else:
ts = int(timestamp())
c = croniter(cron)
c_next = next(c)
if ts >= c_next:
# If the next execution time has passed move the iterator to
# the following time
c_next = next(c)
self.cron_jobs[schedule_hash] = [
c_next, message[3], c, None]
if schedule_hash in self.interval_jobs:
self.interval_jobs.pop(schedule_hash)
# Persist the scheduled job
try:
if schedule_hash not in self.redis_server.lrange(
'interval_jobs', 0, -1):
self.redis_server.lpush('interval_jobs', schedule_hash)
self.redis_server.set(schedule_hash, serialize(message))
self.redis_server.save()
logger.debug('Saved job {} with hash {} to redis'.format(
message, schedule_hash))
except redis.ConnectionError:
logger.warning('Could not contact redis server. Unable to '
'guarantee persistence.')
except Exception as e:
logger.warning(str(e))
# Send a request in haste mode, decrement run_count if needed
if 'nohaste' not in headers:
if run_count > 0 or run_count == INFINITE_RUN_COUNT:
# Don't allow run_count to decrement below 0
if run_count > 0:
self.interval_jobs[schedule_hash][4] -= 1
self.send_request(message[3], queue=queue)
def get_run_count_from_headers(self, headers):
run_count = INFINITE_RUN_COUNT
for header in headers.split(','):
if 'run_count:' in header:
run_count = int(header.split(':')[1])
return run_count
def on_heartbeat(self, msgid, message):
"""
Noop command. The logic for heartbeating is in the event loop.
"""
@classmethod
def schedule_hash(cls, message):
"""
Create a unique identifier for this message for storing
and referencing later
Args:
message (str): The serialized message passed to the scheduler
Returns:
int: unique hash for the job
"""
# Get the job portion of the message
msg = deserialize(message[3])[1]
# Use json to create the hash string, sorting the keys.
schedule_hash_items = json.dumps(
{'args': msg['args'],
'kwargs': msg['kwargs'],
'class_args': msg['class_args'],
'class_kwargs': msg['class_kwargs'],
'path': msg['path'],
'callable': msg['callable']},
sort_keys=True)
# Hash the sorted, immutable set of items in our identifying dict
schedule_hash = emq_hash(
schedule_hash_items.encode('utf-8')).hexdigest()
return schedule_hash
def scheduler_main(self):
"""
Kick off scheduler with logging and settings import
"""
setup_logger("eventmq")
import_settings()
self.__init__()
self.start(addr=conf.SCHEDULER_ADDR)
# Entry point for pip console scripts
def scheduler_main():
s = Scheduler()
s.scheduler_main()
def test_job(*args, **kwargs):
"""
Simple test job for use with the scheduler
"""
print("hello!") # noqa
|