Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 261 to 375

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);
    if (!
$jobuserdata)
        {
        
$logmessage " - Job #{$jobref} could not be run as invalid user ref #{$jobuser} was supplied." PHP_EOL;
        echo 
$logmessage;
        
debug($logmessage);
        
job_queue_update($jobref,$job_data,STATUS_ERROR);
        return;
        }
    
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;
        }
    elseif(
$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 ' at ' date('Y-m-d H:i:s') . 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 ' at ' date('Y-m-d H:i:s') . PHP_EOL;
    echo 
$logmessage;
    
debug($logmessage);
    
    
clear_process_lock('job_' $jobref);
    }

This article was last updated 5th December 2024 08:05 Europe/London time based on the source file dated 18th October 2024 11:40 Europe/London time.