AgentSkillsCN

job-scheduler

管理基于 APScheduler 的后台任务调度与执行。当您需要处理定时任务、Cron 任务、间隔任务、任务触发器、执行历史,或 Celery 任务时,可调用此技能。涵盖 CRUD 操作、手动触发、分布式锁,以及调度器状态的监控。

SKILL.md
--- frontmatter
name: job-scheduler
description: |
  Manages APScheduler-based background job scheduling and execution. Use when working with
  scheduled jobs, cron jobs, interval jobs, job triggers, execution history, or Celery tasks.
  Covers CRUD operations, manual triggers, distributed locking, and scheduler status monitoring.
allowed-tools: Read, Write, Edit, Grep, Glob, Bash

Job Scheduler Management

Overview

The Job Scheduler system manages background task execution using:

  • APScheduler - Core scheduling engine (AsyncIOScheduler)
  • FastAPI - REST API layer
  • SQLAlchemy - Async database operations
  • Celery - Long-running task offloading

Capabilities: Interval jobs, cron jobs, manual triggers, execution history, distributed locking, multi-instance coordination.

CRITICAL: Reuse existing patterns. Do not introduce new scheduling frameworks.

When to Use This Skill

Activate when request involves:

  • Creating/modifying scheduled jobs
  • Cron or interval scheduling
  • Job triggers, pause, resume, enable, disable
  • Execution history or cleanup
  • Scheduler status monitoring
  • Background job processing
  • Celery scheduler tasks

Quick Reference

Backend Locations

ComponentPath
API Routersrc/backend/api/v1/scheduler.py
Servicesrc/backend/api/services/scheduler_service.py
Repositorysrc/backend/api/repositories/scheduler_repository.py
Schemassrc/backend/api/schemas/scheduler_schemas.py
Modelssrc/backend/db/models.py
Celery Taskssrc/backend/tasks/scheduler.py

Frontend Locations

ComponentPath
Dashboardsrc/my-app/app/(pages)/scheduler/page.tsx
Componentssrc/my-app/app/(pages)/scheduler/_components/
Typessrc/my-app/types/scheduler.ts
Actionssrc/my-app/lib/actions/scheduler.actions.ts
Hookssrc/my-app/hooks/use-scheduler-jobs.ts

API Endpoints

MethodEndpointPurpose
POST/scheduler/jobsCreate job
GET/scheduler/jobsList jobs
GET/scheduler/jobs/{id}Get job
PUT/scheduler/jobs/{id}Update job
DELETE/scheduler/jobs/{id}Delete job
POST/scheduler/jobs/{id}/actionTrigger/pause/resume/enable/disable
GET/scheduler/jobs/{id}/historyExecution history
GET/scheduler/statusScheduler status

For complete API documentation, see REFERENCE.md.

Core Concepts

ScheduledJob Model

python
# Key fields
task_function_id: int       # What to execute
job_type_id: int            # 1=interval, 2=cron

# Interval (mutually exclusive with cron)
interval_seconds/minutes/hours/days: Optional[int]

# Cron
cron_expression: Optional[str]  # e.g., "0 2 * * *"

# Execution settings
priority: int = 0           # Higher runs first
max_instances: int = 1      # Parallel limit (1-10)
misfire_grace_time: int = 60  # Seconds for missed jobs
coalesce: bool = True       # Combine missed runs

# Status
is_enabled: bool = True     # Can be scheduled
is_active: bool = True      # Soft delete flag

Execution Lifecycle

code
pending → running → success
                 ↘ failed

Distributed Locking

Prevents duplicate execution across instances via ScheduledJobLock table. Locks expire after 1 hour.

For detailed model documentation, see REFERENCE.md.

Allowed Operations

DO:

  • Add API endpoints following existing patterns
  • Extend SchedulerService methods
  • Add job actions (following action pattern)
  • Update frontend scheduler components
  • Add/update tests

DON'T:

  • Bypass service layer
  • Call APScheduler directly from routes
  • Add new background systems (RQ, threads)
  • Modify HRIS/attendance logic

Implementation Patterns

For Celery task patterns, async patterns, and code examples, see:

Validation Checklist

Before completing scheduler work:

  • Existing patterns reused
  • Service layer contains logic
  • Repository layer for DB operations
  • Schemas inherit from CamelModel
  • Async/await used correctly
  • Tests updated

Utility Scripts

Validate cron expressions:

bash
python .claude/skills/job-scheduler/scripts/validate_cron.py "0 2 * * *"

Check job status (requires running backend):

bash
python .claude/skills/job-scheduler/scripts/check_job.py --job-id 1

Additional Resources

Scope Boundaries

This skill applies ONLY to the job scheduler system.

NOT covered:

  • Employee shifts (read-only from TMS)
  • Attendance records (read-only)
  • HRIS synchronization logic

If request involves both scheduler and HRIS, apply this skill only to scheduler portion.