Hello: I have an application in which it would be ideal to run two background HTTP requests in serial. This page on StackExchange suggests to use Background Batch. I feel like that might be a bit heavy, and the other approach that comes to mind is to run the second request within the first request's designated callback:

Idea

 background_process_http_request('http://www.example.com/stuff/', array('callback' => 'mycallback'));

function mycallback($result) {
  // Actually, ignore $result and just run the second request
  background_process_http_request('http://www.example.com/things/');
}

This would work well, except I'd really like to be able to pass in some additional arguments for the callback function, something more like this:

Better idea...?

 background_process_http_request('http://www.example.com/stuff/', array('callback' => 'mycallback', 'callback_args' => array(1,2,3)));

function mycallback($result,$args) {
  // Actually, ignore $result and just run some other requests (using $args)
  foreach ($args as $arg) {
    background_process_http_request('http://www.example.com/things/'.$arg);
  }
}

Is this a worthwhile feature request? ... or would it be simpler/better to implement the "argument passing" using objects?

Comments

holtzermann17’s picture

Issue summary: View changes

fix formatting

gielfeldt’s picture

Hi

For regular http requests (not starting background processes as such), it's possible to issue them in a "sliding window" fashion using the "postpone" option.

<?php
  $r = array();
  for ($i = 0; $i < 10; $i++) {
    $r[] = background_process_http_request('http://www.example.com/stuff/', array('postpone' => TRUE, 'callback' => 'mycallback'));
  }
  background_process_http_request_process($r, array('limit' => 5));
?>

The above will run launch all 10 ten requests, but only 5 at time. For serial execution, you could just set the "limit" to 1.

In this case (and the one you wrote), the 'callback' option is not used as part of the URL, but will be called when the request is finished and have the request object passed as an argument.

holtzermann17’s picture

Status: Active » Fixed

Hi Gielfeldt - thanks very much for the tip! That works well for me in combination with background_process_start. Cheers!

function my_module_do_stuff{
    // add some processes to be run in serial
    ...
    background_process_start('my_module_run_bg_requests',$r);
    ...
}

function my_module_run_bg_requests($requests){
  background_process_http_request_process($requests, array('limit' => 1));
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

make idea 2 more clear