In the past, every time I developed a scheduled task function, I needed to operate crontab online to add new items, so I want to use PHP to uniformly manage the task items in a single project.
You can create a table [id, name, status, func, timer, last_time, created_at] to uniformly store the planned task scripts in the project. Through simple configuration, each planned task can be abstracted into simple task classes, and then uniformly accessed through a single entrance configured in crontab, thus reducing the need for online The trouble of modifying the code to the
online server environment.
Then there are some pitfalls here, because there may be several types of tasks divided by functional nature, such as:
1. Single execution, ends immediately , multiple instances can be run at the same time
2. The daemon process can only run one instance at the same time
For the second type, a locking mechanism is needed to prevent the program from being unlocked due to errors, exceptions, etc. As a result, this kind of task cannot be started again
For this kind of task, we also need to consider how to quickly and conveniently terminate this task if other requirements change during operation
I guess the lock mechanism is implemented through the task id, and each time the task is executed You need to apply for a lock. Each lock applied for has a fixed usage quota. This kind of task
will consume the usage quota once after each batch is executed. When the quota is 0, you need to re-apply for a lock from the system.
lock_id: $task_id
lock_{$task_id}_quota: $quota
Every time you reapply for a lock, you need to read the configuration information of the task in the task configuration table again
If the application fails (-1 ), then close this execution and wait for the next execution. In this way, when you want to abort this kind of task, you can take two options:
1. To abort this time, just set its usage quota to -1
2. Complete Disabled, reset the status of this task to disabled, and then reset its usage quota to -1
After the task is completed, the lock must be released. If the lock release fails, an invalid lock detection mechanism is required to force the release
Determination of invalid locks:
The key is how to determine whether the task instance is alive and whether the lock quota is
and is given by the invalid lock detection mechanism? Get rid of it?
Will there be any effects after getting rid of it?