def scheduler() -> None: """ Celery beat main scheduler for reports """ if not is_feature_enabled("ALERT_REPORTS"): return with session_scope(nullpool=True) as session: active_schedules = ReportScheduleDAO.find_active(session) for active_schedule in active_schedules: for schedule in cron_schedule_window( active_schedule.crontab, active_schedule.timezone ): logger.info( "Scheduling alert %s eta: %s", active_schedule.name, schedule ) async_options = {"eta": schedule} if ( active_schedule.working_timeout is not None and app.config["ALERT_REPORTS_WORKING_TIME_OUT_KILL"] ): async_options["time_limit"] = ( active_schedule.working_timeout + app.config["ALERT_REPORTS_WORKING_TIME_OUT_LAG"] ) async_options["soft_time_limit"] = ( active_schedule.working_timeout + app.config["ALERT_REPORTS_WORKING_SOFT_TIME_OUT_LAG"] ) execute.apply_async((active_schedule.id, schedule,), **async_options)
def test_cron_schedule_window_los_angeles( current_dttm: str, cron: str, expected: List[FakeDatetime]) -> None: """ Reports scheduler: Test cron schedule window for "America/Los_Angeles" """ with freeze_time(current_dttm): datetimes = cron_schedule_window(cron, "America/Los_Angeles") assert (list( cron.strftime("%A, %d %B %Y, %H:%M:%S") for cron in datetimes) == expected)
def test_cron_schedule_window_chicago_daylight( current_dttm: str, cron: str, expected: List[FakeDatetime]) -> None: """ Reports scheduler: Test cron schedule window for "America/Chicago" """ with freeze_time(current_dttm, tz_offset=0): datetimes = cron_schedule_window(cron, "America/Chicago") assert (list( cron.strftime("%A, %d %B %Y, %H:%M:%S") for cron in datetimes) == expected)
def test_cron_schedule_window_invalid_timezone( current_dttm: str, cron: str, expected: List[FakeDatetime]) -> None: """ Reports scheduler: Test cron schedule window for "invalid timezone" """ with freeze_time(current_dttm): datetimes = cron_schedule_window(cron, "invalid timezone") # it should default to UTC assert (list( cron.strftime("%A, %d %B %Y, %H:%M:%S") for cron in datetimes) == expected)
def test_cron_schedule_window_new_york(app_context: AppContext, current_dttm: str, cron: str, expected: List[FakeDatetime]) -> None: """ Reports scheduler: Test cron schedule window for "America/New_York" """ with freeze_time(current_dttm, tz_offset=0): datetimes = cron_schedule_window(cron, "America/New_York") assert (list( cron.strftime("%A, %d %B %Y, %H:%M:%S") for cron in datetimes) == expected)