
Scheduled Tasks
1. Core Functions
- Scheduled Task Management: Supports defining task execution cycles through Cron expressions, and supports operations such as adding, editing, deleting, and manually executing tasks.
- Task Types: Currently supports two types of tasks:
geturl
: Periodically requests a specified URL.invokefunction
: Periodically calls a specified function (requires function name and parameters).
- Automatic Scheduling: The system periodically checks for due tasks and executes them automatically, while updating the next execution time for the tasks.
- Persistent Storage: Task configurations are stored in the
sys_job
table in the database, supporting dynamic configuration updates.
2. Technical Implementation
Core Data Structures
PeriodicJob
: Defined insrc/worker/common/periodic.rs
, used to describe the core information of scheduled tasks, including:name
: Task nameclass
: Task processor class name (used to associate execution logic)cron
: Cron expression (defines execution cycle)queue
: Task queue nameargs
: Task parameters (JSON format)- Other attributes: Retry configuration, next execution time, etc.
Job
: Defined insrc/worker/common/job.rs
, is the executable unit converted fromPeriodicJob
, containing information such as queue, parameters, execution status, etc.
Task Scheduling Mechanism
Task Storage:
- Periodic tasks are stored through Redis's sorted set (
periodic
key), sorted by "next execution time" as the score. - Scheduled tasks to be executed (non-periodic) are stored in the
schedule
andretry
sorted sets.
- Periodic tasks are stored through Redis's sorted set (
Scheduling Logic:
- After system startup,
Processor
(src/worker/common/processor.rs
) will start two periodic checking tasks:- Every 5 seconds, check the
schedule
andretry
sets, adding due tasks (current time ≥ task execution time) to the execution queue. - Every 30 seconds, check the
periodic
set, execute due periodic tasks, then calculate the next execution time and update it back to theperiodic
set.
- Every 5 seconds, check the
- After system startup,
Task Management and Execution Flow
Task Configuration Management:
- Database table
sys_job
(defined inqiluo.sql
) stores task configurations, including task ID, type, Cron expression, parameters, status, etc. - Manage task configurations through methods (
add
/edit
/delete
, etc.) insrc/model/sys/model/msys_job.rs
.
- Database table
Task Loading and Updating:
- The
update_job
function (src/service/sys/s_sys_job.rs
) clears existing periodic tasks and loads all tasks in enabled status (status = 0
) from the database, re-registering them to Redis. - Registration logic is implemented through the
periodic_worker
function (src/worker/periodic_manager.rs
), converting tasks toPeriodicJob
and storing them in Redis'speriodic
set.
- The
Task Execution:
- When a task is due, the system converts
PeriodicJob
toJob
, and executes specific logic through the corresponding processor (such asRequestUrlWorker
handlinggeturl
tasks,InvokeFunctionWorker
handlinginvokefunction
tasks). - After execution, update the task's
run_count
(number of executions) in the database.
- When a task is due, the system converts
3. Key Code Modules
Module Path | Functionality |
---|---|
src/worker/common/periodic.rs | Defines the PeriodicJob structure and core logic such as Cron parsing and next execution time calculation. |
src/worker/common/scheduled.rs | Implements the queuing logic for scheduled tasks (reading due tasks from Redis and executing them). |
src/worker/common/processor.rs | Starts the scheduler, periodically checks and triggers task execution. |
src/service/sys/s_sys_job.rs | Provides task management API (CRUD operations, execution, Cron expression validation, etc.). |
src/model/sys/entity/sys_job.rs | ORM mapping for the database table sys_job . |
4. Example Tasks
The database initialization data (qiluo.sql
) includes two example scheduled tasks:
- Request Baidu (
https://www.baidu.com/
) every 30 seconds. - Request QQ (
https://www.qq.com/
) every 30 seconds.
In summary, the repository's scheduled task system implements efficient and reliable periodic task scheduling through Rust's asynchronous runtime (Tokio), Redis cache, and Cron expressions, supporting dynamic configuration and multiple task types, suitable for automation scenarios in enterprise applications.