tripal_bulk_loader.drush.inc

  1. 2.x tripal_bulk_loader/tripal_bulk_loader.drush.inc
  2. 3.x tripal_bulk_loader/tripal_bulk_loader.drush.inc
  3. 1.x tripal_bulk_loader/tripal_bulk_loader.drush.inc

Implements drush integration for this module

File

tripal_bulk_loader/tripal_bulk_loader.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Implements drush integration for this module
  5. *
  6. * @ingroup tripal_bulk_loader
  7. */
  8. /**
  9. * Implements hook_drush_command().
  10. *
  11. * @ingroup tripal_bulk_loader
  12. */
  13. function tripal_bulk_loader_drush_command() {
  14. $items = array();
  15. $items['tripal-loader-progress'] = array(
  16. 'description' => dt('Display the progress of any running tripal bulk loading job.'),
  17. 'aliases' => array('trpload-%'),
  18. );
  19. $items['tripal-loader-view'] = array(
  20. // used by drush help
  21. 'description' => dt('Returns the status/details of the specified bulk loading job.'),
  22. 'arguments' => array(
  23. 'nid' => dt('The Node ID of the bulk Loading Job')
  24. ),
  25. 'examples' => array(
  26. 'Standard Example' => 'drush tripal-loader-view 5433',
  27. ),
  28. 'aliases' => array('trpload-view')
  29. );
  30. $items['tripal-loader-cancel'] = array(
  31. // used by drush help
  32. 'description' => dt('Cancels the specified bulk loading job.'),
  33. 'arguments' => array(
  34. 'nid' => dt('The Node ID of the bulk Loading Job')
  35. ),
  36. 'examples' => array(
  37. 'Standard Example' => 'drush tripal-loader-cancel 5433',
  38. ),
  39. 'aliases' => array('trpload-cncl')
  40. );
  41. $items['tripal-loader-submit'] = array(
  42. // used by drush help
  43. 'description' => dt('Submit or Re-submit the given bulk loading job.'),
  44. 'arguments' => array(
  45. 'nid' => dt('The Node ID of the bulk Loading Job')
  46. ),
  47. 'examples' => array(
  48. 'Standard Example' => 'drush tripal-loader-submit 5433',
  49. ),
  50. 'aliases' => array('trpload-sbmt')
  51. );
  52. $items['tripal-loader-revert'] = array(
  53. // used by drush help
  54. '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.'),
  55. 'arguments' => array(
  56. 'nid' => dt('The Node ID of the bulk Loading Job')
  57. ),
  58. 'examples' => array(
  59. 'Standard Example' => 'drush tripal-loader-revert 5433',
  60. ),
  61. 'aliases' => array('trpload-revert')
  62. );
  63. return $items;
  64. }
  65. /**
  66. * Code ran for the tripal-loader-progress drush command
  67. * Display the progress of any running tripal bulk loading job.
  68. *
  69. * @ingroup tripal_bulk_loader
  70. */
  71. function drush_tripal_bulk_loader_tripal_loader_progress() {
  72. // determine the progress of any loading jobs
  73. $sql = "SELECT t.loader_name, t.file, t.job_id FROM {tripal_bulk_loader} t WHERE job_status='Loading...'";
  74. $resource = db_query($sql);
  75. while ($r = db_fetch_object($resource)) {
  76. if ($r->job_id) {
  77. $progress = tripal_bulk_loader_progess_file_get_progress($r->job_id);
  78. if ($progress->num_records > 0 AND $progress->total_percent < 100) {
  79. drush_print(
  80. $r->loader_name . "\n"
  81. . str_repeat("-", 40) . "\n"
  82. . "File:" . $r->file . "\n"
  83. . "Current Constant Set:\n"
  84. . "\tLines processed: " . $progress->num_lines . "\n"
  85. . "\tRecord Inserted: " . $progress->num_records . "\n"
  86. . "\tPercent Complete: " . $progress->percent_file . "\n"
  87. . "Number of Constant Sets fully loaded: " . $progress->num_constant_sets_loaded . "\n"
  88. . "Job Percent Complete: " . $progress->total_percent . "\n"
  89. );
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Returns the status/details of the specified bulk loading job.
  96. *
  97. * @param $nid
  98. * The Node ID of the bulk Loading Job
  99. *
  100. * @ingroup tripal_bulk_loader
  101. */
  102. function drush_tripal_bulk_loader_tripal_loader_view ($nid) {
  103. $node = node_load($nid);
  104. $author = user_load($node->uid);
  105. drush_print("Job Name: ".$node->loader_name);
  106. drush_print("Submitted By: ".$author->name);
  107. drush_print("Job Creation Date: ".format_date($node->created));
  108. drush_print("Last Updated: ".format_date($node->changed));
  109. drush_print("Template Name: ".$node->template->name);
  110. drush_print("Data File: ".$node->file);
  111. drush_print("Job Status: ".$node->job_status);
  112. }
  113. /**
  114. * Cancels the specified bulk loading job.
  115. *
  116. * @param $nid
  117. * The Node ID of the bulk Loading Job
  118. *
  119. * @ingroup tripal_bulk_loader
  120. */
  121. function drush_tripal_bulk_loader_tripal_loader_cancel ($nid) {
  122. $node = node_load($nid);
  123. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Job Cancelled', $node->nid);
  124. tripal_cancel_job($node->job_id,FALSE);
  125. }
  126. /**
  127. * Submit or Re-submit the given bulk loading job.
  128. *
  129. * @param $nid
  130. * The Node ID of the bulk Loading Job
  131. *
  132. * @ingroup tripal_bulk_loader
  133. */
  134. function drush_tripal_bulk_loader_tripal_loader_submit ($nid) {
  135. global $user;
  136. if ($node->job_id) {
  137. //Re-submit Tripal Job
  138. tripal_rerun_job($node->job_id);
  139. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
  140. }
  141. else {
  142. //Submit Tripal Job
  143. $node= node_load($nid);
  144. $job_args[1] = $nid;
  145. if (is_readable($node->file)) {
  146. $fname = basename($node->file);
  147. $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  148. // add job_id to bulk_loader node
  149. $success = db_query("UPDATE {tripal_bulk_loader} SET job_id=%d WHERE nid=%d", $job_id, $nid);
  150. // change status
  151. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
  152. }
  153. else {
  154. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $node->file)));
  155. }
  156. }
  157. }
  158. /**
  159. * Revert the records loaded by the last run of the specified loading job. This is only
  160. * available if the specified loading job is keeping track of inserted IDs.
  161. *
  162. * @param $nid
  163. * The Node ID of the bulk Loading Job
  164. *
  165. * @ingroup tripal_bulk_loader
  166. */
  167. function drush_tripal_bulk_loader_tripal_loader_revert ($nid) {
  168. // Remove the records from the database that were already inserted
  169. $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d ORDER BY tripal_bulk_loader_inserted_id DESC', $nid);
  170. while ($r = db_fetch_object($resource)) {
  171. $ids = preg_split('/,/', $r->ids_inserted);
  172. db_query('DELETE FROM {%s} WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted);
  173. $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));
  174. if (!$result->present) {
  175. drush_print('Successfully Removed data Inserted into the '.$r->table_inserted_into.' table.');
  176. db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=%d', $r->tripal_bulk_loader_inserted_id);
  177. }
  178. else {
  179. drush_print('Unable to remove data Inserted into the '.$r->table_inserted_into.' table!');
  180. }
  181. }
  182. // reset status
  183. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Reverted -Data Deleted', $nid);
  184. }

Related topics