Use the new Job Queue to make logic hooks perform faster

bsoremsugar —  August 9, 2012 — 27 Comments

NOTE: Updated to remove before mentioned hack for making job class available, now you can do this via the Ext Framework

I’ve many times over seen logic hooks like the below being used to send out an alert email on save to an Account record.

This is a pretty effective way to send out such an email, but if you are the end user trying to save records you’ll notice it can make the save time a bit longer as the entire send email process in happening synchronously with the save. In other words, in order for the save to finish to email must be successfully sent, which can take a few seconds to complete. Not exactly a great user experience.

The Job Queue, a new developer feature in Sugar 6.5, can help solve this problem. What this enables you to do is to send longer running jobs, such as sending emails, calling web services, or doing other resource intensive jobs that can be done asynchronously to be handled by the cron in the background. To setup the cron to run, follow this support article.

Now let’s look at how to change the above example to take advantage of the Job Queue. First off, let’s re-write the logic hook above to instead push the work to be done into the Job Queue…

The key pieces above are creating the new Job and pushing it into the queue to run.

Now we just need to define the Job. All that you need to do here is create a new class that implements ‘RunnableSchedulerJob’, which requires implementing it’s two abstract methods of setJob() and run(). You put this file in the custom/Extensions/modules/Schedulers/Ext/ScheduledTasks/ directory with the name AccountAlertJob.php

You’ll note in the logic hook where we defined the job we set the $data property to be the record id. This $data property is then passed to the run() call, so we have the context from the logic hook to run the job in the background.

With this, the next time the cron is called, the alert email will be sent out, and all of you users will have much faster record saves.

27 responses to Use the new Job Queue to make logic hooks perform faster


    Love this idea. I’ll be implementing in the next job I have that could utilize it. Might make debugging a bit more difficult, but it sounds like you could just make a traditional logic-hook and then translate it to the Runnable Scheduler Job. Two questions:

    Is there an a way to get entries into custom/include/SugarQueue/SugarCronJobs.php without blowing away the existing copy (e.g. the way we do vardefs and language files)?

    Second: Why haven’t I heard about this before? Is this the first announcement for this feature? I don’t recall reading it in documentation or email or hearing about it in any webinars. It sounds like a super-cool new feature and deserves some more publicity.


      1) Not right now, but I’ve added a bug to get that fixed ( see ). Another way around this is to leverage the Ext framework and define the include in a custom utils.php file. But still it’s a bit hacky, so I’m hoping the bug gets resolved.
      2) It just came out in 6.5, and have been digging into it to pull together a great example on how it’s used. So you can blame that one on me ;-)



    Would there be *any* way of using something like this in a 5.x version of Sugar? We are stuck on this version for several reasons (too many to mention here). What files would I need to port over from the 6.5 CE version to the 5.x version? Or would it be too complicated? Thanks!


    Hi John,you give me this link in a thread that i posted in sugar forums,i want send data by mail and my custom module contains 4 fields,
    so the first step i think is to use the function current_user() to get current user’s data second get the content of the module by using $bean->field_name???
    and what function should i use to send these data???


    Hi John,
    I was playing around with the new Sugar Job queues and I love the idea!
    We often do background processing and email sending in the background.
    We actually built some custom code to do that, especially for emails, that take a while to be processed usually.

    I was going to apply my email queue to workflows as well, so that we have a way to track also the email sent (currently they don’t get saved at all). That would also improve the processing time while saving a record too!

    Is there any plan to convert the email sending functionality of workflows, using the queue instead, and allowing also the optional storing of sent emails, against SugarCRM records?

    In regards to the example above… I was actually not able to get it working on SugarCRM Pro 6.5.4… and I was quite puzzled! Then I found out that there are actually a couple of bugs!
    I’ve just contributed with the fix on SugarCRM’s github.
    Could you verify if it works and let me know how it goes?


    Enrico Simonetti
    InsightfulCRM Australia


    Hi John,

    When the crontab task ends, we get the following php error.

    Class ‘AccountAlertJob’ not found in …..modulesSchedulersJobsSchedulersJob.php on line 532

    The hook works fine, and I can see the job queue ands new job

    Keith Neuendorff April 5, 2013 at 5:41 pm

    I used the example in this blog post for a Sugar 6.5.5 Pro system and it basically did not work. However, the examples in the recently re-done Developers Guide did work. The big issue seems to be that the developer guide version included a line of code to set the $job->assigned_user_id value which seems to be necessary. I also noted that the developer guide version uses “function::AccountAlertJob” instead of “class::AccountAlertJob”. I think the version using the class probably will work but the function way is a little simpler.


      I had the same issue, and looking throughout the source code, I noted that internally, the task must be executed under a user name ($job->assigned_user_id = $bean->assigned_user_id) to allow the SchedulerJobQueue to call the $instance->setJob($job) and $instance->run($data).

      Therefore, to be able to run the job, in the hook must be that line (MISSING) like in:
      // First, let’s create the new job
      $job = new SchedulersJob();
      $job->name = “Account Alert Job – {$bean->name}”;
      $job->data = $bean->id; // key piece, this is data we are passing to the job that it can use to run it.
      $job->target = “class::AccountAlertJob”; // class that has the job to run; implements RunnableSchedulerJob

      // (MISSING) Assign the user that the task will run on behalf of
      $job->assigned_user_id = $bean->assigned_user_id;

      // Now push into the queue to run
      $jq = new SugarJobQueue();
      $jobid = $jq->submitJob($job);

      Hope this helps others.

Trackbacks and Pingbacks:

  1. Troubleshooting an "After Time Elapses" Workflow - August 14, 2012

    […] […]

  2. 6.2.2->6.5.2 upgrade broke my scheduled custom jobs? - August 15, 2012

    […] […]

  3. Send the content of a custom module to the administrator by mail with logic hook??? - August 15, 2012

    […] […]

  4. what am i doing wrong? curl - August 29, 2012

    […] […]

  5. SugarCRM Developer Blog » Blog Archive » Adding your own reoccuring jobs to the Scheduler - September 7, 2012

    […] talked about before on using the new Job Queue for scheduling one time jobs to run. But there are many cases where you have job to run that are reoccurring. Examples of this […]

  6. Can SugarCRM workflows pass data into external database? (integation) - October 3, 2012

    […] […]

  7. Notify record creation to related branch - October 3, 2012

    […] […]

  8. Send mail notification to Admin when user upload a file in Documents Module - January 14, 2013

    […] […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s