In my previous post, I discussed how to import a large dataset into Drupal via Drush's batch API. In this blog post, I'll cover how to create background tasks in Drupal 7 that will take long amounts of time to finish.

Why would you ever want that?

If you have a task that must occur regularly, but will take a long amount of time to complete, the cron queue might be a good solution. For instance, if you have a lot of nodes that need to stay synchronized with a remote dataset, you might want to synchronize a large portion of them during a cron run, but would like your other cron tasks to complete in a timely manner.

Drupal provides an easy interface to adding such long-running background tasks via hook_cron_queue_info() and hook_cron() .

Implement hook_cron()

hook_cron() is run everytime the drupal cron job is run. However, it is not very well suited to longer running tasks since it runs them sequentially. To avoid holding up the other cron job tasks, we'll need to create an item in the DrupalQueue.

function mymodule_cron() { // ... // get dataset to work on // ... $queue = DrupalQueue::get("resync"); foreach ($dataset as $data) { $queue->createItem($data); } }

Now on every cron run, we'll insert a bunch of items in our queue to process. Let's go ahead and let Drupal know what to do for each queue's item.

function mymodule_cron_queue_info() { $queues['resync'] = array ( 'worker callback' => 'mymodule_resync_item', 'time' => 180, // Time, in seconds, to let this process run [Optional] ); } function mymodule_resync_item($data) { // ... // Code to resync data here // ... }