tripal_core.drush.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. return $items;
  73. }
  74. /**
  75. * Executes jobs in the Tripal Jobs Queue
  76. *
  77. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  78. */
  79. function drush_tripal_core_tripal_launch_jobs($username) {
  80. $parallel = drush_get_option('parallel');
  81. $job_id = drush_get_option('job_id');
  82. if($username){
  83. global $user;
  84. $user = user_load(array('name' => $username));
  85. }
  86. else {
  87. drush_print('ERROR: Please provide a username for running this job.');
  88. return;
  89. }
  90. if ($parallel) {
  91. drush_print("Tripal Job Launcher (in parallel)");
  92. drush_print("Running as user '$username'");
  93. drush_print("-------------------");
  94. tripal_jobs_launch($parallel,$job_id);
  95. }
  96. else {
  97. drush_print("Tripal Job Launcher");
  98. drush_print("Running as user '$username'");
  99. drush_print("-------------------");
  100. tripal_jobs_launch(0,$job_id);
  101. }
  102. }
  103. /**
  104. * Prints details about the current running job
  105. *
  106. * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  107. */
  108. function drush_tripal_core_tripal_current_job() {
  109. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  110. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  111. $jobs = db_query($sql);
  112. while ($job = db_fetch_object($jobs)) {
  113. $job_pid = $job->pid;
  114. $output = "Name: " . $job->job_name . "\n"
  115. ."Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n"
  116. ."Started: " . date(DATE_RFC822, $job->start_time) . "\n"
  117. ."Module: " . $job->modulename . "\n"
  118. ."Callback: " . $job->callback . "\n"
  119. ."Process ID: " . $job->pid . "\n"
  120. ."Progress: " . $job->progress . "%\n";
  121. drush_print($output);
  122. }
  123. if (!$job_pid) {
  124. drush_print('There are currently no running jobs.');
  125. }
  126. //log to the command line with an OK status
  127. drush_log('Running tripal-current-job', 'ok');
  128. }
  129. /**
  130. * Updates the specified materialized view
  131. *
  132. * @param $mview_id
  133. * The ID of the materialized view (tripal_mview.mview_id)
  134. * @param $table_name
  135. * The name of the table storing the materialized view (tripal_mview.mv_table)
  136. *
  137. * Note: Either $mview_id OR $table_name is required
  138. */
  139. function drush_tripal_core_tripal_update_mview() {
  140. $mview_id = drush_get_option('mview_id');
  141. $table_name = drush_get_option('table_name');
  142. // Either table_name or mview is required
  143. if (!$mview_id) {
  144. if ($table_name) {
  145. // if table_name supplied use that to get mview_id
  146. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
  147. $r = db_fetch_object(db_query($sql, $table_name));
  148. if (!$r->mview_id) {
  149. drush_set_error('No Materialized View associated with that table_name.');
  150. }
  151. $mview_id=$r->mview_id;
  152. }
  153. else {
  154. drush_set_error('Either mview_id OR table_name are required.');
  155. }
  156. }
  157. drush_print('Updating the Materialized View with ID=' . $mview_id);
  158. $status = tripal_update_mview($mview_id);
  159. if ($status) {
  160. drush_log('Materialized View Updated', 'ok');
  161. }
  162. else {
  163. drush_set_error('Update failed.');
  164. }
  165. }