Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write asynchronous web service run in background php

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.

like image 801
Roshan Wijesena Avatar asked Feb 24 '23 02:02

Roshan Wijesena


1 Answers

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.
like image 98
Clement Herreman Avatar answered Mar 08 '23 05:03

Clement Herreman