Scalable, Delayed PHP Processing

后端 未结 15 956
长情又很酷
长情又很酷 2020-12-08 01:41

I\'m working on an online PHP application that has a need for delayed PHP event. Basically I need to be able to execute arbitrary PHP code x many seconds (but it could be da

15条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 02:06

    A can't think of anything that does everything you asked for:

    • has to be very precise
    • delay for long periods of time
    • ability to remove/change the time of the event

    The trivial way would be to use a combination of the following functions:

    set_time_limit(0);
    ignore_user_abort(true);
    time_sleep_until(strtotime('next Friday'));
    // execute code
    

    However, like @deceze said it's probably not a very good idea since if you set up a high delay Apache could eventually kill the child process (unless you're using PHP CLI, that would make it easier). It also doesn't allow you to change / delete the event unless you set up a more complex logic and a database to hold the events. Also, register_shutdown_function() might be useful if you want to go this road.

    A better approach would be to set up a CRON job in my opinion.

提交回复
热议问题