~/drupal/54-drupal-7-how-implement-cron-tasks-hookcron-and-hookcronqueueinfo-takes-much-time.md

Drupal 7: How to implement cron tasks (hook_cron and hook_cron_queue_info) that takes much time

2 min readMarch 18, 2018by Gorav Singal#drupal#drupal-cron#drupal-websites
// TL;DR

Use hook_cron_queue_info with a queue worker callback to handle resource-intensive cron tasks that exceed the default hook_cron timeout.

Drupal 7: How to implement cron tasks (hook_cron and hook_cron_queue_info) that takes much time

Problem Statement

I want certain data to be updated regularly. Cron job is the best place to have such code. The real issue is that the process might take much time that normal cron job's timeout value exceeds.

hook_cron() suggests to put tasks that executes in shorter time or non-resource intensive tasks.

Mine was neither executing in shorter time nor non-resource intensive!

Solution

In this article, I'm going to show how to use cron functionality of drupal to put resource intensive OR CPU intensive tasks.

Drupal provides two hooks:

  • hook_cron
  • hook_cron_queue_info

Create a Module

Create a module, say name: mytestmodule

Create a file: mytestmodule.module

function mytestmodule_cron() {
   $queue = DrupalQueue::get('name-of-my-queue'); //queue name can be any string you want
   $test = array('29102', '1322', '2322'); //some random numbers
   foreach ($test as $t) {
      $queue->createItem($t);
   }
}
function mytestmodule_cron_queue_info() {
   $info['name-of-my-queue'] = array(
      'worker callback' => 'mytestmodule_do_tasks',
      'time' => 60,
   );
   return $info;
}
function mytestmodule_do_tasks($data) {
   //do something with data
   //For each cron run, this function will get called number of times I have created tasks
}

In hook_cron function, I have defined a queue, and put 3 tasks to it. Note: a single task can be to save a node. Here, you should have all the data you require to initiate a task.

In hook_cron_queue_info function, I have defined a callback function which will receive each task separately. And, specified a timeout value in seconds.

My callback function will be called as many number of times, as the number of tasks.

So, in this example, mytestmodule_do_tasks will receive values: 29012, 1322, 2322 independently.

Hope it helps.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Drupal 8: How to Export and Import View

Drupal 8: How to Export and Import View

You have created some views, and want to port it to your production environment…

Drupal Code: Fetch Link, Title, Category names, tag names from every article

Drupal Code: Fetch Link, Title, Category names, tag names from every article

See the code below: The output will be 4 columns separated by comma. You can…

Drupal: How to block a user by email programatically

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered…

Drupal 7: How to save a node programmatically and add an image field from a public URL

Drupal 7: How to save a node programmatically and add an image field from a public URL

Note: I have public URLs of these images, which I want to save. return…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

How to add alt attribute of images in all of my drupal articles or other content type

How to add alt attribute of images in all of my drupal articles or other content type

I have a custom content type, and there are around 2000 number of nodes in my…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…