Suite v7 triggering job queue from logic hook does not run job

I am trying to create a job queue that is triggered from a logic hook.
I have a process that takes time so want it to run in the background, NOT hold up the user interface while it runs.

I have followed the instructions at
https://docs.suitecrm.com/developer/scheduled-tasks/
in the Job Queue section.

The logic hook part works and I have confirmed this by using

echo "<pre>";
var_dump(a variety of variables and arrays);
echo "</pre>";
exit;

in the code. This shows me that the logic hook is setting all the variables it is supposed to.

However, the job is NOT being put into the queue and this has been confirmed by direct SQL calls to the job_queue table and by using the above code in the job itself.

The cron call is working as is confirmed by the Scheduler results for other Scheduled tasks and via direc SQL queries to job_queue

My code is below

Anyone see what I am doing wrong/missing?

The Logic hook call at custom/Extension/modules/{custom_module=AYU_Funds}/Ext/LogicHooks/calculateFundFields.php is working

<?php
$hook_version = 1;
$hook_array = Array();
 // position, file, function
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(
	80,
	'Calculate and store totals for $ and # Tickets for this Event',
	'custom/modules/AYU_Funds/sumEventTotals.php',
	'sumEventTotals',
	'ifNewSumSaveEvent',
);

The logic hook code at custom/{custom_module=AYU_Funds}/sumEventTotals.php to initiate the job in the queue - this is setting all the variables it is supposed to

<?php

class sumEventTotals  {

	function ifNewSumSaveEvent($bean, $event, $arguments) {

        // This system will be storing thousands of records
        // and this logic hook will be looping through them one at a time
        // to calculate the totals for all associated records
        // Rather than run the code real-time inside this logic hook
        // which would take time and impact the user interaction
        // this logic hook creates a background job (Scheduled Task)
        // so the process works in the background
        // allowing the user interaction to proceed unaffected
        // while the code executes
		require_once 'include/SugarQueue/SugarJobQueue.php';
		$sumEventTotalsJob = new SchedulersJob();

		// Give it a useful name
		$sumEventTotalsJob->name = "Sum Total $ and # Tickets for {$bean->module_name}: {$bean->name}";

		// Jobs need an assigned user in order to run
		// I like to use the id of the person requesting the job
		global $current_user;
		$sumEventTotalsJob->assigned_user_id = $current_user->id;

		// Pass the information that our job will need
		$sumEventTotalsJob->data = json_encode(array(
												'module' => $bean->module_name,
												'id' => $bean->id,
											)
										 );

		// Tell the scheduler what class to use
		$sumEventTotalsJob->target = "class::EventTotalsJob";

		// Mark this job for 2 retries
		// This is optional and can be left out if unneeded
		// I leave it in
		$sumEventTotalsJob->requeue = true;
		$sumEventTotalsJob->retry_count = 2;

		$queue = new SugarJobQueue();
		$queue->submitJob($sumEventTotalsJob);
	}
}

The job queue setup at custom/Extension/modules/Schedulers/Ext/ScheduledTasks/EventTotalsJob.php is NOT being triggered (simple code just to test the queue)

<?php

class EventTotalsJob implements RunnableSchedulerJob {

	public function run($callingFundRaiser) {

		// Only different part of the job code
		// compared to what it would look like in a logic hook
		// Grab the bean using the supplied arguments.
		$fundRaiserArray = json_decode($callingFundRaiser,1);
		$fundBean = BeanFactory::getBean($fundRaiserArray['module'],$fundRaiserArray['id']);

		// Add code as you would have added in a logic hook
        $fundBean->description = "testing the background job";
        $fundBean->save();

	}

	// Close off the class by submitting the job
	// which will run in the background, not impeding user interactions
	public function setJob(SchedulersJob $job) {
		$this->job = $job;
	}
}

Anyone see anything I am doing wrong / missing?

You don’t say where you put that final file.

Also, a QR&R will be needed for that Extension file to be picked up.

@pgr

Sorry for the omission re the file locations - I edited the initial post to correct
And yes, I did a QR&R after each attempt - no change

The file locations are (not sure I am using the correct terminology here):

  • For the calling file: custom/Extension/modules/{custom_module}/EXT/LogicHooks/
  • For the file called: custom/modules/{custom_module}/
  • For the last file (the schedule queue?): custom/Extension/modules/Schedulers/Ext/SheduledTasks

I was reading
https://docs.suitecrm.com/developer/logic-hooks/
where it said the job queue hooks go into custom/modules/logic_hooks.php

But that seems like it is intended for Scheduled job, not the ones called by module logic hookssince there is nowhere to specific for which module the “before_save” or “after_save” or … applies

Thoughts?

Your locations seem ok

You can check that the QR&R picked it up by looking for the consolidated extensions file under custom/modules/Schedulers/Ext somewhere (if I am not mistaken)

All the pieces are captured in the Ext location

But it is not running the background job queue

I’ll play with it some more tomorrow, but if you have any ideas, they would be appreciated

@pgr

One unknown answered

The hook for job queue jobs does NOT go in custom/modules/logic_hooks.php

The instructions at

where talking about two hooks that triggered when scheduled (not job queue) jobs failed to run. They used two specific hooks (ie not after_save, before_save, …)
job_failure
job_failure_retry

It is explained fairly well at
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_11.0/Architecture/Logic_Hooks/Job_Queue_Hooks/job_failure/
(too bad we still depend on Sugar documentation …)

So, I am following EXACTLY the instructions at

but it is not working

The hook works
the logic called by the hook is being triggered
but the final code at custom/Extension/modules/Schedulers/Ext/SheduledTasks is not even being called - as is confirmed by putting test outputs and by direct sql queries to the database

SELECT * FROM job_queue WHERE target = 'class::EventTotalsJob';

Any ideas?
Anyone?

Try having a look at the job_queue table in the database.

Check that in Admin / Schedulers your jobs show a recent “last ran successfully” date, and in the correct timezone. Your schedulers might all be stuck, not just this one.

@pgr

Why is it that just when you think someone is being really annoying by telling you to do something you have done a dozen times, you realize that what they are saying helps you solve the puzzle? :slight_smile:

And unfortunately, this confession forces me to admit I am an idiot …

When I was doing direct enquiries on the MySQL database to see if my job was running, I was using the WRONG database! I had previously given the use … command for another database and not changed that to the database for the instance of SuiteCRM on which I was working.

Son of a gun, once I selected the right database, I had all kinds of instances of the job queue running.

Jeez …

And thank you @pgr for sticking with me.

For anyone else, if you want to run logic in the background based on a logic hook call, the above setup works fine.

aaarrrggghhh!

1 Like