function tripal_launch_job

2.x tripal_launch_job($do_parallel = 0, $job_id = NULL, $max_jobs = -1, $single = 0)

A function used to manually launch all queued tripal jobs


$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 tripal_core/
DEPRECATED. Executes jobs in the Tripal Jobs Queue.
drush_tripal_core_trp_rerun_job in tripal_core/
Executes jobs in the Tripal Jobs Queue.
drush_tripal_core_trp_run_jobs in tripal_core/
Executes jobs in the Tripal Jobs Queue.
tripal_execute_job in tripal_core/api/
Execute a specific Tripal Job.
tripal_jobs_launch in tripal_core/api/

... See full list

1 string reference to 'tripal_launch_job'


tripal_core/api/, line 430
Tripal offers a job management subsystem for managing tasks that may require an extended period of time for completion.


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 "Jobs are still running. Use the --parallel=1 option with the Drush command to run jobs in parallel.\n";

  if ($do_parallel && tripal_max_jobs_exceeded($max_jobs)) {
    print "At least $max_jobs jobs are still running. At least one of these jobs much complete before a new job can start.\n";

  // get all jobs that have not started and order them such that
  // they are processed in a FIFO manner.
  if ($job_id) {
    $sql = "SELECT * 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";
    $job_res = db_query($sql, array(':job_id' => $job_id));
  else {
    $sql = "SELECT * FROM {tripal_jobs} TJ " .
      "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL " .
      "ORDER BY priority ASC,job_id ASC";
    $job_res = db_query($sql);
  print "There are " . $job_res->rowCount() . " jobs queued.\n";

  foreach ($job_res as $job) {
    // set the start time for this job
    $record = new stdClass();
    $record->job_id = $job->job_id;
    $record->start_time = time();
    $record->status = 'Running';
    $record->pid = getmypid();
    drupal_write_record('tripal_jobs', $record, 'job_id');

    // call the function provided in the callback column.
    // Add the job_id as the last item in the list of arguments. All
    // callback functions should support this argument.
    $callback = $job->callback;

    // arguments for jobs used to be stored as plain string with a double colon
    // separating them.  But as of Tripal v2.0 the arguments are stored as
    // a serialized array.  To be backwards compatible, we should check for serialization
    // and if not then we will use the old style
    $args = unserialize($job->arguments);
    if (!$args) {
      $args = explode("::", $job->arguments);
    $args[] = $job->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.
    $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;

    print "Calling: $callback(" . implode(", ", $string_args) . ")\n";
    call_user_func_array($callback, $args);
    // set the end time for this job
    $record->end_time = time();
    $record->status = 'Completed';
    $record->progress = '100';
    drupal_write_record('tripal_jobs', $record, 'job_id');

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

    // send an email to the user advising that the job has finished