def create_weekly_event(callback, day, hour, minute, callback_args=None): # relies on all the built-in checks in EventSchedule.save() schedule = EventSchedule(callback=callback, hours=set([hour]), \ days_of_week=set([day]), minutes=set([minute]), \ callback_args=callback_args ) schedule.save() return schedule
def start (self): """Configure your app in the start phase.""" if not self.bootstrapped: # initialize the forms app for registration self._form_app = self.router.get_app("form") # this tells the form app to add itself as a message handler # which registers the regex and function that this will dispatch to self._form_app.add_message_handler_to(self) formslogic = WeltelFormsLogic() formslogic.app = self # this tells the form app that this is also a form handler self._form_app.add_form_handler("weltel", formslogic) self.boostrapped = True # we could also put this in the fixture # set up automatic deregistration event try: EventSchedule.objects.get(callback="weltel.callbacks.automatic_deregistration") except EventSchedule.DoesNotExist: # automatically deregister users we haven't seen in 3 weeks set_daily_event("weltel.callbacks.automatic_deregistration", \ hour=0, minute=15, callback_args=3) # set up bi-weekly shida report try: EventSchedule.objects.get(callback="weltel.callbacks.shida_report") except EventSchedule.DoesNotExist: schedule = EventSchedule(callback="weltel.callbacks.shida_report", \ hours=set([8,15]), minutes=set([0]) ) schedule.save()
def create_daily_event(callback, hour, minute, callback_args): # relies on all the built-in checks in EventSchedule.save() schedule = EventSchedule(callback=callback, hours=set([hour]), \ minutes=set([minute]), \ callback_args=callback_args ) schedule.save() return schedule
def start(self): """Configure your app in the start phase.""" if not self.bootstrapped: # initialize the forms app for registration self._form_app = self.router.get_app("form") # this tells the form app to add itself as a message handler # which registers the regex and function that this will dispatch to self._form_app.add_message_handler_to(self) formslogic = WeltelFormsLogic() formslogic.app = self # this tells the form app that this is also a form handler self._form_app.add_form_handler("weltel", formslogic) self.boostrapped = True # set up bi-daily shida report try: EventSchedule.objects.get(callback="weltel.callbacks.shida_report") except EventSchedule.DoesNotExist: schedule = EventSchedule(callback="weltel.callbacks.shida_report", hours=set([8, 14]), minutes=set([0])) schedule.save() # set up daily inactive check try: EventSchedule.objects.get(callback="weltel.callbacks.mark_inactive") except EventSchedule.DoesNotExist: set_daily_event("weltel.callbacks.mark_inactive", hour=0, minute=15, callback_args=[3]) # set up daily 'other report' try: EventSchedule.objects.get(callback="weltel.callbacks.other_report") except EventSchedule.DoesNotExist: set_daily_event("weltel.callbacks.other_report", hour=14, minute=30, callback_args=[])
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): """ 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 setUp(self): super(testDecoratedExecution, self).setUp() global callback_counter callback_counter = 0 EventSchedule.objects.all().delete() ExecutionRecord.objects.all().delete() schedule = EventSchedule(callback="scheduler.tests.decorators.decorated_callback", \ minutes=ALL_VALUE) schedule.save() self.schedule = EventSchedule.objects.get(pk=schedule.pk)
def setUp(self): super(TestTasks, self).setUp() global callback_counter callback_counter = 0 EventSchedule.objects.all().delete() ExecutionRecord.objects.all().delete() schedule = EventSchedule(callback="scheduler.tests.tasks.callback_func", \ minutes=ALL_VALUE) schedule.save() self.schedule = EventSchedule.objects.get(pk=schedule.pk) self.assertTrue(self.schedule.last_ran is None)
def schedule_notifications (self, reg, reg_time): """schedule all future notificaitons to be sent for this patient""" for day in config.notification_days: send_at = self.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.app.send_notification", \ start_time=local_send_at, count=1, minutes='*', callback_args=[reg.patient_id, day, reg.language], description='patient %s; day %d' % (reg.patient_id, day)) schedule.save()
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.realtime.callback_func", minutes=ALL, callback_args=([3]), count=1) schedule.save() time.sleep(3.0) self.assertEquals(callback_counter,3) self.router.stop()
def load_schedules(): # TODO make this sane. # {module: {function: (hours, minutes)}} # everything is daily. # we convert from TZ time to UTC theschedule = {"logistics_project.apps.tanzania.reminders.delivery": {"first_facility": (9, 0), "second_facility": (9, 0), "third_facility": (9, 0), "first_district": (9, 0), "second_district": (9, 0), "third_district": (9, 0)}, "logistics_project.apps.tanzania.reminders.randr": {"first_facility": (9, 0), "second_facility": (9, 0), "third_facility": (9, 0), "first_district": (9, 0), "second_district": (9, 0), "third_district": (9, 0)}, "logistics_project.apps.tanzania.reminders.stockonhand": {"first": (9, 0), "second": (9, 0), "third": (9, 0)}, "logistics_project.apps.tanzania.reminders.stockonhandthankyou": {"first": (9, 0)}, "logistics_project.apps.tanzania.reminders.reports": {"delivery_summary": (9, 0), "soh_summary": (9, 0), "randr_summary": (9, 0), "email_reports": (9, 0)}, "logistics_project.apps.tanzania.reminders.test": {"test_email_admins": (9, 0)}} tanzania_tz = timezone("Africa/Dar_es_Salaam") def _to_tz_time(hours, minutes): localized = tanzania_tz.normalize(tanzania_tz.localize(datetime(2011, 1, 1, hours, minutes))) utced = localized.astimezone(pytz.utc) return (utced.hour, utced.minute) for module, funcdict in theschedule.items(): for func, (hours, minutes) in funcdict.items(): func_abspath = "%s.%s" % (module, func) hours, minutes = _to_tz_time(hours, minutes) try: schedule = EventSchedule.objects.get(callback=func_abspath) schedule.hours = [hours] schedule.minutes = [minutes] except EventSchedule.DoesNotExist: schedule = EventSchedule(callback=func_abspath, hours=[hours], minutes=[minutes]) schedule.save()
def testDefaults(self): schedule = EventSchedule(callback="foo", minutes=ALL_VALUE) schedule.save() sback = EventSchedule.objects.get(pk=schedule.pk) self.assertEqual([], sback.months) self.assertEqual([], sback.days_of_month) self.assertEqual([], sback.days_of_week) self.assertEqual([], sback.hours) self.assertEqual(["*"], sback.minutes)
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.realtime.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_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 = 'groupmessaging_main' if EventSchedule.objects.filter(description=event_desc).count() > 0: return schedule = EventSchedule(description=event_desc, \ callback="groupmessaging.utils.process_queue_callback", \ minutes=self.minutes, \ callback_args=('self.router')) schedule.save() self.log('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 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 testExecutionFailure(self): fail_schedule = EventSchedule(callback="scheduler.tests.execution.fail", \ minutes=ALL_VALUE) fail_schedule.save() self.assertEqual(0, ExecutionRecord.objects.count()) asof = datetime.utcnow() self.assertEqual(None, fail_schedule.run(asof)) fail_schedule = EventSchedule.objects.get(pk=fail_schedule.pk) self.assertEqual(asof, fail_schedule.last_ran) self.assertEqual(1, ExecutionRecord.objects.count()) self.assertEqual(1, FailedExecutionRecord.objects.count()) [rec] = FailedExecutionRecord.objects.all() self.assertEqual(fail_schedule, rec.schedule) self.assertEqual(asof, rec.runtime) self.assertEqual("FAIL.", rec.message) self.assertEqual("FAIL.", rec.message)
def testFields(self): schedule = EventSchedule(callback="foo", \ minutes=ALL_VALUE) args = ["a", 2, None] kwargs = {"foo": "bar", "4": None, "asdf": 18} schedule.callback_args = args schedule.callback_kwargs = kwargs schedule.save() sback = EventSchedule.objects.get(pk=schedule.pk) self.assertEqual(len(args), len(sback.callback_args)) for i, val in enumerate(args): self.assertEqual(sback.callback_args[i], val) self.assertEqual(len(kwargs), len(sback.callback_kwargs)) for k, val in kwargs.items(): self.assertEqual(sback.callback_kwargs[k], val)
def start (self): """Configure your app in the start phase.""" if not self.bootstrapped: # initialize the forms app for registration self._form_app = self.router.get_app("form") # this tells the form app to add itself as a message handler # which registers the regex and function that this will dispatch to self._form_app.add_message_handler_to(self) formslogic = WeltelFormsLogic() formslogic.app = self # this tells the form app that this is also a form handler self._form_app.add_form_handler("weltel", formslogic) self.boostrapped = True # set up bi-daily shida report try: EventSchedule.objects.get(callback="weltel.callbacks.shida_report") except EventSchedule.DoesNotExist: schedule = EventSchedule(callback="weltel.callbacks.shida_report", \ hours=set([8,14]), minutes=set([0]) ) schedule.save() # set up daily inactive check try: EventSchedule.objects.get(callback="weltel.callbacks.mark_inactive") except EventSchedule.DoesNotExist: set_daily_event("weltel.callbacks.mark_inactive", hour=0, minute=15, callback_args=[3]) # set up daily 'other report' try: EventSchedule.objects.get(callback="weltel.callbacks.other_report") except EventSchedule.DoesNotExist: set_daily_event("weltel.callbacks.other_report", hour=14, minute=30, callback_args=[])
def load_schedules(): # TODO make this sane. # {module: {function: (hours, minutes)}} # everything is daily. # we convert from TZ time to UTC theschedule = { "logistics_project.apps.tanzania.reminders.delivery": { "first_facility": (9, 0), "second_facility": (9, 0), "third_facility": (9, 0), "first_district": (9, 0), "second_district": (9, 0), "third_district": (9, 0) }, "logistics_project.apps.tanzania.reminders.randr": { "first_facility": (9, 0), "second_facility": (9, 0), "third_facility": (9, 0), "first_district": (9, 0), "second_district": (9, 0), "third_district": (9, 0) }, "logistics_project.apps.tanzania.reminders.stockonhand": { "first": (9, 0), "second": (9, 0), "third": (9, 0) }, "logistics_project.apps.tanzania.reminders.stockonhandthankyou": { "first": (9, 0) }, "logistics_project.apps.tanzania.reminders.reports": { "delivery_summary": (9, 0), "soh_summary": (9, 0), "randr_summary": (9, 0), "email_reports": (9, 0) }, "logistics_project.apps.tanzania.reminders.test": { "test_email_admins": (9, 0) } } tanzania_tz = timezone("Africa/Dar_es_Salaam") def _to_tz_time(hours, minutes): localized = tanzania_tz.normalize( tanzania_tz.localize(datetime(2011, 1, 1, hours, minutes))) utced = localized.astimezone(pytz.utc) return (utced.hour, utced.minute) for module, funcdict in theschedule.items(): for func, (hours, minutes) in funcdict.items(): func_abspath = "%s.%s" % (module, func) hours, minutes = _to_tz_time(hours, minutes) try: schedule = EventSchedule.objects.get(callback=func_abspath) schedule.hours = [hours] schedule.minutes = [minutes] except EventSchedule.DoesNotExist: schedule = EventSchedule(callback=func_abspath, hours=[hours], minutes=[minutes]) schedule.save()