MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / run_due_jobs

Function run_due_jobs

deployments/feishu/webhook-server/scripts/scheduler.py:421–522  ·  view source on GitHub ↗
(
    *,
    now: Optional[datetime] = None,
    roles_path: Path = ROLE_META_PATH,
    state_path: Path = SCHEDULER_STATE_PATH,
)

Source from the content-addressed store, hash-verified

419
420def run_due_jobs(
421 *,
422 now: Optional[datetime] = None,
423 roles_path: Path = ROLE_META_PATH,
424 state_path: Path = SCHEDULER_STATE_PATH,
425) -> List[Dict[str, str]]:
426 if not scheduler_enabled():
427 return []
428
429 current_time = get_scheduler_now(now)
430 state = load_scheduler_state(state_path)
431 _update_runtime_state(state, started_at=current_time)
432 roles = get_scheduled_roles(load_roles_meta(roles_path))
433 results: List[Dict[str, str]] = []
434 state_changed = False
435
436 for role in roles:
437 role_name = role["role_name"]
438 user_id = role["user_id"]
439 chat_id = role["chat_id"]
440
441 if not chat_id:
442 LOGGER.warning("Skipping scheduled jobs for %s: missing feishu_chat_id", role_name)
443 continue
444
445 daily_job_key = f"daily_push:{role_name}"
446 daily_marker = _daily_marker(current_time)
447 daily_trigger = None
448 if _is_retry_due(state, daily_job_key, daily_marker, current_time):
449 daily_trigger = "retry"
450 elif should_run_daily_push(current_time, role_name, state):
451 daily_trigger = "scheduled" if _is_within_schedule_window(current_time, *get_daily_push_time()) else "compensation"
452
453 if daily_trigger and _acquire_job(daily_job_key):
454 try:
455 _trigger_daily_push(role_name, user_id, chat_id)
456 _mark_ran(state, daily_job_key, daily_marker)
457 _clear_retry(state, daily_job_key)
458 state_changed = True
459 results.append({"job": "daily_push", "role_name": role_name, "status": "success", "trigger": daily_trigger})
460 except Exception as exc:
461 LOGGER.exception("Scheduled daily push failed for %s: %s", role_name, exc)
462 retry_scheduled = _schedule_retry(state, daily_job_key, daily_marker, str(exc), current_time)
463 state_changed = True
464 results.append(
465 {
466 "job": "daily_push",
467 "role_name": role_name,
468 "status": "error",
469 "trigger": daily_trigger,
470 "retry_scheduled": retry_scheduled,
471 }
472 )
473 alert_suffix = "retry scheduled" if retry_scheduled else "retries exhausted"
474 _send_scheduler_alert(f"daily_push failed for {role_name}: {exc} ({alert_suffix})")
475 finally:
476 _release_job(daily_job_key)
477
478 weekly_job_key = f"weekly_report:{role_name}"

Callers 1

scheduler_loopFunction · 0.85

Calls 15

scheduler_enabledFunction · 0.85
get_scheduler_nowFunction · 0.85
load_scheduler_stateFunction · 0.85
_update_runtime_stateFunction · 0.85
get_scheduled_rolesFunction · 0.85
_daily_markerFunction · 0.85
_is_retry_dueFunction · 0.85
should_run_daily_pushFunction · 0.85
_scheduled_datetimeFunction · 0.85
get_daily_push_timeFunction · 0.85
_acquire_jobFunction · 0.85

Tested by

no test coverage detected