# Scheduled Tasks
Scheduled tasks are used to define tasks or logic that need to be executed at specific times in the system, such as sending contract expiration notifications or periodically archiving data.
# Types of System Scheduled Tasks
The system supports the following three types of scheduled tasks:
- Tasks executed at startup (Run at Startup)
These tasks are automatically executed once when the system starts up. They are suitable for initialization operations or data processing tasks that need to be executed immediately after system startup.
Example usage scenarios:
Loading system configurations, initializing connections with OpcUa servers, cache warming, etc.
- One-time delayed execution tasks Schedule Task (One Time)
These tasks are preset to execute once at a specific time in the future. They are suitable for tasks that need to be executed with a delay or at a specific time.
Example usage scenarios:
Sending reminder emails, executing batch data processing, system maintenance operations, etc.
- Periodically recurring tasks (Cron Task)
These tasks use Springboot's Cron expression (opens new window) to set the execution cycle, allowing them to run cyclically by minute, hour, day, week, month, etc. They are suitable for tasks that need to be repeated regularly.
Example usage scenarios:
Regular data backups, cache cleaning, statistical monitoring, etc.
Our scheduled task framework supports all three types, allowing flexible selection and configuration based on actual needs. It also provides task scheduling, monitoring, logging, and other management functions.
# Enable Logic
In scheduled tasks, enableLogic can be set to determine whether the scheduled task needs to run. The input and output parameters of the dynamic logic for enabling scheduled tasks are described as follows:
# Input Parameters
Variable Name | Variable Type | Description |
---|---|---|
triggerDatetime | java.time.LocalDateTime | Trigger time of the scheduled task |
task | tech.muyan.dynamic.task.DynamicTask | Instance of the triggered scheduled task |
application | grails.core.GrailsApplication | Current grails application context |
log | Closure<?> | Log closure for printing execution logs |
# Return Result
The structure of the return result after running Enable Logic is as follows
// ่กจ็คบ่ฏฅ action ๆ task ๆ widget ๆฏๅฆๅฏ็จ
// Indicates whether this action or task or widget is enabled
[result: true | false]
2
# Core Logic
The core logic of a scheduled task is the specific code executed when the task runs. Its input and return values are described as follows:
# Input Parameters
Variable Name | Variable Type | Description |
---|---|---|
triggerDatetime | java.time.LocalDateTime | Trigger time of the scheduled task |
task | tech.muyan.dynamic.task.DynamicTask | Instance of the triggered scheduled task |
application | grails.core.GrailsApplication | Current grails application context |
log | Closure<?> | Log closure for printing execution logs |
# Return Result
The return result is a Map structure. The following is the structure of the return result after the scheduled task execution:
return [
//ๆง่ก็ปๆ๏ผ็ฑปๅไธบๆๆฌ
//Execution result, type is text
execResult: 'OK, Result'
]
2
3
4
# Scheduled Task Rerun
Scheduled task rerun is a way to manually trigger and rerun a task for a specific execution cycle instance.
When rerunning a scheduled task, the original task's trigger time will be used as the triggerDatetime
parameter.
The trigger method is: Select an execution record on the list page of scheduled task execution records (DynamicTaskExecRecord
), and perform the Rerun Task
operation.