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:

  1. import "github.com/gogf/gf/v2/os/gcron"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/os/gcron

Brief Description:

  1. The New method is used to create a custom scheduled task management object.
  2. The Add method is used to add scheduled tasks, where:
    • The pattern parameter uses CRON 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.
  3. 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).
  4. The AddOnce method is used to add scheduled tasks that run only once. When run once, the task automatically destroys itself.
  5. The AddTimes method is used to add scheduled tasks that run a specified number of times. When run times times, the task automatically destroys itself.
  6. The Entries method is used to obtain information on all currently registered scheduled tasks.
  7. The Remove method is used to delete scheduled tasks by name (stop and delete).
  8. The Search method is used to search scheduled tasks by name (returns the *Entry object pointer of the task).
  9. The Start method is used to start a scheduled task (Add automatically starts the task). The name parameter can be specified to start the task.
  10. The Stop method is used to stop a scheduled task (Remove stops and deletes). The name parameter can be specified to stop the task.
  11. 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

Documentation

📄️ Cron Job - Expressionscron 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 - UsageManage 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 - LoggingLog 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 JobThe 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.