Scalable, Delayed PHP Processing

后端 未结 15 881
长情又很酷
长情又很酷 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:04

    I am not sure why you are trying to avoid cron. You could create a queue of requests in a table, and have cron fire up a process to check for current jobs.

    There are a few issues, depending on your exact requirements. For instance:

    • How precise must the call be?
    • How long would each call take?
    • What is the normal and peak load in any given period?

    So if you want precise execution, or execution takes longer than a second, or there is the potential for a heavy load, then the cron approach can run into problems.

    I have many daeons that run PHP (using daemontools). With this approach you could hold the requests in core and perform whatever timing you wanted internally.

    However if exact and relaible timing is what you want you should probably move away from PHP altogether.

    0 讨论(0)
  • 2020-12-08 02:04

    Here's the correct answer, but you may not like it.

    PHP is designed entirely around being used as a request-response (http) language, and thus doesn't support what you are looking for - it's great to hack and find ways around, but it will be just that, a hack, whatever 'solution' you end up getting.

    What you really need is an event driven language that supports xmpp, and for that you need look no further than node.js /v8 and the supporting XMPP libraries - this natively supports and is designed for just what you need. you could also go down the Java route, but if you want to port quickly and get a whole host of new features and support for what you are doing, node is the one.

    If you insist on going with PHP (as I have many times over many years) the 'lightest' and most effective way to do this is a persistent PHP deamon with an event Queue in a database - sadly!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 02:06

    What about using either cron to run a checker, that can the execute stuff from the DB for example.

    Or using the "at" linux command to schedule execution of some command?

    0 讨论(0)
  • 2020-12-08 02:09

    Have your php script make an exec call to schedule your PHP script to run at the time you need using the command "at"

    exec("at 22:56 /usr/bin/php myscript.php");

    at executes commands at a specified time.

    from the man page:

    At allows fairly complex time specifications, extending the POSIX.2 standard. It accepts times of the form HH:MM to run a job at a spe cific time of day. (If that time is already past, the next day is assumed.) You may also specify midnight, noon, or teatime (4pm) and you can have a time-of-day suffixed with AM or PM for running in the morning or the evening. You can also say what day the job will be run, by giving a date in the form month-name day with an optional year, or giving a date of the form MMDDYY or MM/DD/YY or DD.MM.YY. The specifi cation of a date must follow the specification of the time of day. You can also give times like now + count time-units, where the time-units can be minutes, hours, days, or weeks and you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with tomorrow.

    Further, if you need one second time resolution, have your script run at the start of the minute, then just sleep n seconds until it is time to execute.

    0 讨论(0)
  • 2020-12-08 02:11

    You could use Node.JS which is an event-driven, JavaScript-based web server. Run it on a secret, internal port with a script that receives notification from the PHP script and then schedules the action to be run xx seconds later. The action in Node.JS could be as simple as running a PHP script on the main web server.

    0 讨论(0)
提交回复
热议问题