Hypothetical implementation of phpcron task management

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.