0

I have set up some FeedImporters and I've linked some custom batch imports to these FeedImporters. But for some reason, the function isn't triggered when running the cron... I'm wondering why. Anyone who can help me out on this one?

I've linked some custom functions to be triggered after the import:

/*
 *  Initialize hook_feeds_after_import()
 */
function my_module_feeds_after_import(FeedsSource $source) {
    _my_custom_function();
}

function _my_custom_function() {
    // Define fields
    $fields  = array('field1', 'field2', 'field3');

    // Trigger Batch for import
    batch_set(_start_my_batch('myfile', 'mytable', $fields));
}

And then my custom batch import:

function _start_my_batch($file, $table, $fields) {
    $batch = array(
      'operations'        => array(
        array('_import_csv_to_database_batch', array($file, $table, $fields)),
      ),
      'finished'          => '_import_csv_to_database_finish',
      'title'             => t("Import data in the $table-table"),
      'init_message'      => t("Feed is starting ($table)."),
      'progress_message'  => t('Processed @current out of @total.'),
      'error_message'     => t('Feed has encountered an error.'),
      'file'              => drupal_get_path('module', 'batch_example') . '/batch_example.inc',
    );

    batch_set($batch);
}

the _import_csv_to_database_batch is a custom batch import by the book. But for some reason, when I run following cron function, the _my_custom_function() isn't even triggered...

function my_module_cron() {
    _my_custom_function();
    watchdog('my_module', 'feed cron');
}

So why is that and how can I configure my cron to trigger this?

1

Can you change:

function _my_custom_function() {
    // Define fields
    $fields  = array('field1', 'field2', 'field3');
    // Trigger Batch for import
    batch_set(_start_my_batch('myfile', 'mytable', $fields));
}

Into

function _my_custom_function() {
    // Define fields
    $fields  = array('field1', 'field2', 'field3');
    $function = '_start_my_batch';
    $batch = $function('myfile', 'mytable', $fields);
    // Trigger Batch for import
    batch_set($batch);
}

I think you should check every functions separately (hook_cron, batch function..) and make sure they work. After that put on the same place and play. The Example Module also is the very good place for doing this kind of stuff.

  • Thanks for your input, but it didn't made any difference... Why did you thought it would help? – Michiel Aug 13 '13 at 12:52
  • Because i did the cron and batch function before. Can you check the _my_custom_function function. Just place it somewhere to invoke. – Stone Vo Aug 13 '13 at 13:52
  • Hi @Michiel. I searched and found that they usually dont use the batch + cron combination because the timeout problem. They use the Queues module for that. You can see with this example drupal.org/node/988192#comment-3847954. By the way. In my answer i just made the changes to make the batch function working. – Stone Vo Aug 14 '13 at 1:57
  • Hi @Stone, thanks for your effort! Do you have any idea why this isn't working? I'll try the Queue with the hook_cron... I'll keep you posted! – Michiel Aug 20 '13 at 9:48
  • You should try Queues module with cron and Let's see can it help you? (Please update if you have something new) – Stone Vo Aug 20 '13 at 10:39
0

You cannot perform batch jobs from cron. The Batch API requires user interaction to work.

As mentioned above, the Queue API is what you want to use for cron jobs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.