How to run a PHP function in the background?

Asked

Viewed 3,838 times

5

I’m using the cPanel API to add dynamically parked domains. So far so good, send the required data via jQuery.post() only that this process takes a little to be completed, about 1 minute.

Have some alternative for the process to continue running in "background" and the user continues browsing the system?
Currently the user cannot navigate until the process is finished.

  • 1

    You could try using Threads (PHP) or take a look at it which may not be your problem, but is useful: http://css-tricks.com/multiple-simultaneous-ajax-requests-callback-jquery/

  • You could open a new window that fires that ajax and you’ll be waiting. Otherwise it could make the navigation happen on the same page, changing only the content of some parts of the page.

  • This would involve Messaging Pattern - SOA?

  • Thank you guys so much!

  • It could just generate a "cron" schedule. Via PHP, create a cronjob or schtask (windows) and leave it scheduled to start the task in 1 minute. This way, the browser, the visual part of the user can browse without worry and without having to wait for the process. As for scheduling running on the server, when you complete, fire an email to the user or message, something like that to let them know you’ve completed. It’s just an idea.. I particularly use it a lot. I prefer it that way than asynchronous actions or the use of ignore_user_abort().

4 answers

4

In the PHP script itself, add the following lines at the beginning:

set_time_limit( 7200 ); // Limite de tempo de execução: 2h. Deixe 0 (zero) para sem limite
ignore_user_abort( true ); // Não encerra o processamento em caso de perda de conexão

Below these lines program normally.

I hope I’ve helped!

2

I think it is possible to do the background execution using Curl as in the example below:

background-script.php

if ($_GET['iniciarbackground'] == 'true') { // chamada iniciada pelo ajax
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'http://www.yoursite.com/background-script.php');
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);

    // repassar ao cURL, tudo que recebeu como POST:
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);

    curl_exec($ch);
    curl_close($ch);
} else { // chamada iniciada pelo cURL
// tarefas a executar no background

}

jquery:

$.post( "background-script.php?iniciarbackground=true", { name: "John", time: "2pm" } );

Source: http://www.paul-norman.co.uk/2009/06/asynchronous-curl-requests/

  • Thank you very much Jader. really with Curl can be done the way you expected. a hug! all good.

0

There must be something wrong, if I’m not mistaken the jQuery by default makes the ajax requests without "LOCK" navigation, async.

Anyway check if the requisitions are being made asymptotically.

For users with windows host (taking advantage of the question) there is the function:

win32_create_service PHP that allows you to run a script as a service, with options to stop, pause, etc...

0

I think the best alternative for you is to create a daemon in php, for that you would have to have another server(worker) that can be cheap because you can manage a queue cadenciada. To accomplish this work I recommend the use of Gearman is excellent, the PHP documentation is here

  • Interesting. Would you better explain that threads are not yet stable? What problems can happen?

  • I think I better take this comment about threads because I did not find in the documentation again, probably will depend on the version of your PHP believe that in the versions 5.5 up is already very stable. I apologize

  • Fábio. managed to solve the problem with help from Jader. Using Curl.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.