简体   繁体   中英

PHP page to execute a background process

I have a PHP script to pull user specific data from a 3rd party source and dump it into a local table, which I want to execute every X mins when a user is logged in, but it takes about 30 seconds to run, which I don't want the user to experience. I figured the best way of doing this would be to timestamp each successful pull, then place some code in every page footer that checks the last pull and executes the PHP script in the background if it was more than X minutes ago.

I don't want to use a cron job to do this, as there are global and session variables specific to the user that I need when executing the pull script.

I can see that popen() would allow me to run the PHP script, but I can't seem to find any information relating to whether this would be run as the visitor within their session (and therefore with access to the user specific global or session variables) or as a separate session altogether.

  1. Will popen() solve my problem?
  2. Am I going about this the right way or is there a better method to execute the script at intervals while the user is logged in?

Any help or advice is much appreciated, as always!

Cheers

Andy

Maybe an idea to put the session data also in a table. That way you can easily access it from your background process. You only have to pass the session_id() or the user id as argument so the process knows which user it is currently processing.

  1. No, because PHP still needs to wait on the process started by popen() before it can exit
  2. Probably not, because the solution doesn't fit the architectural constraints of the system

Your reasoning for not using a cron job isn't actually sound. You may have given up on exploring that particular path too quickly and drawn yourself into a corner with trying to fit a square peg in a round hole.

The real problem you're having is figuring out how to do inter-process communication between the web-request and the PHP running from your crontab. This can be solved in a number of ways, and more easily then trying to work against PHP's architectural constraints.

For example, you can store the session ids in your database or some other storage, and access the session files from the PHP script running in your crontab, independently of the web request.

Let's say you determine that a user is logged in based on the last request they made to your server by storing this information in some data store as a timestamp, along with the current session id in the user table.

The cron job can startup every X minutes, look at the database or persistence store for any records that have an active timestamp within the given range, pull those session ids and do what's needed.

Now the question of how do you actually get this to scale effectively if the processing time takes more than 30 seconds can be solved independently as well. See this answer for more details on how to deal with distributed job managers/queues from PHP .

I would use Javascript and AJAX requests to call the script from the frontend and handle the result. You could then set the interval in JavaScript and send an AJAX-Request each interval tick.

AJAX is what you need: http://api.jquery.com/jquery.ajax/

The use the method "success" to do something after those 30 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM