Collections functions
General functions
Render functions
Theme permission functions
Resource functions

job_queue_run_job()

Description

Run offline job

Parameters

ColumnTypeDefaultDescription
$job array Metadata of the queued job as returned by job_queue_get_jobs()
$clear_process_lock boolean Clear process lock for this job

Return

void

Location

include/job_functions.php lines 264 to 370

Definition

 
function job_queue_run_job($job$clear_process_lock)
    {
    
// Runs offline job using defined job handler
    
$jobref $job["ref"];
    
$job_data=json_decode($job["job_data"], true);

    
$jobuser $job["user"];
    if (!isset(
$jobuser) || $jobuser == || $jobuser == "")
        {
        
$logmessage " - Job could not be run as no user was supplied #{$jobref}PHP_EOL;
        echo 
$logmessage;
        
debug($logmessage);
        
job_queue_update($jobref,$job_data,STATUS_ERROR);
        return;
        }

    
$jobuserdata get_user($jobuser);
    
setup_user($jobuserdata);
    
$job_success_text=$job["success_text"];
    
$job_failure_text=$job["failure_text"];

    
// Variable used to avoid spinning off offline jobs from an already existing job.
    // Example: create_previews() is using extract_text() and both can run offline.
    
global $offline_job_in_progress$plugins;
    
$offline_job_in_progress false;

    if(
is_process_lock('job_' $jobref) && !$clear_process_lock)
        {
        
$logmessage =  " - Process lock for job #{$jobref}PHP_EOL;
        echo 
$logmessage;
        
debug($logmessage);
        return;
        }
    else if(
$clear_process_lock)
        {
        
$logmessage =  " - Clearing process lock for job #{$jobref}PHP_EOL;
        echo 
$logmessage;
        
debug($logmessage);
        
clear_process_lock("job_{$jobref}");
        }
    
    
set_process_lock('job_' $jobref);
    
    
$logmessage =  "Running job #" $jobref PHP_EOL;
    echo 
$logmessage;
    
debug($logmessage);

    
$logmessage =  " - Looking for " __DIR__ "/job_handlers/" $job["type"] . ".php" PHP_EOL;
    echo 
$logmessage;
    
debug($logmessage);

    if (
file_exists(__DIR__ "/job_handlers/" $job["type"] . ".php"))
        {
        
$logmessage=" - Attempting to run job #" $jobref " using handler " $job["type"]. PHP_EOL;
        echo 
$logmessage;
        
debug($logmessage);
        
job_queue_update($jobref$job_data,STATUS_INPROGRESS);
        
$offline_job_in_progress true;
        include 
__DIR__ "/job_handlers/" $job["type"] . ".php";
        
// Update to mark job as complete and reset priority to the default according to job type
        
job_queue_update($jobref$job_data,STATUS_COMPLETE,date('Y-m-d H:i:s'),JOB_PRIORITY_COMPLETED);
        }
    else
        {
        
// Check for handler in plugin
        
$offline_plugins $plugins;

        
// Include plugins for this job user's group
        
$group_plugins ps_query("SELECT name, config, config_json, disable_group_select FROM plugins WHERE inst_version >= 0 AND disable_group_select = 0 AND find_in_set(?,enabled_groups) ORDER BY priority", array("i",$jobuserdata["usergroup"]), "plugins");
        foreach(
$group_plugins as $group_plugin)
            {
            
include_plugin_config($group_plugin['name'],$group_plugin['config'],$group_plugin['config_json']);
            
register_plugin($group_plugin['name']);
            
register_plugin_language($group_plugin['name']);
            
$offline_plugins[]=$group_plugin['name'];
            }    

        foreach(
$offline_plugins as $plugin)
            {
            if (
file_exists(__DIR__ "/../plugins/" $plugin "/job_handlers/" $job["type"] . ".php"))
                {
                
$logmessage=" - Attempting to run job #" $jobref " using handler " $job["type"]. PHP_EOL;
                echo 
$logmessage;
                
debug($logmessage);
                
job_queue_update($jobref$job_data,STATUS_INPROGRESS);
                
$offline_job_in_progress true;
                include 
__DIR__ "/../plugins/" $plugin "/job_handlers/" $job["type"] . ".php";
                
job_queue_update($jobref$job_data,STATUS_COMPLETE,date('Y-m-d H:i:s'),JOB_PRIORITY_COMPLETED);
                break;
                }
            }
        }
    
    if(!
$offline_job_in_progress)
        {
        
$logmessage="Unable to find handlerfile: " $job["type"]. PHP_EOL;
        echo 
$logmessage;
        
debug($logmessage);
        
job_queue_update($jobref,$job_data,STATUS_ERROR,date('Y-m-d H:i:s'));
        }
    
    
$logmessage =  " - Finished job #" $jobref PHP_EOL;
    echo 
$logmessage;
    
debug($logmessage);
    
    
clear_process_lock('job_' $jobref);
    }

This article was last updated 2nd October 2022 02:05 Europe/London time based on the source file dated 1st July 2022 11:05 Europe/London time.