Browse Source

Added drush functions for bulk loader: tripal-loader-view, tripal-loader-cancel, tripal-loader-submit, tripal-loader-revert

Lacey Sanderson 12 years ago
parent
commit
db13ec1fcb
2 changed files with 176 additions and 34 deletions
  1. 140 0
      tripal_bulk_loader/tripal_bulk_loader.drush.inc
  2. 36 34
      tripal_core/includes/jobs.php

+ 140 - 0
tripal_bulk_loader/tripal_bulk_loader.drush.inc

@@ -14,6 +14,51 @@ function tripal_bulk_loader_drush_command() {
     'description' => dt('Display the progress of any running tripal bulk loading job.'),
     'description' => dt('Display the progress of any running tripal bulk loading job.'),
     'aliases' => array('trpload-%'),
     'aliases' => array('trpload-%'),
   );
   );
+  $items['tripal-loader-view'] = array(
+    // used by drush help
+    'description' => dt('Returns the status/details of the specified bulk loading job.'),
+    'arguments' => array(
+      'nid' => dt('The Node ID of the bulk Loading Job')
+    ),
+    'examples' => array(
+      'Standard Example' => 'drush tripal-loader-view 5433',
+    ),
+    'aliases' => array('trpload-view')
+  );
+  $items['tripal-loader-cancel'] = array(
+    // used by drush help
+    'description' => dt('Cancels the specified bulk loading job.'),
+    'arguments' => array(
+      'nid' => dt('The Node ID of the bulk Loading Job')
+    ),
+    'examples' => array(
+      'Standard Example' => 'drush tripal-loader-cancel 5433',
+    ),
+    'aliases' => array('trpload-cncl')
+  );
+  $items['tripal-loader-submit'] = array(
+    // used by drush help
+    'description' => dt('Submit or Re-submit the given bulk loading job.'),
+    'arguments' => array(
+      'nid' => dt('The Node ID of the bulk Loading Job')
+    ),
+    'examples' => array(
+      'Standard Example' => 'drush tripal-loader-submit 5433',
+    ),
+    'aliases' => array('trpload-sbmt')
+  );
+  $items['tripal-loader-revert'] = array(
+    // used by drush help
+    'description' => dt('Revert the records loaded by the last run of the specified loading job. This is only available if the specified loading job is keeping track of inserted IDs.'),
+    'arguments' => array(
+      'nid' => dt('The Node ID of the bulk Loading Job')
+    ),
+    'examples' => array(
+      'Standard Example' => 'drush tripal-loader-revert 5433',
+    ),
+    'aliases' => array('trpload-revert')
+  );
+
   return $items;
   return $items;
 }
 }
 
 
@@ -46,3 +91,98 @@ function drush_tripal_bulk_loader_tripal_loader_progress() {
     }
     }
   }
   }
 }
 }
+
+/**
+ * Returns the status/details of the specified bulk loading job.
+ *
+ * @param $nid
+ *   The Node ID of the bulk Loading Job
+ */
+function drush_tripal_bulk_loader_tripal_loader_view ($nid) {
+  $node = node_load($nid);
+  $author = user_load($node->uid);
+
+  drush_print("Job Name: ".$node->loader_name);
+  drush_print("Submitted By: ".$author->name);
+  drush_print("Job Creation Date: ".format_date($node->created));
+  drush_print("Last Updated: ".format_date($node->changed));
+  drush_print("Template Name: ".$node->template->name);
+  drush_print("Data File: ".$node->file);
+  drush_print("Job Status: ".$node->job_status);
+}
+
+/**
+ * Cancels the specified bulk loading job.
+ *
+ * @param $nid
+ *   The Node ID of the bulk Loading Job
+ */
+function drush_tripal_bulk_loader_tripal_loader_cancel ($nid) {
+  $node = node_load($nid);
+  db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Job Cancelled', $node->nid);
+  tripal_jobs_cancel($node->job_id,FALSE);
+}
+
+/**
+ * Submit or Re-submit the given bulk loading job.
+ *
+ * @param $nid
+ *   The Node ID of the bulk Loading Job
+ */
+function drush_tripal_bulk_loader_tripal_loader_submit ($nid) {
+  global $user;
+
+  if ($node->job_id) {
+    //Re-submit Tripal Job
+    tripal_jobs_rerun($node->job_id);
+    db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
+  }
+  else {
+    //Submit Tripal Job
+    $node= node_load($nid);
+    $job_args[1] = $nid;
+    if (is_readable($node->file)) {
+      $fname = basename($node->file);
+      $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
+
+      // add job_id to bulk_loader node
+      $success = db_query("UPDATE {tripal_bulk_loader} SET job_id=%d WHERE nid=%d", $job_id, $nid);
+
+      // change status
+      db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
+    }
+    else {
+      drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
+    }
+  }
+
+}
+
+/**
+ * Revert the records loaded by the last run of the specified loading job. This is only
+ * available if the specified loading job is keeping track of inserted IDs.
+ *
+ * @param $nid
+ *   The Node ID of the bulk Loading Job
+ */
+function drush_tripal_bulk_loader_tripal_loader_revert ($nid) {
+
+  // Remove the records from the database that were already inserted
+  $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d ORDER BY tripal_bulk_loader_inserted_id DESC', $node->nid);
+  while ($r = db_fetch_object($resource)) {
+    $ids = preg_split('/,/', $r->ids_inserted);
+    db_query('DELETE FROM %s WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted);
+    $result = db_fetch_object(db_query('SELECT true as present FROM %s WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted));
+    if (!$result->present) {
+      drupal_set_message(t('Successfully Removed data Inserted into the %tableto table.', array('%tableto' => $r->table_inserted_into)));
+      db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=%d', $r->tripal_bulk_loader_inserted_id);
+    }
+    else {
+      drupal_set_message(t('Unable to remove data Inserted into the %tableto table!', array('%tableto' => $r->table_inserted_into)), 'error');
+    }
+  }
+
+  // reset status
+  db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Reverted -Data Deleted', $node->nid);
+
+}

+ 36 - 34
tripal_core/includes/jobs.php

@@ -137,14 +137,14 @@ function tripal_get_module_active_jobs($modulename) {
  */
  */
 function tripal_jobs_report_form($form, &$form_state = NULL) {
 function tripal_jobs_report_form($form, &$form_state = NULL) {
   $form = array();
   $form = array();
-	
+
   // set the default values
   // set the default values
   $default_status = $form_state['values']['job_status'];
   $default_status = $form_state['values']['job_status'];
-  
+
   if (!$default_status) {
   if (!$default_status) {
     $default_status = $_SESSION['tripal_job_status_filter'];
     $default_status = $_SESSION['tripal_job_status_filter'];
-  }    
-  
+  }
+
   $form['job_status'] = array(
   $form['job_status'] = array(
     '#type'          => 'select',
     '#type'          => 'select',
     '#title'         => t('Filter by Job Status'),
     '#title'         => t('Filter by Job Status'),
@@ -153,17 +153,17 @@ function tripal_jobs_report_form($form, &$form_state = NULL) {
 	    0           => 'All Jobs',
 	    0           => 'All Jobs',
 	    'Running'   => 'Running',
 	    'Running'   => 'Running',
 	    'Waiting'   => 'Waiting',
 	    'Waiting'   => 'Waiting',
-	    'Completed' => 'Completed',    
-	    'Cancelled' => 'Cancelled', 
-	    'Error'     => 'Error',  
+	    'Completed' => 'Completed',
+	    'Cancelled' => 'Cancelled',
+	    'Error'     => 'Error',
 	  ),
 	  ),
   );
   );
-  
+
   $form['submit'] = array(
   $form['submit'] = array(
     '#type'         => 'submit',
     '#type'         => 'submit',
     '#value'        => t('Filter'),
     '#value'        => t('Filter'),
   );
   );
-  return $form;  
+  return $form;
 }
 }
 /**
 /**
  *
  *
@@ -171,7 +171,7 @@ function tripal_jobs_report_form($form, &$form_state = NULL) {
  */
  */
 function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
 function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
   $job_status = $form_state['values']['job_status'];
   $job_status = $form_state['values']['job_status'];
-  $_SESSION['tripal_job_status_filter'] = $job_status;    
+  $_SESSION['tripal_job_status_filter'] = $job_status;
 }
 }
 /**
 /**
  * Returns the Tripal Job Report
  * Returns the Tripal Job Report
@@ -183,14 +183,14 @@ function tripal_jobs_report_form_submit($form, &$form_state = NULL) {
  */
  */
 function tripal_jobs_report() {
 function tripal_jobs_report() {
 
 
-  // run the following function which will 
+  // run the following function which will
   // change the status of jobs that have errored out
   // change the status of jobs that have errored out
   tripal_jobs_check_running();
   tripal_jobs_check_running();
-  
+
 	$jobs_status_filter = $_SESSION['tripal_job_status_filter'];
 	$jobs_status_filter = $_SESSION['tripal_job_status_filter'];
-  
+
   $sql = "
   $sql = "
-    SELECT 
+    SELECT
       TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
       TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
       TJ.status as job_status, TJ,submit_date,TJ.start_time,
       TJ.status as job_status, TJ,submit_date,TJ.start_time,
       TJ.end_time,TJ.priority,U.name as username
       TJ.end_time,TJ.priority,U.name as username
@@ -200,19 +200,19 @@ function tripal_jobs_report() {
     $sql .= "WHERE TJ.status = '%s' ";
     $sql .= "WHERE TJ.status = '%s' ";
   }
   }
   $sql .= "ORDER BY job_id DESC";
   $sql .= "ORDER BY job_id DESC";
-  
+
   $jobs = pager_query($sql, 25, 0, "SELECT count(*) FROM ($sql) as t1", $jobs_status_filter);
   $jobs = pager_query($sql, 25, 0, "SELECT count(*) FROM ($sql) as t1", $jobs_status_filter);
   $header = array(
   $header = array(
-    'Job ID', 
-    'User', 
-    'Job Name', 
-    array('data' => 'Dates', 'style'=> "white-space: nowrap"), 
-    'Priority', 
-    'Progress', 
-    'Status', 
+    'Job ID',
+    'User',
+    'Job Name',
+    array('data' => 'Dates', 'style'=> "white-space: nowrap"),
+    'Priority',
+    'Progress',
+    'Status',
     'Action');
     'Action');
-  $rows = array();  
-  
+  $rows = array();
+
   // iterate through the jobs
   // iterate through the jobs
   while ($job = db_fetch_object($jobs)) {
   while ($job = db_fetch_object($jobs)) {
     $submit = tripal_jobs_get_submit_date($job);
     $submit = tripal_jobs_get_submit_date($job);
@@ -235,7 +235,7 @@ function tripal_jobs_report() {
       "$cancel_link $rerun_link $view_link",
       "$cancel_link $rerun_link $view_link",
     );
     );
   }
   }
-  
+
   // create the report page
   // create the report page
   $output .= "Waiting jobs are executed first by priority level (the lower the ".
   $output .= "Waiting jobs are executed first by priority level (the lower the ".
              "number the higher the priority) and second by the order they ".
              "number the higher the priority) and second by the order they ".
@@ -321,7 +321,7 @@ function tripal_jobs_get_submit_date($job) {
  *   To launch a specific job provide the job id.  This option should be
  *   To launch a specific job provide the job id.  This option should be
  *   used sparingly as the jobs queue managment system should launch jobs
  *   used sparingly as the jobs queue managment system should launch jobs
  *   based on order and priority.  However there are times when a specific
  *   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 
+ *   job needs to be launched and this argument will allow it.  Only jobs
  *   which have not been run previously will run.
  *   which have not been run previously will run.
  *
  *
  * @ingroup tripal_jobs_api
  * @ingroup tripal_jobs_api
@@ -343,7 +343,7 @@ function tripal_jobs_launch($do_parallel = 0, $job_id = NULL) {
             "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = %d ".
             "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL and TJ.job_id = %d ".
             "ORDER BY priority ASC,job_id ASC";
             "ORDER BY priority ASC,job_id ASC";
     $job_res = db_query($sql,$job_id);
     $job_res = db_query($sql,$job_id);
-  } 
+  }
   else {
   else {
     $sql =  "SELECT * FROM {tripal_jobs} TJ ".
     $sql =  "SELECT * FROM {tripal_jobs} TJ ".
             "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
             "WHERE TJ.start_time IS NULL and TJ.end_time IS NULL ".
@@ -496,13 +496,13 @@ function tripal_jobs_rerun($job_id, $goto_jobs_page = TRUE) {
   $job = db_fetch_object(db_query($sql, $job_id));
   $job = db_fetch_object(db_query($sql, $job_id));
   $args = explode("::", $job->arguments);
   $args = explode("::", $job->arguments);
   $job_id = tripal_add_job(
   $job_id = tripal_add_job(
-    $job->job_name, 
-    $job->modulename, 
-    $job->callback, 
-    $args, 
+    $job->job_name,
+    $job->modulename,
+    $job->callback,
+    $args,
     $user->uid,
     $user->uid,
     $job->priority);
     $job->priority);
-    
+
   if ($goto_jobs_page) {
   if ($goto_jobs_page) {
     drupal_goto("admin/tripal/tripal_jobs");
     drupal_goto("admin/tripal/tripal_jobs");
   }
   }
@@ -517,7 +517,7 @@ function tripal_jobs_rerun($job_id, $goto_jobs_page = TRUE) {
  *
  *
  * @ingroup tripal_jobs_api
  * @ingroup tripal_jobs_api
  */
  */
-function tripal_jobs_cancel($job_id) {
+function tripal_jobs_cancel($job_id, $redirect = TRUE) {
   $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = %d";
   $sql = "SELECT * FROM {tripal_jobs} WHERE job_id = %d";
   $job = db_fetch_object(db_query($sql, $job_id));
   $job = db_fetch_object(db_query($sql, $job_id));
 
 
@@ -534,5 +534,7 @@ function tripal_jobs_cancel($job_id) {
   else {
   else {
     drupal_set_message(t("Job %job_id cannot be cancelled. It is in progress or has finished.", array('%job_id' => $job_id)));
     drupal_set_message(t("Job %job_id cannot be cancelled. It is in progress or has finished.", array('%job_id' => $job_id)));
   }
   }
-  drupal_goto("admin/tripal/tripal_jobs");
+  if ($redirect) {
+    drupal_goto("admin/tripal/tripal_jobs");
+  }
 }
 }