Hello All,
I am trying to call batch api in drupal web services but batch api not working with drupal services whereas the same batch api code being successfully called in hook menu.
what i am doing for batch in service module is below....
function batch_my_module_import_start() {
$batch = array(
'title' => t('Import products'),
'operations' => array(
array('batch_my_module_import', array()),
),
'progress_message' => t('Import. Operation @current out of @total.'),
'error_message' => t('Error!'),
'finished' => 'my_module_batch_finished',
'file' => drupal_get_path('module', 'streetbid_services') . '/resources/batchtest.inc',
);
batch_set($batch);
batch_process('node/1');
}
function batch_my_module_import(&$context) {
// Your iterms. In my case select all products
$pids = 50;
// Get Count of products
if (empty($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $pids;
//watchdog('import', 'import products');
}
// Create Iteration variable
if (empty($context['sandbox']['iteration'])) {
$context['sandbox']['iteration'] = 0;
}
// Check for the end of cycle
if ($context['sandbox']['iteration'] < $context['sandbox']['max']) {
// Count of operation in one butch step
$limit = 10;
// Counts completed operations in one butch step
$counter = 0;
if ($context['sandbox']['progress'] != 0) {
$context['sandbox']['iteration'] = $context['sandbox']['iteration'] + $limit;
}
// Loop all Product items in xml
for ($i = $context['sandbox']['iteration']; $i < $context['sandbox']['max'] && $counter < $limit; $i++) {
$node = new StdClass();
$node->type = 'streetbid';
$node->language = 'und';
$node->uid = 1;
$node->created = strtotime("now");
$node->changed = strtotime("now");
$node->status = 1;
$node->comment = 0;
$node->promote = 0;
$node->sticky = 0;
$node->title = 'testdineshagain';
$node->field_filenumber['und'][0]['value'] = $i;
$node->field_street_number['und'][0]['value'] = $i;
$node->field_street_name['und'][0]['value'] = 'testbid';
$node->field_bid_city['und'][0]['value'] = 'delhi';
$node->field_bid_state['und'][0]['value'] = 'delhi';
$node->field_bid_zip['und'][0]['value'] = '11111';
node_save($node);
$context['results']['added']['products'][] = $node->nid;
// Update Progress
$context['sandbox']['progress']++;
$counter++;
// Messages
$context['message'] = t('Now processing product %name. Product %progress of %count', array('%name' => $node->nid, '%progress' => $context['sandbox']['progress'], '%count' => $context['sandbox']['max']));
$context['results']['processed'] = $context['sandbox']['progress'];
}
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function my_module_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('@count products added.', array('@count' => isset($results['added']) ? count($results['added']) : 0)));
}
else {
$error_operation = reset($operations);
drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
}
watchdog('import', 'import finished');
}
the problem is this when i am calling this "batch_my_module_import_start" function in hook_menu it's working well but when i am calling the same "batch_my_module_import_start" function in hook_services then it's not working.
Please suggest me what is the issue and how this can be achive.
Thanks
Dinesh Pal
Comments
Comment #1
marcingy commentedBasically I don't think this will work as batch uses drupal gotos this effectively will prevent services from working. What I would do is
1) Services adds request to a queue
2) Queue is then processed via a cron job
Alternatively there may be something that you can do to indicate to the batch api that it is a programmatic submission (not sure on this as I tend to never use the batch api and instead utilise drush etc etal for batch processing) - however this has the downside of timeouts server side are possible and/or the client has to wait for a long running request.
Comment #2
marcingy commented