def test_update_scheduled_task(self): with self.app.app_context(), freeze_time( "2016-01-01 00:00:00") as frozen_time: @schedulable def tester(arg): CronTest.has_run = True CronTest.received_arg = arg schedule_task(datetime(2016, 1, 1, 1, 0, 0), tester, "arg") run_scheduled_tasks() self.assertFalse(CronTest.has_run) update_scheduled_task(datetime(2016, 1, 1, 3, 20, 0), tester, "new-arg") frozen_time.tick(delta=timedelta(hours=2)) run_scheduled_tasks() self.assertFalse(CronTest.has_run) frozen_time.tick(delta=timedelta(hours=3)) run_scheduled_tasks() self.assertTrue(CronTest.has_run) self.assertEqual(CronTest.received_arg, "new-arg")
def test_scheduling_unknown_function_fails(self): with self.app.app_context(): def test_func(): pass with self.assertRaises(NotSchedulable): schedule_task(datetime.utcnow(), test_func)
def notify_patch_blacklist(new, old): """Send an email to a user if one of his entries was updated.""" # Checks if the particular update resolved the blacklist entry or just # fixes an error, for example changed the reason or price. An entry is # resolved when the end_time is before now. if 'end_time' not in new: return # Either send mail immediately, or schedule for the future item = {**old, **new} if new['end_time'] <= datetime.utcnow(): send_removed_mail(item) elif new['end_time'] != old['end_time']: schedule_task(new['end_time'], send_removed_mail, item)
def notify_new_blacklist(items): """Send an email to a user who has a new blacklist entry.""" for item in items: email = _get_email(item) fields = { 'reason': item['reason'], 'reply_to': current_app.config['BLACKLIST_REPLY_TO'] } if item['price']: fields['price'] = item['price'] / 100 # convert Rappen to CHF template = current_app.config['BLACKLIST_ADDED_EMAIL_W_PRICE'] else: template = current_app.config['BLACKLIST_ADDED_EMAIL_WO_PRICE'] mail(email, 'You have been blacklisted!', template.format(**fields)) # If the end time is already known, schedule removal mail if item['end_time'] and item['end_time'] > datetime.utcnow(): schedule_task(item['end_time'], send_removed_mail, item)
def notify_new_blacklist(items): """Send an email to a user who has a new blacklist entry.""" for item in items: email = _get_email(item) fields = { 'reason': item['reason'], 'reply_to': current_app.config['BLACKLIST_REPLY_TO'] } if item['price']: fields['price'] = item['price']/100 # convert Rappen to CHF template = current_app.config['BLACKLIST_ADDED_EMAIL_W_PRICE'] else: template = current_app.config['BLACKLIST_ADDED_EMAIL_WO_PRICE'] mail(email, 'You have been blacklisted!', template.format(**fields)) # If the end time is already known, schedule removal mail if item['end_time'] and item['end_time'] > datetime.utcnow(): schedule_task(item['end_time'], send_removed_mail, item)
def test_scheduled_function_gets_called(self): with self.app.app_context(), freeze_time( "2016-01-01 00:00:00") as frozen_time: @schedulable def scheduled_function(arg): CronTest.has_run = True CronTest.received_arg = arg schedule_task(datetime(2016, 1, 1, 1, 0, 0), scheduled_function, "arg") run_scheduled_tasks() self.assertFalse(CronTest.has_run) frozen_time.tick(delta=timedelta(hours=1)) run_scheduled_tasks() self.assertTrue(CronTest.has_run) self.assertEqual(CronTest.received_arg, "arg")