sample implementation

gsvitak - February 24, 2008 - 18:07
Project:Job queue
Version:5.x-3.0
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Hello,

I was wondering if someone could point me a sample implementation of the job queue module or post an example of how to use the api.

Thanks,

#1

Ryan Palmer - May 15, 2008 - 01:15

I had difficulty using this module not knowing how best to utilize it with other modules. Can we add something like this to a README file:

==Introduction==

This module can be used by other modules to queue up function calls for execution on cron. The job_queue_add() function may be used by any module to add a job to the queue. It is documented as follows (from job_queue.module):

<?php
/**
* Add a job to the queue. The function added will be called in the order it
* was added during cron.
*
* @param $function
*   The function name to call.
* @param $description
*   A human-readable description of the queued job.
* @param $arguments
*   Optional array of arguments to pass to the function.
* @param $file
*   Optional file path which needs to be included for $fucntion.
* @param $no_duplicate
*   If TRUE, do not add the job to the queue if one with the same function and
*   arguments already exists.
*/
function job_queue_add($function, $description, $arguments = array(), $file = '', $no_duplicate = FALSE) {
?>

==Example==

Normally, to send email from Drupal, the drupal_mail function is used directly to send email. If many emails must be sent at one time, however, this process may cause Drupal to time out. Instead, job_queue_add() may be used to queue the job to eventually be executed as cron runs and the job_queue module processes the job queue.

Before:

<?php
  drupal_mail
('some-email-id', $to, $subject, $body, $from, $headers);
?>

After:

<?php
  job_queue_add
('drupal_mail', 'Description of the email process', array('some-email-id', $to, $subject, $body, $from, $headers), '', TRUE);
?>

As we can see, the first argument is the name of the function we would have otherwise executed, the next is a description to give this job, and the next is the array of arguments that you otherwise would have executed the original function with. The last two arguments are optional, but in this case it was decided to include the TRUE argument to insure that duplicate emails are not accidentally queued.

From here, we can see the job appear in the job queue:
http://www.mysite.com/admin/logs/job_queue

As cron is triggered, the queue will be processed and the jobs removed from it.

==Setting Priorities==
Modules may also define their own function priorities. A priority weight (from -10 to 10) may be set to determine which jobs get priority over others, based on their executing function.

The hook hook_job_queue_functions() may be used to define such functions to prioritize:

Example:

<?php
 
function mymodule_job_queue_functions() {
   
$function['drupal_mail'] = array('title' => 'Drupal mail');
    return
$function;
  }
?>

Once defined initially, the function's priority may be configured here:
http://www.mysite.com/admin/settings/job_queue

#2

Ryan Palmer - May 18, 2008 - 06:32
Status:active» needs review

#3

gsvitak - May 23, 2008 - 20:31

thank you wish I had this a couple of months ago.. very very good..

#4

Ryan Palmer - June 18, 2008 - 05:55
Version:5.x-2.0» 5.x-3.0

#5

drumm - July 21, 2008 - 00:43
Status:needs review» fixed

Thanks for the documentation! I decided to go ahead and put this on the project front page. I did do some editing, such as replacing some uses of "may be" with "is."

#6

abqaria - August 3, 2008 - 20:24

how to know the name of the other functions that usually run on cron ?

#7

Anonymous (not verified) - August 17, 2008 - 20:24
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.