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?