Reputation: 3136
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.
Upvotes: 4
Views: 2315
Reputation: 69581
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 */
Upvotes: 2
Reputation: 10536
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:
Upvotes: 7