function tripal_launch_job

2.x tripal_core.jobs.api.inc tripal_launch_job($do_parallel = 0, $job_id = NULL, $max_jobs = -1, $single = 0)
3.x tripal.jobs.api.inc tripal_launch_job($do_parallel = 0, $job_id = NULL, $max_jobs = -1, $single = 0)

A function used to manually launch all queued tripal jobs.

Parameters

$do_parallel: A boolean indicating whether jobs should be attempted to run in parallel

$job_id: To launch a specific job provide the job id. This option should be used sparingly as the jobs queue managment system should launch jobs based on order and priority. However there are times when a specific job needs to be launched and this argument will allow it. Only jobs which have not been run previously will run.

$max_jobs: The maximum number of jobs that should be run concurrently. If -1 then unlimited.

$single: Ensures only a single job is run rather then the entire queue.

Related topics

6 calls to tripal_launch_job()
drush_tripal_core_tripal_jobs_launch in legacy/tripal_core/tripal_core.drush.inc
DEPRECATED. Executes jobs in the Tripal Jobs Queue.
drush_tripal_trp_rerun_job in tripal/tripal.drush.inc
Executes jobs in the Tripal Jobs Queue.
drush_tripal_trp_run_jobs in tripal/tripal.drush.inc
Executes jobs in the Tripal Jobs Queue.
drush_tripal_trp_run_jobs_install in tripal/tripal.drush.inc
Executes jobs in the Tripal Jobs Queue.
tripal_execute_job in tripal/api/tripal.jobs.api.inc
Execute a specific Tripal Job.

... See full list

1 string reference to 'tripal_launch_job'

File

tripal/api/tripal.jobs.api.inc, line 347
Tripal offers a job management subsystem for managing tasks that may require an extended period of time for completion.

Code

function tripal_launch_job($do_parallel = 0, $job_id = NULL, $max_jobs = -1, $single = 0) {

  // First check if any jobs are currently running if they are, don't continue,
  // we don't want to have more than one job script running at a time.
  if (!$do_parallel and tripal_is_job_running()) {
    print date('Y-m-d H:i:s') . ": Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel.";
    return;
  }

  if ($do_parallel && tripal_max_jobs_exceeded($max_jobs)) {
    print date('Y-m-d H:i:s') . ": More than $max_jobs jobs are still running. At least one of these jobs much complete before a new job can start.";
    return;
  }

  // Get all jobs that have not started and order them such that they are
  // processed in a FIFO manner.
  if ($job_id) {
    $sql = "
      SELECT TJ.job_id
      FROM {tripal_jobs} TJ
      WHERE
        TJ.start_time IS NULL AND
        TJ.end_time IS NULL AND
        TJ.job_id = :job_id
      ORDER BY priority ASC, job_id ASC
    ";
    $jobs = db_query($sql, array(':job_id' => $job_id));
  }
  else {
    $sql = "
      SELECT TJ.job_id
      FROM {tripal_jobs} TJ
      WHERE
        TJ.start_time IS NULL AND
        TJ.end_time IS NULL AND
        NOT TJ.status = 'Cancelled'
      ORDER BY priority ASC,job_id ASC
    ";
    $jobs = db_query($sql);
  }
  if ($jobs) {
    print date('Y-m-d H:i:s') . ": There are " . $jobs->rowCount() . " jobs queued.\n";
  }

  foreach ($jobs as $jid) {

    $job_id = $jid->job_id;

    // Create the Tripoaljob object.
    $job = new TripalJob();
    $job->load($job_id);

    // We need to do some additional processing for printing since the switch
    // to serialized arrays now allows nested arrays which cause errors when
    // printed using implode alone.
    $args = $job->getArguments();
    $string_args = array();
    foreach ($args as $k => $a) {
      if (is_array($a)) {
        $string_args[$k] = 'Array';
      }
      elseif (is_object($a)) {
        $string_args[$k] = 'Object';
      }
      else {
        $string_args[$k] = $a;
      }
    }

    // Run the job
    $callback = $job->getCallback();
    print date('Y-m-d H:i:s') . ": Calling: $callback(" . implode(", ", $string_args) . ")\n";
    try {
      $job->run();
    }
    catch (Exception $e) {
      $job->logMessage($e->getMessage(), array(), TRIPAL_ERROR);
      drupal_set_message($e->getMessage(), 'error');
    }

    if ($single) {
      // Don't start any more jobs
      break;
    }
    if (tripal_max_jobs_exceeded($max_jobs)) {
      break;
    }

    // TODO: Send an email to the user advising that the job has finished
  }
}