About the implementation of PHP cron task management

Source: Internet
Author: User
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 scripts in your project, with 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 the crontab, thus reducing the on-line code

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

The second need to lock the 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 terminate this task quickly and conveniently if other requirements change during the run.

It is assumed that the lock mechanism is implemented by the task ID, and each time a task is executed, a lock is requested and a fixed quota is used for each lock.

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

LOCK_ID: $task _id

lock_{$task _id}_quota: $quota

Each time the lock is re-requested, the configuration information for the task in the Task configuration table needs to be read again

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. Abort this session by setting its usage quota to-1

2. Completely prohibit, reset the task's status to Disabled, and then reset its usage quota to 1

After the task is finished, to release the lock, if the lock release fails, an invalid lock detection mechanism is required to force the 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

To be killed by an invalid lock detection mechanism?

Is there any effect after the kill?

  • 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.