0

I have two feeds importers. I wanted to run the 2nd importer when the first is finished.

I have written a cronapi hook for those two importers.

/**
 * Implements hook_cronapi().
 */
function mymodule_cronapi($op, $job = NULL) {
  $items = array(
    'cron_one' => array(
      'description' => 'import feeds one',
      'rule' => '4 0 * * *',
      'callback' => 'mymodule_import_feeds_one',
    ),
    'cron_2' => array(
      'description' => 'Import feeds two',
      'rule' => '4 0 * * *',
      'callback' => 'mymodule_import_feeds_two',
    ),
  );

  return $items;
}

In the hook_enable I am executing the first importer

function mymodule_enable() {
  mymodule_import_feeds_one();
}

function mymodule_import_feeds_one() {
  $src = feeds_source('import_feeds_one');
  $src->save();
  // Set the directory path.
  $source = 'directory-path';
  $config = array('FeedsFileFetcher' => array('source' => $source));
  $src->addConfig($config);
  $src->save();
  try {
    $src->import();
    $src->schedule();
  }
  catch (Exception $e) {
    return FALSE;
  }
  // Prevent notices from being thrown.
  $src->state(FEEDS_FETCH);
  $src->state(FEEDS_PROCESS);
  $src->state(FEEDS_PROCESS_CLEAR);

  return FALSE;
}

In hook_feeds_after_import

/**
 * Implements hook_feeds_after_import().
 */
function mymodule_feeds_after_import(FeedsSource $source) {
  if ($source->id == 'import_feeds_one') {

    mymodule_import_feeds_two();
  }
}

The problem is, when I enable the module, it schedules both importers on next cron run.

I want to schedule only the first one and when the first one is completed then schedule the 2nd one.

How can I do that?

  • When I use elysia_cron_run_job in hook_feeds_after_import. The first import runs fine. The 2nd import is in stage importing with 1%. But I see Next import: not scheduled. – Ahmad Sep 2 at 9:32
  • Scheduling an import depends on the periodic import settings configured on the importer, not on your custom defined cron job in mymodule_cronapi(). – MegaChriz Sep 2 at 11:37
0

Your approach is already heading in the good direction. If you don't want to schedule the import of both importers, then you should only schedule the first one in mymodule_cronapi().

Then, in mymodule_feeds_after_import(), you'll schedule the next one by calling startImport() on the second source object (or by doing something similar as you did in mymodule_import_feeds_one()). If you configure your importer to import in the background, then startImport() will result into the source being imported during cron. If your import task is large, it may be divided in multiple chunks (depends on the implementation of the parser) so PHP wouldn't exceed the maximum runtime. In this case the import may take multiple cron runs to complete.

/**
 * Implements hook_cronapi().
 */
function mymodule_cronapi($op, $job = NULL) {
  $items = array(
    'cron_one' => array(
      'description' => 'import feeds one',
      'rule' => '4 0 * * *',
      'callback' => 'mymodule_import_feeds_one',
    ),
  );

  return $items;
}

/**
 * Implements hook_feeds_after_import().
 */
function mymodule_feeds_after_import(FeedsSource $source) {
  if ($source->id == 'import_feeds_one') {

    feeds_source('import_feeds_two')->existing()->startImport();
  }
}

I'm not sure why you want to start an import in mymodule_enable(). This code is only called once, when enabling your custom module (unless you disable the module and enable it again).

  • I want to import the items when the module is enabled. And then next time during cron runs. – Ahmad Sep 3 at 4:35

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.