def start(self): try: schedule = EventSchedule.objects.get(description=SCHEDULE_DESC) except EventSchedule.DoesNotExist: schedule = EventSchedule(description=SCHEDULE_DESC) schedule.callback = CALLBACK schedule.minutes = '*' schedule.save() self.info('started')
def test_one_shot(self): """ Test scheduler in real time""" global callback_counter self.router.start() schedule = EventSchedule(callback="scheduler.tests.slow.callback_func", minutes=ALL, callback_args=([3])) schedule.save() time.sleep(180.0) self.assertEquals(callback_counter,9) self.router.stop()
def test_one_shot(self): """ Test scheduler in real time""" global callback_counter self.router.start() schedule = EventSchedule(callback="scheduler.tests.slow.callback_func", minutes=ALL, callback_args=([3])) schedule.save() time.sleep(180.0) self.assertEquals(callback_counter, 9) self.router.stop()
def test_recurring(self): """ Test regular recurring schedules """ global callback_counter self.router.start() self.router.get_app('scheduler').schedule_thread._debug_speedup(minutes=1) schedule = EventSchedule(callback="scheduler.tests.speedup.callback_func", minutes=ALL, callback_args=([3])) schedule.save() time.sleep(2.9) self.assertEquals(callback_counter,9) self.router.stop()
def test_one_shot(self): """ Basically test 'count'""" global callback_counter self.router.start() # speedup the scheduler so that 1 second == 1 minute self.router.get_app('scheduler').schedule_thread._debug_speedup(minutes=1) schedule = EventSchedule(callback="scheduler.tests.speedup.callback_func", minutes=ALL, callback_args=([3]), count=1) schedule.save() time.sleep(3.0) self.assertEquals(callback_counter,3) self.router.stop()
def schedule_notifications (reg, reg_time): """schedule all future notificaitons to be sent for this patient""" for day in config.notification_days: send_at = calc_send_time(reg_time, reg.contact_time, day) #scheduler works in local server time instead of UTC local_send_at = send_at.astimezone(pytz.timezone(config.server_tz)).replace(tzinfo=None) schedule = EventSchedule(callback="circumcision.apps.circumcision.schedule.send_notification", \ start_time=local_send_at, count=1, minutes='*', callback_args=[reg.pk, day], description='patient %s; day %d' % (reg.patient_id, day)) schedule.save()
def test_recurring(self): """ Test regular recurring schedules """ global callback_counter self.router.start() self.router.get_app('scheduler').schedule_thread._debug_speedup( minutes=1) schedule = EventSchedule( callback="scheduler.tests.speedup.callback_func", minutes=ALL, callback_args=([3])) schedule.save() time.sleep(2.9) self.assertEquals(callback_counter, 9) self.router.stop()
def start(self): # create a scheduled event if not present # due to bug in fixture handling in scheduler event_desc = 'group_messaging_main' if EventSchedule.objects.filter(description=event_desc).count() > 0: return schedule = EventSchedule(description=event_desc, \ callback="group_messaging.utils.process_queue_callback", \ minutes=([00,58]), \ callback_args=('self.router')) schedule.save() self.debug(u"Created Event Schedule %s" % event_desc)
def test_timestart_timestop(self): """ Test timebound schedules """ global callback_counter self.router.start() self.router.get_app('scheduler').schedule_thread._debug_speedup(minutes=1) start_time = datetime.now() + timedelta(minutes=2) end_time = datetime.now() + timedelta(minutes=4) schedule = EventSchedule(callback="scheduler.tests.speedup.callback_func", minutes=ALL, callback_args=([3]), \ start_time = start_time, end_time = end_time) schedule.save() time.sleep(7.0) self.assertEquals(callback_counter,6) self.router.stop()
def schedule_response_reminders(self, d): self.debug('in App.schedulecallback(): self.router: %s', self.router) cb = d.pop('callback') m = d.pop('minutes') reps = d.pop('repetitions') self.debug('callback:%s; minutes:%s; repetitions:%s; kwargs:%s',cb,m,reps,d) t = datetime.now() s = timedelta(minutes=m) # for n in [(t + 1*s), (t + 2*s), ... (t + r+s)], where r goes from [1, reps+1) for st in [t + r*s for r in range(1,reps+1)]: self.debug('scheduling a reminder to fire after %s', st) schedule = EventSchedule(callback=cb, minutes=ALL, callback_kwargs=d, start_time=st, count=1) schedule.save()
def test_timestart_timestop(self): """ Test timebound schedules """ global callback_counter self.router.start() self.router.get_app('scheduler').schedule_thread._debug_speedup( minutes=1) start_time = datetime.now() + timedelta(minutes=2) end_time = datetime.now() + timedelta(minutes=4) schedule = EventSchedule(callback="scheduler.tests.speedup.callback_func", minutes=ALL, callback_args=([3]), \ start_time = start_time, end_time = end_time) schedule.save() time.sleep(7.0) self.assertEquals(callback_counter, 6) self.router.stop()
def test_one_shot(self): """ Basically test 'count'""" global callback_counter self.router.start() # speedup the scheduler so that 1 second == 1 minute self.router.get_app('scheduler').schedule_thread._debug_speedup( minutes=1) schedule = EventSchedule( callback="scheduler.tests.speedup.callback_func", minutes=ALL, callback_args=([3]), count=1) schedule.save() time.sleep(3.0) self.assertEquals(callback_counter, 3) self.router.stop()
def schedule_response_reminder(self, d): self.debug("in App.schedulecallback(): self.router: %s", self.router) cb = d.pop("callback") m = d.pop("minutes") reps = d.pop("repetitions") self.debug("callback:%s; minutes:%s; repetitions:%s; kwargs:%s", cb, m, reps, d) t = datetime.now() s = timedelta(minutes=m) # for n in [(t + 1*s), (t + 2*s), ... (t + r+s)], where r goes from [1, reps+1) # for st in [t + r*s for r in range(1,reps+1)]: # MLL: Changed to do one at a time, so resend will schedule the next one schedule = EventSchedule(callback=cb, minutes=ALL, callback_kwargs=d, start_time=t + s, count=1) schedule.save() self.debug("scheduling a reminder to fire after %s at %s, id=%d", s, s + t, schedule.id)
def test_hours(self): schedule = EventSchedule(callback="foo", hours=set([3,5,23]), \ minutes='*') self.assertFalse(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + 3 * hour)) self.assertFalse(schedule.should_fire(start + 4 * hour)) self.assertTrue(schedule.should_fire(start + 5 * hour)) self.assertFalse(schedule.should_fire(start + 6 * hour)) self.assertTrue(schedule.should_fire(start + 23 * hour))
def LoadSchedules(): count = 0 callbacks = ['ilsgateway.callbacks.run_reminders'] # 'ilsgateway.callbacks.district_randr_reminder', # 'ilsgateway.callbacks.facility_delivery_reminder', # 'ilsgateway.callbacks.district_delivery_reminder', # 'ilsgateway.callbacks.district_delinquent_deliveries_summary', # 'ilsgateway.callbacks.facility_soh_reminder',] for callback in callbacks: if not EventSchedule.objects.filter(callback=callback): e = EventSchedule(callback=callback, description=callback, days_of_month='*', hours='*', minutes=[0,15,30,45]) e.save() count = count + 1 print "Loaded %d EventSchedules" % count
def test_all(self): schedule = EventSchedule(callback="foo", \ minutes=ALL) self.assertTrue(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + sec)) self.assertTrue(schedule.should_fire(start + min)) self.assertTrue(schedule.should_fire(start + hour)) self.assertTrue(schedule.should_fire(start + day)) self.assertTrue(schedule.should_fire(start + week)) self.assertTrue( schedule.should_fire(start + sec + min + hour + day + week))
def test_hours(self): schedule = EventSchedule(callback="foo", hours=set([3, 5, 23]), minutes="*") self.assertFalse(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + 3 * hour)) self.assertFalse(schedule.should_fire(start + 4 * hour)) self.assertTrue(schedule.should_fire(start + 5 * hour)) self.assertFalse(schedule.should_fire(start + 6 * hour)) self.assertTrue(schedule.should_fire(start + 23 * hour))
def test_all(self): schedule = EventSchedule(callback="foo", minutes=ALL) self.assertTrue(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + sec)) self.assertTrue(schedule.should_fire(start + min)) self.assertTrue(schedule.should_fire(start + hour)) self.assertTrue(schedule.should_fire(start + day)) self.assertTrue(schedule.should_fire(start + week)) self.assertTrue(schedule.should_fire(start + sec + min + hour + day + week))
def test_month(self): """ Fire event every minute in February Note: months are indexed from '1' """ schedule = EventSchedule(callback="foo", months=set([2]), days_of_month="*", hours="*", minutes="*") self.assertFalse(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + month)) self.assertTrue(schedule.should_fire(start + month + sec)) self.assertTrue(schedule.should_fire(start + month + min)) self.assertTrue(schedule.should_fire(start + month + hour)) self.assertTrue(schedule.should_fire(start + month + day)) self.assertTrue(schedule.should_fire(start + month + sec + min + hour + day)) self.assertFalse(schedule.should_fire(start + 2 * month)) self.assertFalse(schedule.should_fire(start + 2 * month + sec)) self.assertFalse(schedule.should_fire(start + 2 * month + min)) self.assertFalse(schedule.should_fire(start + 2 * month + hour)) self.assertFalse(schedule.should_fire(start + 2 * month + day))
def test_minutes(self): schedule = EventSchedule(callback="foo", minutes=set([1, 3, 5]), count=1) self.assertFalse(schedule.should_fire(start)) self.assertFalse(schedule.should_fire(start + sec)) self.assertTrue(schedule.should_fire(start + 1 * min)) self.assertFalse(schedule.should_fire(start + 2 * min)) self.assertTrue(schedule.should_fire(start + 3 * min)) self.assertFalse(schedule.should_fire(start + 4 * min)) self.assertTrue(schedule.should_fire(start + 5 * min)) self.assertFalse(schedule.should_fire(start + hour)) self.assertFalse(schedule.should_fire(start + day)) self.assertFalse(schedule.should_fire(start + week))
def test_days_of_month(self): """ Fire event on the 1st, 15th, and 30th of the month at 10:00 am Note: days of month are indexed starting '1' """ schedule = EventSchedule(callback="foo", days_of_month=set([1,15,30]), hours=set([10]), \ minutes=set([0])) self.assertFalse(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + 10 * hour)) self.assertFalse(schedule.should_fire(start + 10 * hour + min)) self.assertFalse(schedule.should_fire(start + 10 * min)) self.assertFalse(schedule.should_fire(start + day + 10 * hour)) self.assertTrue(schedule.should_fire(start + 14 * day + 10 * hour)) self.assertFalse(schedule.should_fire(start + 14 * day + 9 * hour)) self.assertFalse(schedule.should_fire(start + 14 * day + 11 * hour)) self.assertFalse(schedule.should_fire(start + 13 * day + 10 * hour)) self.assertFalse(schedule.should_fire(start + 15 * day + 10 * hour)) self.assertTrue(schedule.should_fire(start + 29 * day + 10 * hour)) self.assertFalse(schedule.should_fire(start + 29 * day + 1 * min)) self.assertFalse(schedule.should_fire(start + 29 * day - 1 * min)) self.assertFalse(schedule.should_fire(start + 28 * day + 10 * hour))
def test_days_of_week(self): """ fire event monday, friday, and sunday, at 8:15 am and 5:15 pm Note: days of week are indexed starting '0' """ schedule = EventSchedule(callback="foo", days_of_week=set([0,4,6]), hours=set([8,17]), \ minutes=set([15])) self.assertFalse(schedule.should_fire(start)) self.assertFalse(schedule.should_fire(start + 5 * day)) self.assertFalse(schedule.should_fire(start + 7 * day)) self.assertFalse(schedule.should_fire(start + 3 * hour)) self.assertFalse(schedule.should_fire(start + 5 * day + 7 * hour)) self.assertFalse(schedule.should_fire(start + 7 * day + 16 * hour)) self.assertFalse(schedule.should_fire(start + 8 * hour + 13 * min)) self.assertFalse( schedule.should_fire(start + 5 * day + 8 * hour + 16 * min)) self.assertFalse( schedule.should_fire(start + 7 * day + 8 * hour + 25 * min)) self.assertFalse( schedule.should_fire(start + 3 * day + 8 * hour + 15 * min)) self.assertFalse( schedule.should_fire(start + 5 * day + 17 * hour + 15 * min)) self.assertTrue(schedule.should_fire(start + 8 * hour + 15 * min)) self.assertTrue(schedule.should_fire(start + 17 * hour + 15 * min)) self.assertTrue( schedule.should_fire(start + 4 * day + 8 * hour + 15 * min)) self.assertTrue( schedule.should_fire(start + 6 * day + 17 * hour + 15 * min))
def test_days_of_week(self): """ fire event monday, friday, and sunday, at 8:15 am and 5:15 pm Note: days of week are indexed starting '0' """ schedule = EventSchedule(callback="foo", days_of_week=set([0, 4, 6]), hours=set([8, 17]), minutes=set([15])) self.assertFalse(schedule.should_fire(start)) self.assertFalse(schedule.should_fire(start + 5 * day)) self.assertFalse(schedule.should_fire(start + 7 * day)) self.assertFalse(schedule.should_fire(start + 3 * hour)) self.assertFalse(schedule.should_fire(start + 5 * day + 7 * hour)) self.assertFalse(schedule.should_fire(start + 7 * day + 16 * hour)) self.assertFalse(schedule.should_fire(start + 8 * hour + 13 * min)) self.assertFalse(schedule.should_fire(start + 5 * day + 8 * hour + 16 * min)) self.assertFalse(schedule.should_fire(start + 7 * day + 8 * hour + 25 * min)) self.assertFalse(schedule.should_fire(start + 3 * day + 8 * hour + 15 * min)) self.assertFalse(schedule.should_fire(start + 5 * day + 17 * hour + 15 * min)) self.assertTrue(schedule.should_fire(start + 8 * hour + 15 * min)) self.assertTrue(schedule.should_fire(start + 17 * hour + 15 * min)) self.assertTrue(schedule.should_fire(start + 4 * day + 8 * hour + 15 * min)) self.assertTrue(schedule.should_fire(start + 6 * day + 17 * hour + 15 * min))
def test_minutes(self): schedule = EventSchedule(callback="foo", \ minutes=set([1,3,5]), count=1) self.assertFalse(schedule.should_fire(start)) self.assertFalse(schedule.should_fire(start + sec)) self.assertTrue(schedule.should_fire(start + 1 * min)) self.assertFalse(schedule.should_fire(start + 2 * min)) self.assertTrue(schedule.should_fire(start + 3 * min)) self.assertFalse(schedule.should_fire(start + 4 * min)) self.assertTrue(schedule.should_fire(start + 5 * min)) self.assertFalse(schedule.should_fire(start + hour)) self.assertFalse(schedule.should_fire(start + day)) self.assertFalse(schedule.should_fire(start + week))
def test_month(self): """ Fire event every minute in February Note: months are indexed from '1' """ schedule = EventSchedule(callback="foo", months=set([2]), \ days_of_month='*', hours='*', \ minutes='*') self.assertFalse(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + month)) self.assertTrue(schedule.should_fire(start + month + sec)) self.assertTrue(schedule.should_fire(start + month + min)) self.assertTrue(schedule.should_fire(start + month + hour)) self.assertTrue(schedule.should_fire(start + month + day)) self.assertTrue( schedule.should_fire(start + month + sec + min + hour + day)) self.assertFalse(schedule.should_fire(start + 2 * month)) self.assertFalse(schedule.should_fire(start + 2 * month + sec)) self.assertFalse(schedule.should_fire(start + 2 * month + min)) self.assertFalse(schedule.should_fire(start + 2 * month + hour)) self.assertFalse(schedule.should_fire(start + 2 * month + day))
def test_days_of_month(self): """ Fire event on the 1st, 15th, and 30th of the month at 10:00 am Note: days of month are indexed starting '1' """ schedule = EventSchedule(callback="foo", days_of_month=set([1, 15, 30]), hours=set([10]), minutes=set([0])) self.assertFalse(schedule.should_fire(start)) self.assertTrue(schedule.should_fire(start + 10 * hour)) self.assertFalse(schedule.should_fire(start + 10 * hour + min)) self.assertFalse(schedule.should_fire(start + 10 * min)) self.assertFalse(schedule.should_fire(start + day + 10 * hour)) self.assertTrue(schedule.should_fire(start + 14 * day + 10 * hour)) self.assertFalse(schedule.should_fire(start + 14 * day + 9 * hour)) self.assertFalse(schedule.should_fire(start + 14 * day + 11 * hour)) self.assertFalse(schedule.should_fire(start + 13 * day + 10 * hour)) self.assertFalse(schedule.should_fire(start + 15 * day + 10 * hour)) self.assertTrue(schedule.should_fire(start + 29 * day + 10 * hour)) self.assertFalse(schedule.should_fire(start + 29 * day + 1 * min)) self.assertFalse(schedule.should_fire(start + 29 * day - 1 * min)) self.assertFalse(schedule.should_fire(start + 28 * day + 10 * hour))