4

im up to write background processing web service using php that need to run in background even user closed the browser is there any way to do this using php ?

More Info

Yes im working on large web project (pay roll) using symfony/php. it needs to process in every month when payroll user comes and click the process button. then payroll should process without apache server time out. for do that i hope to wirte asynchronous web service that run in background.

10
  • 1
    Yeah, what exactly do you want to do? Commented Sep 2, 2011 at 6:42
  • 1
    You can always run your PHP script from the command line and install it as a cronjob - but I agree to my previous posters, why would you want to do that with a web service? ;) Commented Sep 2, 2011 at 6:44
  • 1
    Create a symfony task, schedule it every 10 minutes in a cronjob and make it check the database to see if the button were pushed. That's pretty much the easier you can do. :-) Commented Sep 2, 2011 at 6:50
  • 2
    You should not. That's the goal of what we call a cronjob. Cron is a daemon which allows to run regularly a script/binary/whatever on the system independently of the web server. That's eventually called "backend". If you have around you good linux users or admins they will know about. Check out clickmojo.com/code/cron-tutorial.html Commented Sep 2, 2011 at 7:00
  • 1
    But even if you can directly and should not call it by the way the task, you can call the same code, yes, if it's in custom libraries or in your model classes. It's up to you and how you write your code. Commented Sep 2, 2011 at 7:01

2 Answers 2

7

As commentor said, you should use a CRON job as it's best suited for this kind of problems. However, you need to launch your job on the click of a user. Here is what I'd use:

  1. On the click of a user, create a row in some table, or create a file with needed parameter to execute the task. Basically it says to the CRON 'hey, you need to launch the task'.
  2. Setup a CRON job to look after that row/file every minute, and launch the task if the row/file is found. Delete the file before launching the task, or parallels tasks could happen.
  3. If you need to tell your user when the payroll is done, make the task create another row/file to show that the CRON ended, and with javascript refresh your user page every 30s/1min, and stop the automatic refresh when the new row/file is found, and display the appropriate output/notice message.
Sign up to request clarification or add additional context in comments.

7 Comments

problem is what will happen if user close the browser
The job will still be done, however user will loose the display of notice. Which seems to be what they wanted, as they closed the browser.
I created symfony task and then i want to execute it form shell_exec('/var/www/esm/symfony php symfony doNothing'); but apache error saying permission denied.
Why do you try to launch your task via PHP ? Use a CRON job.
The problem with CRON jobs is they are limited to running at most, once per minute, so you can't handle things in 'real time'.
|
2

Take a look at this article. Depending on what you're doing this can be much better than a CRON job, most specifically, if you want to take action immediately. CRON jobs are limited to running at most, once per minute, with this approach, you can begin handling the request in the background immediately.

// this script can run forever
set_time_limit(0);
 
// tell the client the request has finished processing
header('Location: index.php');  // redirect (optional)
header('Status: 200');          // status code
header('Connection: close');    // disconnect
 
// clear ob stack 
@ob_end_clean();
 
// continue processing once client disconnects
ignore_user_abort();
 
ob_start();
/* ------------------------------------------*/
/* this is where regular request code goes.. */
 
/* end where regular request code runs..     */
/* ------------------------------------------*/
$iSize = ob_get_length();
header("Content-Length: $iSize");
 
// if the session needs to be closed, persist it
// before closing the connection to avoid race
// conditions in the case of a redirect above
session_write_close();
 
// send the response payload to the client
@ob_end_flush();
flush();
 
/* -------------------------------------------*/
/* code here runs after the client disconnect */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.