: This article mainly introduces the implementation hypothesis of phpcron task management. if you are interested in PHP tutorials, please refer to it. In the past, each scheduled task was developed online.
CrontabSo I want to use PHP to manage task items in a single project,
You can create a table [id, name, status, func, timer, last_time, created_at] to store the script of the scheduled task in a unified manner.
Abstract The scheduled tasks into simple task classes, and then useCrontabTo reduce the online code
The trouble of modifying the online server environment.
Then there are some pitfalls here, because there may be several types of tasks by functional nature, such:
1. a single execution stops immediately. multiple instances can be run at the same time.
2.DaemonOnly one instance can be run at a time.
The second method requires a locking mechanism, and also prevents the program from starting the task again because of an unexpected error or exception.
For such a task, you also need to consider how to quickly and conveniently terminate the task if it is changed due to other requirements during running.
It is assumed that the lock mechanism is implemented by the task id. the lock must be applied for each task execution. the lock applied for each time has a fixed usage quota.
After each batch of execution is completed, you need to use the quota once. when the quota is 0, you need to apply for a new lock from the system.
Lock_id: $ task_id
Lock _ {$ task_id} _ quota: $ quota
Every time you apply for a lock again, you must read the configuration information of the task in the task configuration table again.
If the application fails (-1), close the execution and wait for the next execution. in this way, you can use two methods to stop the task:
1. to stop this operation, set the quota to-1.
2. it is completely forbidden. reset the status of this task to disabled, and then reset its usage quota to-1.
After the task is completed, release the lock. if the lock fails to be released, an invalid lock detection mechanism is required to force release the lock.
Determination of invalid locks:
The key is how to determine whether the task instance is alive, and whether the lock quota <1 can be determined as invalid locks. whether the task will appear in the process of re-applying for the lock
Is the lock detection mechanism ineffective?
Is it affected after it is killed?
--------------
The above is my assumption.
The above describes the implementation assumptions about php cron task management, including crontab and Daemon. I hope to help anyone who is interested in PHP tutorials.