tripal_core.drush.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * Contains function relating to drush-integration of this module.
  5. */
  6. /**
  7. * Describes each drush command implemented by the module
  8. *
  9. * @return
  10. * The first line of description when executing the help for a given command
  11. */
  12. function tripal_core_drush_help($command) {
  13. switch ($command) {
  14. case 'drush:tripal-current-job':
  15. return dt('Returns details about the currently running tripal job including percent complete.');
  16. case 'drush:tripal-update-mview':
  17. return dt('Updates the specified materialized view.');
  18. }
  19. }
  20. /**
  21. * Registers a drush command and constructs the full help for that command
  22. *
  23. * @return
  24. * And array of command descriptions
  25. */
  26. function tripal_core_drush_command() {
  27. $items = array();
  28. $items['tripal-current-job'] = array(
  29. 'description' => dt('Returns details about the currently running tripal job including percent complete.'),
  30. 'arguments' => array(
  31. ),
  32. 'examples' => array(
  33. 'Standard example' => 'drush tripal-current-job',
  34. ),
  35. 'aliases' => array('trpjob-cur'),
  36. );
  37. $items['tripal-update-mview'] = array(
  38. // used by drush help
  39. 'description' => dt('Updates the specified materialized view.'),
  40. 'arguments' => array(
  41. 'mview_id' => dt('The ID of the materialized view to update'),
  42. 'table_name' => dt('The name of the materialized view table to update.'),
  43. ),
  44. 'examples' => array(
  45. 'By Materialized View ID' => 'drush tripal-update-mview --mview_id=5',
  46. 'By Table Name' => 'drush tripal-update-mview --table_name=organism_feature_count'
  47. ),
  48. // supply options
  49. 'options' => array(
  50. 'mview_id',
  51. 'table_name'
  52. ),
  53. 'aliases' => array('trpmv-up')
  54. );
  55. $items['tripal-launch-jobs'] = array(
  56. // used by drush help
  57. 'description' => dt('Lauches any jobs waiting in the queue.'),
  58. 'examples' => array(
  59. 'Normal Job' => 'drush tripal-launch-jobs admin',
  60. 'Parallel Job' => 'drush tripal-launch-jobs admin --parallel=1'
  61. ),
  62. 'arguments' => array(
  63. 'user' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  64. ),
  65. // supply options
  66. 'options' => array(
  67. 'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
  68. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  69. ),
  70. 'aliases' => array('trpjob-run')
  71. );
  72. $items['tripal-rerun-job'] = array(
  73. // used by drush help
  74. 'description' => dt('Rerun any job in the queue.'),
  75. 'examples' => array(
  76. 'Normal Job' => 'drush tripal-rerun-job admin 2',
  77. 'Parallel Job' => 'drush tripal-rerun-job admin 2 --parallel=1'
  78. ),
  79. 'arguments' => array(
  80. 'user' => dt('The Drupal username under which the job should be run. The permissions for this user will be used.'),
  81. 'job_id' => dt('The job ID to run.'),
  82. ),
  83. // supply options
  84. 'options' => array(
  85. 'parallel' => dt('Normally jobs are executed one at a time. But if you are certain no conflicts will occur with other currently running jobs you may set this argument to a value of 1 to make the job run in parallel with other running jobs.'),
  86. ),
  87. 'aliases' => array('trpjob-rerun')
  88. );
  89. return $items;
  90. }
  91. /**
  92. * Executes jobs in the Tripal Jobs Queue
  93. *
  94. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  95. */
  96. function drush_tripal_core_tripal_launch_jobs($username) {
  97. $parallel = drush_get_option('parallel');
  98. $job_id = drush_get_option('job_id');
  99. if ($username) {
  100. global $user;
  101. $user = user_load(array('name' => $username));
  102. }
  103. else {
  104. drush_print('ERROR: Please provide a username for running this job.');
  105. return;
  106. }
  107. if ($parallel) {
  108. drush_print("Tripal Job Launcher (in parallel)");
  109. drush_print("Running as user '$username'");
  110. drush_print("-------------------");
  111. tripal_jobs_launch($parallel, $job_id);
  112. }
  113. else {
  114. drush_print("Tripal Job Launcher");
  115. drush_print("Running as user '$username'");
  116. drush_print("-------------------");
  117. tripal_jobs_launch(0, $job_id);
  118. }
  119. }
  120. /**
  121. * Executes jobs in the Tripal Jobs Queue
  122. *
  123. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  124. */
  125. function drush_tripal_core_tripal_rerun_job($username, $job_id) {
  126. $new_job_id = tripal_jobs_rerun($job_id, FALSE);
  127. drush_tripal_core_tripal_launch_jobs($username, $new_job_id);
  128. }
  129. /**
  130. * Prints details about the current running job
  131. *
  132. * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  133. */
  134. function drush_tripal_core_tripal_current_job() {
  135. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  136. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  137. $jobs = db_query($sql);
  138. while ($job = db_fetch_object($jobs)) {
  139. $job_pid = $job->pid;
  140. $output = "Name: " . $job->job_name . "\n"
  141. ."Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n"
  142. ."Started: " . date(DATE_RFC822, $job->start_time) . "\n"
  143. ."Module: " . $job->modulename . "\n"
  144. ."Callback: " . $job->callback . "\n"
  145. ."Process ID: " . $job->pid . "\n"
  146. ."Progress: " . $job->progress . "%\n";
  147. drush_print($output);
  148. }
  149. if (!$job_pid) {
  150. drush_print('There are currently no running jobs.');
  151. }
  152. //log to the command line with an OK status
  153. drush_log('Running tripal-current-job', 'ok');
  154. }
  155. /**
  156. * Updates the specified materialized view
  157. *
  158. * @param $mview_id
  159. * The ID of the materialized view (tripal_mview.mview_id)
  160. * @param $table_name
  161. * The name of the table storing the materialized view (tripal_mview.mv_table)
  162. *
  163. * Note: Either $mview_id OR $table_name is required
  164. */
  165. function drush_tripal_core_tripal_update_mview() {
  166. $mview_id = drush_get_option('mview_id');
  167. $table_name = drush_get_option('table_name');
  168. // Either table_name or mview is required
  169. if (!$mview_id) {
  170. if ($table_name) {
  171. // if table_name supplied use that to get mview_id
  172. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
  173. $r = db_fetch_object(db_query($sql, $table_name));
  174. if (!$r->mview_id) {
  175. drush_set_error('No Materialized View associated with that table_name.');
  176. }
  177. $mview_id=$r->mview_id;
  178. }
  179. else {
  180. drush_set_error('Either mview_id OR table_name are required.');
  181. }
  182. }
  183. drush_print('Updating the Materialized View with ID=' . $mview_id);
  184. $status = tripal_update_mview($mview_id);
  185. if ($status) {
  186. drush_log('Materialized View Updated', 'ok');
  187. }
  188. else {
  189. drush_set_error('Update failed.');
  190. }
  191. }