Introduction
The gcron
module provides the implementation of scheduled tasks, supporting a configuration management style similar to crontab
, and manages scheduled tasks with a minimum granularity of seconds
.
Usage:
import "github.com/gogf/gf/v2/os/gcron"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/os/gcron
Brief Description:
- The
New
method is used to create a custom scheduled task management object. - The
Add
method is used to add scheduled tasks, where:- The
pattern
parameter usesCRON syntax format
(for details, see the subsequent related description in this chapter). - The
job
parameter is the method (address) that needs to be executed. - The
name
is an optional parameter used to assign a unique name to the scheduled task. Note that if a task with the same name already exists, adding the scheduled task will fail.
- The
- The
AddSingleton
method is used to add singleton scheduled tasks, meaning only one copy of the task can run simultaneously (deduplication is performed in memory). - The
AddOnce
method is used to add scheduled tasks that run only once. When run once, the task automatically destroys itself. - The
AddTimes
method is used to add scheduled tasks that run a specified number of times. When runtimes
times, the task automatically destroys itself. - The
Entries
method is used to obtain information on all currently registered scheduled tasks. - The
Remove
method is used to delete scheduled tasks by name (stop and delete). - The
Search
method is used to search scheduled tasks by name (returns the*Entry
object pointer of the task). - The
Start
method is used to start a scheduled task (Add
automatically starts the task). Thename
parameter can be specified to start the task. - The
Stop
method is used to stop a scheduled task (Remove
stops and deletes). Thename
parameter can be specified to stop the task. - The
Close
method is used to close the custom scheduled task management object.
Notes
- Influence of Global Timezone: Scheduled tasks strictly depend on time calculation, so the global timezone of the process greatly affects scheduled task execution. When adding scheduled tasks, pay attention to the global timezone settings of the current process. If no global timezone is set, the system timezone is used by default. For more information on timezone settings, please refer to: Time - Time Zone