tripal_core.drush.inc 5.2 KB

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