Background Worker & Scheduled Tasks
10/30/24Less than 1 minute
Architecture & Workflow
flowchart LR
A[DB: SysJobModel] -->|update_job| B[Register periodic_worker]
B -->|cron trigger| C[Worker.perform execution]
C --> D[SysJobLogModel log]
E[Manual execute_job] --> C
F[enqueue_async] --> G[Processor concurrent consumption]
G --> C- Storage: SysJobModel (tasks), SysJobLogModel (logs)
- Scheduling: update_job → clear existing periodic tasks → scan DB → register by type
- Execution: Worker.perform processes messages, writes logs and increments run count
- Immediate trigger: execute_job or XxxWorker::enqueue_async / execute_async
Quick Start
pub async fn worker_init(self) -> Self {
let p = processor_job().await;
tokio::spawn(async move {
p.run().await;
});
s_sys_job::update_job().await;
self
}Task Management API
- List: GET /job/list
- Create: POST /job/add
- Edit: POST /job/edit
- Delete: POST /job/delete
- Execute: POST /job/execute
- Cron Validation: POST /job/validate-cron
Built-in Task Types & Workers
| Worker | Queue | Purpose | Key Parameters |
|---|---|---|---|
| RequestUrlWorker | default | Periodic HTTP GET (Webhook/heartbeat) | URL, job_id |
| InvokeFunctionWorker | default | Periodic internal function call | callfun, parmets |
| MailerWorker | mailer | Send email (text/HTML/template) | from, to, subject, text, html |
| LoginInfoWorker | logininfo | Login log post-processing | ipaddr, info_id |
| JobWorker | default | Task system utility | - |
Security & Stability
- invokefunction uses a whitelist of function mappings (already implemented)
- Strict parameter validation for job_params
- HTTP calls: add timeout and retry strategies
- Idempotence: internal functions should be idempotent
- Error handling: avoid unwrap_or_default; report exceptions explicitly