Browse Source

Fixed issue #208

Stephen Ficklin 7 years ago
parent
commit
a21f3505a9
2 changed files with 54 additions and 17 deletions
  1. 19 14
      tripal/api/tripal.jobs.api.inc
  2. 35 3
      tripal/includes/TripalJob.inc

+ 19 - 14
tripal/api/tripal.jobs.api.inc

@@ -84,7 +84,7 @@ function tripal_add_job($job_name, $modulename, $callback, $arguments, $uid,
 
   try {
     $job = new TripalJob();
-    $job->create(array(
+    $is_created = $job->create(array(
       'job_name' => $job_name,
       'modulename' => $modulename,
       'callback' => $callback,
@@ -94,19 +94,24 @@ function tripal_add_job($job_name, $modulename, $callback, $arguments, $uid,
       'includes' => $includes,
     ));
 
-    // If no exceptions were thrown then we know the creation worked.  So
-    // let the user know!
-    drupal_set_message(t("Job '%job_name' submitted.", array('%job_name' => $job_name)));
-
-    // If this is the Tripal admin user then give a bit more information
-    // about how to run the job.
-    if (user_access('administer tripal')) {
-      $jobs_url = url("admin/tripal/tripal_jobs");
-      drupal_set_message(t("Check the <a href='!jobs_url'>jobs page</a> for status.",
-          array('!jobs_url' => $jobs_url)));
-      drupal_set_message(t("You can execute the job queue manually on the command line " .
-          "using the following Drush command: <br>drush trp-run-jobs --username=%uname --root=%base_path",
-          array('%base_path' => DRUPAL_ROOT, '%uname' => $user->name)));
+    if ($is_created) {
+      // If no exceptions were thrown then we know the creation worked.  So
+      // let the user know!
+      drupal_set_message(t("Job '%job_name' submitted.", array('%job_name' => $job_name)));
+
+      // If this is the Tripal admin user then give a bit more information
+      // about how to run the job.
+      if (user_access('administer tripal')) {
+        $jobs_url = url("admin/tripal/tripal_jobs");
+        drupal_set_message(t("Check the <a href='!jobs_url'>jobs page</a> for status.",
+            array('!jobs_url' => $jobs_url)));
+        drupal_set_message(t("You can execute the job queue manually on the command line " .
+            "using the following Drush command: <br>drush trp-run-jobs --username=%uname --root=%base_path",
+            array('%base_path' => DRUPAL_ROOT, '%uname' => $user->name)));
+      }
+    }
+    else {
+      drupal_set_message(t("Job '%job_name' already exists in the queue and was not re-submitted.", array('%job_name' => $job_name)), 'warning');
     }
     return $job->getJobID();
   }

+ 35 - 3
tripal/includes/TripalJob.inc

@@ -89,9 +89,19 @@ class TripalJob {
    *   - includes: An array of paths to files that should be included in order
    *     to execute the job. Use the module_load_include function to get a path
    *     for a given file.
+   *   - ignore_duplicate: (Optional). Set to TRUE to ignore a job if it has
+   *     the same name as another job which has not yet run. If TRUE and a job
+   *     already exists then this object will reference the job already in the
+   *     queue rather than a new submission.  The default is TRUE.
    *
    * @throws Exception
    *   On failure an exception is thrown.
+   *
+   * @return
+   *   Returns TRUE if the job was succesfully created. Returns FALSE otherwise.
+   *   A return of FALSE does not mean the job creation failed. If the
+   *   ignore_duplicate is set to false and the job already is present in the
+   *   queue then the return value will be FALSE.
    */
   public function create($details) {
 
@@ -102,16 +112,22 @@ class TripalJob {
     if (!array_key_exists('includes', $details)) {
       $details['includes'] = array();
     }
+    if (!array_key_exists('ignore_duplicate', $details)) {
+      $details['ignore_duplicate'] = TRUE;
+    }
 
     // Make sure the arguments are correct.
     if (!$details['job_name']) {
-      throw new Exception("Must provide a \$job_name argument to the tripal_add_job() function.");
+      throw new Exception("Must provide a 'job_name' to create a job.");
     }
     if (!$details['modulename']) {
-      throw new Exception("Must provide a \$modulename argument to the tripal_add_job() function.");
+      throw new Exception("Must provide a 'modulename' to create a job.");
     }
     if (!$details['callback']) {
-      throw new Exception("Must provide a \$callback argument to the tripal_add_job() function.");
+      throw new Exception("Must provide a 'callback' to create a job.");
+    }
+    if ($details['ignore_duplicate'] !== FALSE and $details['ignore_duplicate'] !== TRUE) {
+      throw new Exception("Must provide either TRUE or FALSE for the ignore_duplicate option when creating a job.");
     }
 
     $includes = $details['includes'];
@@ -151,6 +167,20 @@ class TripalJob {
     }
 
     try {
+      // Before inserting a new record, and if ignore_duplicates is TRUE then
+      // check to see if the job already exists.
+      if ($details['ignore_duplicate'] === TRUE) {
+        $query = db_select('tripal_jobs', 'tj');
+        $query->fields('tj', array('job_id'));
+        $query->condition('job_name', $details['job_name']);
+        $query->isNull('start_time');
+        $job_id = $query->execute()->fetchField();
+        if ($job_id) {
+          $this->load($job_id);
+          return FALSE;
+        }
+      }
+
       $job_id = db_insert('tripal_jobs')
         ->fields(array(
           'job_name' => $details['job_name'],
@@ -166,6 +196,8 @@ class TripalJob {
         ->execute();
       // Now load the job into this object.
       $this->load($job_id);
+
+      return TRUE;
     }
     catch (Exception $e) {
       throw new Exception('Cannot create job: ' .  $e->getMessage());