def test_schedule_tomorrow_polite(): now = datetime.datetime.now().replace(tzinfo=datetime.timezone.utc) t = DelayedTask.schedule_tomorrow_polite("XX", "foo", 1, 2, kw1=1, kw2=5) assert t.due_at > now assert t.due_at.hour == 17 assert t.task_name == "foo" assert t.args == [1, 2] assert t.kwargs == {"kw1": 1, "kw2": 5}
def queue_download_reminder(ballot_request: BallotRequest) -> None: # send a reminder the next day tomorrow = datetime.date.today() + datetime.timedelta(days=1) when = datetime.datetime( tomorrow.year, tomorrow.month, tomorrow.day, 17, 0, 0, # 1700 UTC == 12pm ET == 9am PT == 6am HT tzinfo=datetime.timezone.utc, ) now = datetime.datetime.now().replace(tzinfo=datetime.timezone.utc) DelayedTask.schedule( when, "absentee.tasks.send_download_reminder", str(ballot_request.uuid), )
def test_schedule_days_later_polite(): now = datetime.datetime.now().replace(tzinfo=datetime.timezone.utc) t = DelayedTask.schedule_days_later_polite("XX", 2, "foo", 1, 2, kw1=1, kw2=4) assert t.due_at > now + datetime.timedelta(days=1) assert t.due_at.hour >= 17 assert t.task_name == "foo" assert t.args == [1, 2] assert t.kwargs == {"kw1": 1, "kw2": 4}
def test_schedule(): t = DelayedTask.schedule( datetime.datetime(2020, 1, 2, 3, 5, 5).replace(tzinfo=datetime.timezone.utc), "foo", "a1", "a2", kw1=1, kw2=3, ) assert t.due_at == datetime.datetime( 2020, 1, 2, 3, 5, 5).replace(tzinfo=datetime.timezone.utc) assert t.task_name == "foo" assert t.args == ["a1", "a2"] assert t.kwargs == {"kw1": 1, "kw2": 3}
def process_lob_letter_status(letter_id: str, etype: str) -> None: link = ( Link.objects.filter(external_tool=ExternalToolType.LOB, external_id=letter_id) .order_by("created_at") .first() ) if not link: logger.warning( f"Got lob webhook on unknown letter id {letter_id} etype {etype}" ) return action = link.action event_mapping = { "letter.mailed": EventType.LOB_MAILED, "letter.processed_for_delivery": EventType.LOB_PROCESSED_FOR_DELIVERY, "letter.re-routed": EventType.LOB_REROUTED, "letter.returned_to_sender": EventType.LOB_RETURNED, } item = action.get_source_item() if not item: logger.warning( f"Recieved lob status update on action {action} with not item, {etype}" ) return logger.info(f"Received lob status update on {item} of {etype}") if etype in event_mapping: t = event_mapping[etype] if not Event.objects.filter(event_type=t, action=action).exists(): action.track_event(t) event_trigger = { "letter.processed_for_delivery": { "Registration": [ ("register.tasks.send_print_and_forward_mailed", 0), ("register.tasks.send_mail_chase", 14), ], "BallotRequest": [ ("absentee.tasks.send_print_and_forward_mailed", 0), ("absentee.tasks.send_mail_chase", 14), ], "MoverLead": [ ("integration.tasks.send_moverlead_mailed", 0), ("integration.tasks.send_moverlead_reminder", 3), ("integration.tasks.send_moverlead_chase", 28), ], }, "letter.returned_to_sender": { "Registration": [("register.tasks.send_print_and_forward_returned", 0)], "BallotRequest": [("absentee.tasks.send_print_and_forward_returned", 0)], }, } if etype in event_trigger: itype = type(item).__name__ state = item.new_state if itype == "MoverLead" else item.state.code if itype in event_trigger[etype]: for task, days in event_trigger[etype][itype]: if days: DelayedTask.schedule_days_later_polite( state, days, task, str(item.pk), str(action.pk) ) else: DelayedTask.schedule_polite( state, task, str(item.pk), str(action.pk) )
def queue_registration_reminder(registration: Registration) -> None: DelayedTask.schedule_tomorrow_polite( registration.state.code, "register.tasks.send_registration_reminder", str(registration.uuid), )