Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
 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))
Ejemplo n.º 4
0
 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()
Ejemplo n.º 5
0
 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))
Ejemplo n.º 6
0
 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()
Ejemplo n.º 7
0
 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()
Ejemplo n.º 8
0
 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()
Ejemplo n.º 9
0
 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))
Ejemplo n.º 10
0
 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))
Ejemplo n.º 11
0
    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=[])
Ejemplo n.º 12
0
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()