Вопрос

How is is possible in Kohana 3 to be in the action_index method of a controller and using something like this code

 exec("php index.php --uri=home/other_method");

... to run 'other_method' in the background? I'm in 'home/index' and want to call 'home/other_method' as a background task. I don't want to wait for the web page to respond and 'other_method' can take up to 3 minutes to complete.

I don't want to use a queue and I would like to do it without Minion.

Is this possible? No matter what I try I can't get 'other_method' to even respond. I can access it directly and I can even use a CRON job on the server to call my exec() code.

Why can't I access it via exec() while in action_index? Is there another way to call 'other_method' using a kohana thread so that I can continue with action_index?

(Also, I code on Mac using MAMP. Maybe it has something to do with environment?)

Это было полезно?

Решение

  • In the top of your home/other_method Controller for your script not to stop set:

    ignore_user_abort(true);
    set_time_limit(0);
    
  • Create stream context configuring its timeout to short time period and not to produce Exception on timeout reached:

    $context = stream_create_context(array(
        'http'=>array(
            'timeout' => 1.0, // Set timeout to 1 second
            'ignore_errors' => true // Don't prouce Exception
        )
    ));
    
  • (Assume your home/other_method URL is http://localhost/other_method) call your URL using context created:

    $kick = file_get_contents(`'http://localhost/other_method'`, false, $context);
    

After timeout is reached, file_get_contents() stops returning empty string but your request will continue in the background till its end.

Beware of calling 'http://localhost/other_method' multiple times, all those request will run concurrently.

You can create something similar using Kohana's Request_Client_External class with CURL.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top