function tripal_max_jobs_exceeded

2.x tripal_core.jobs.api.inc tripal_max_jobs_exceeded($max_jobs)
3.x tripal.jobs.api.inc tripal_max_jobs_exceeded($max_jobs)

Check for too many concurrent jobs

Parameters

$max_jobs: The maximum number of concurrent jobs to allow; -1 = no limit

Related topics

1 call to tripal_max_jobs_exceeded()
tripal_launch_job in tripal_core/api/tripal_core.jobs.api.inc
A function used to manually launch all queued tripal jobs

File

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

Code

function tripal_max_jobs_exceeded($max_jobs) {
  if ($max_jobs < 0) {
    // No limit on concurrent jobs
    return FALSE;
  }

  $num_jobs_running = 0;

  // Iterate through each job that has not ended and see if it is still running.
  // If it is not running but does not have an end_time then set the end time
  // and set the status to 'Error'
  $sql = "SELECT * FROM {tripal_jobs} TJ " .
    "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  $jobs = db_query($sql);
  foreach ($jobs as $job) {
    $status = shell_exec('ps -p ' . escapeshellarg($job->pid) . ' -o pid=');
    if ($job->pid && $status) {
      // the job is still running
      $num_jobs_running++;
    }
    else {
      // the job is not running so terminate it
      $record = new stdClass();
      $record->job_id = $job->job_id;
      $record->end_time = time();
      $record->status = 'Error';
      $record->error_msg = 'Job has terminated unexpectedly.';
      drupal_write_record('tripal_jobs', $record, 'job_id');
    }
  }

  return ($num_jobs_running >= $max_jobs);
}