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
Documents
📄️ Cron Job - Expressions
cron expressions in the GoFrame framework and their usage tips. The cron expression consists of six fields, enabling time scheduling from seconds to weeks. It explains the significance of special characters and their application in expressions, making task scheduling more flexible and reliable through various predefined formats and interval configurations.
📄️ Cron Job - Usage
Manage cron jobs using gcron in the GoFrame framework. Learn how to add, start, stop, remove, and search cron jobs. Also covers advanced features like singleton cron jobs, one-time cron jobs, and running specified times jobs. These features help developers efficiently manage and debug in-app cron jobs, enhancing application performance and reliability.
📄️ Cron Job - Logging
Log management in the gcron component of the GoFrame framework. gcron supports setting log output files and levels, and by default, logs at the error level. Users can leverage all the features of the logging component through the GoFrame framework. The article provides Go code examples showing how to set and use gcron's logging feature.
📄️ Cron Job
The difference between the scheduled task module gcron and the timer module gtimer in the GoFrame framework. gtimer is a high-performance module suitable for various scheduling scenarios, including TCP communication and game development. gcron supports crontab syntax, built on gtimer, providing users with a convenient way to manage scheduled tasks.