Using cron manager from within PHP

前端 未结 5 1785
别那么骄傲
别那么骄傲 2020-12-15 14:05

I am wanting to add a section in my php application to add / remove scheduled tasks.

Something similar to what they use in vBulletin Forum.

5条回答
  •  醉话见心
    2020-12-15 15:04

    If you don't have the exec() function enabled, you have to rely on manually setting at least one cron job via some interface and then managing individual jobs in your script.

    What I do for example is this

    1. I have a script heartbeat.php which is executed every minute
    2. This script checks a MySQL table crontab and checks if there is any row (job) with time_to_process <= NOW()
    3. If there is, it loads the class defined for the specific row from crontab table
    4. The class implements an interface with run() method and I only call $cronJob->run() to make it run
    5. The class then handles it's rescheduling via crontab table once it finishes

    A good example is to reset daily views on some items. The job is scheduled at May 1, 2010 00:00. Once the job ran, it schedules itself for the next day automatically.
    You can then implement automatic reset of failed jobs and many other features from within PHP.

提交回复
热议问题