Implementation assumptions about PHP cron task management

Source: Internet
Author: User
About the implementation of PHP cron task management
Before each development of a scheduled task function requires online operation crontab to add new items, so you want to use PHP to unify the management of task items in a single project?

You can create a table [Id,name,status,func,timer,last_time,created_at] to unify the scheduled task script in the project, through a simple configuration?

The ability to abstract each scheduled task into a simple task class and then access it uniformly through a single portal configured in Crontab, reducing the line code pair?

The hassle of modifying the online server environment.

?

Then there are some pits here, because there are several possible types of tasks that can be classified by functional nature, such as:?

?

1. Single execution, end immediately, multiple instances can be run at the same time?

2. Daemons can only run one instance at a time?

?

For the second kind of need? lock up?Mechanism, but also to prevent the program because of a detonation, abnormal, etc. are not unlocked, resulting in the task can not be started again?

For such tasks, it is also necessary to consider how to quickly and easily terminate this task if other requirements change during the run time.

?

Guess through the task ID to implement the lock mechanism, each time the task execution needs to apply for locks, each application of the lock has a fixed usage quota, such a task?

A usage quota is consumed once each batch is completed, and a lock is required to be re-applied to the system when the quota is 0.

?

lock_id: $task _id?

lock_{$task _id}_quota: $quota??

?

Every time you re-request a lock, you need to read the task's configuration information again in the Task configuration table?

?

If the request fails (-1), the execution is closed, waiting for the next execution. In this way, there are 2 options to be taken when such a task is to be discontinued:?

1. To abort this time, simply set its usage quota to-1?

2. Completely prohibit, reset the task to a status of disabled, and then reset its usage quota of-1?

?

After the task is finished, to release the lock, if the lock release fails, need to have one? Invalid lockDetection mechanism to force release?

?

Invalid lock decision:?

The key is how to determine if the task instance is alive, and whether the lock quota < 1 can be considered an invalid lock. Will there be a task in the process of re-application of the lock?

By the invalid lock detection mechanism to kill??

?

Is there any effect after the kill??

?

--------------?

Above is my imaginary, beg to shoot bricks?

?

  • Related Article

    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.