Curl alternative to Cron Job PHP

I am currently using the following php exec command to load the server side url, since I need a solution that does not include the cron job as well as asynchronous, i.e. the user can move from the page, the task is initiated, and the task will still be run:

exec("nohup curl ".$dbupdateurl." > /dev/null 2>&1 & echo $!"); 

This works fine, but rather unpredictably. Is there a better / more solid way to achieve this?

Thanks,

Matt

+5
source share
1 answer

If you are using PHP-FPM, you can use fastcgi_finish_request :

This function deletes all response data to the client and completes the request. This allows you to perform time-consuming tasks without leaving an open connection with the client.

This way you can use your own PHP functions to execute your web request after the user has already received the response. An example of using this function can be found in the example of using fastcgi_finish_request ()

If you are not using PHP-FPM , you can use JobQueue, such as Gearman , to do the work outside the original request. This is probably the most reliable way to do this, since it is a special system for processing these types of tasks.

Also note that curl can execute asynchronous HTTP requests . If the problem is that you do not want to wait for an HTTP response, this may be an easier solution than introducing JobQueue.

Last but not least, you can develop an additional PHP process to do the job. See How to execute a lot of time after sending a response to a client and php to execute a background process

0
source

Source: https://habr.com/ru/post/1235982/


All Articles