tripal_core.drush.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Describes each drush command implemented by the module
  4. *
  5. * @return
  6. * The first line of description when executing the help for a given command
  7. */
  8. function tripal_core_drush_help($command) {
  9. switch ($command) {
  10. case 'drush:tripal-current-job':
  11. return dt('Returns details about the currently running tripal job including percent complete.');
  12. case 'drush:tripal-update-mview':
  13. return dt('Updates the specified materialized view.');
  14. }
  15. }
  16. /**
  17. * Registers a drush command and constructs the full help for that command
  18. *
  19. * @return
  20. * And array of command descriptions
  21. */
  22. function tripal_core_drush_command() {
  23. $items = array();
  24. $items['tripal-current-job'] = array(
  25. 'description' => dt('Returns details about the currently running tripal job including percent complete.'),
  26. 'arguments' => array(
  27. ),
  28. 'examples' => array(
  29. 'Standard example' => 'drush tripal-current-job',
  30. ),
  31. 'aliases' => array('trpjob-cur'),
  32. );
  33. $items['tripal-update-mview'] = array(
  34. // used by drush help
  35. 'description' => dt('Updates the specified materialized view.'),
  36. 'arguments' => array(
  37. 'mview_id' => dt('The ID of the materialized view to update'),
  38. 'table_name' => dt('The name of the materialized view table to update.'),
  39. ),
  40. 'examples' => array(
  41. 'By Materialized View ID' => 'drush tripal-update-mview --mview_id=5',
  42. 'By Table Name' => 'drush tripal-update-mview --table_name=organism_feature_count'
  43. ),
  44. // supply options
  45. 'options' => array(
  46. 'mview_id',
  47. 'table_name'
  48. ),
  49. 'aliases' => array('trpmv-up')
  50. );
  51. $items['tripal-launch-jobs'] = array(
  52. // used by drush help
  53. 'description' => dt('Lauches any jobs waiting in the queue.'),
  54. 'arguments' => array(
  55. '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.'),
  56. ),
  57. 'examples' => array(
  58. 'Normal Job' => 'drush tripal-launch-jobs',
  59. 'Parallel Job' => 'drush tripal-launch-jobs --parallel=1'
  60. ),
  61. // supply options
  62. 'options' => array(
  63. 'parallel',
  64. ),
  65. 'aliases' => array('trpjob-run')
  66. );
  67. return $items;
  68. }
  69. /**
  70. * Executes jobs in the Tripal Jobs Queue
  71. *
  72. * NOTE: The following code is executed when drush 'trpjob-run' or 'drush tripal-launch-jobs' is called
  73. */
  74. function drush_tripal_core_tripal_launch_jobs () {
  75. $parallel = drush_get_option('parallel');
  76. if($parallel){
  77. print "Tripal Job Launcher (in parallel)\n";
  78. print "-------------------\n";
  79. tripal_jobs_launch($parallel);
  80. } else {
  81. print "Tripal Job Launcher\n";
  82. print "-------------------\n";
  83. tripal_jobs_launch();
  84. }
  85. }
  86. /**
  87. * Prints details about the current running job
  88. *
  89. * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  90. */
  91. function drush_tripal_core_tripal_current_job () {
  92. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  93. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  94. $jobs = db_query($sql);
  95. while ($job = db_fetch_object($jobs)) {
  96. $job_pid = $job->pid;
  97. $output = "Name: ".$job->job_name."\n"
  98. ."Submitted: ".date(DATE_RFC822,$job->submit_date)."\n"
  99. ."Started: ".date(DATE_RFC822,$job->start_time)."\n"
  100. ."Module: ".$job->modulename."\n"
  101. ."Callback: ".$job->callback."\n"
  102. ."Process ID: ".$job->pid."\n"
  103. ."Progress: ".$job->progress."%\n";
  104. drush_print($output);
  105. }
  106. if (!$job_pid) {
  107. drush_print('There are currently no running jobs.');
  108. }
  109. //log to the command line with an OK status
  110. drush_log('Running tripal-current-job', 'ok');
  111. }
  112. /**
  113. * Updates the specified materialized view
  114. *
  115. * @param $mview_id
  116. * The ID of the materialized view (tripal_mview.mview_id)
  117. * @param $table_name
  118. * The name of the table storing the materialized view (tripal_mview.mv_table)
  119. *
  120. * Note: Either $mview_id OR $table_name is required
  121. */
  122. function drush_tripal_core_tripal_update_mview () {
  123. $mview_id = drush_get_option('mview_id');
  124. $table_name = drush_get_option('table_name');
  125. // Either table_name or mview is required
  126. if (!$mview_id) {
  127. if ($table_name) {
  128. // if table_name supplied use that to get mview_id
  129. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
  130. $r = db_fetch_object(db_query($sql, $table_name));
  131. if (!$r->mview_id) {
  132. drush_set_error('No Materialized View associated with that table_name.');
  133. }
  134. $mview_id=$r->mview_id;
  135. } else {
  136. drush_set_error('Either mview_id OR table_name are required.');
  137. }
  138. }
  139. drush_print('Updating the Materialized View with ID='.$mview_id);
  140. $status = tripal_update_mview($mview_id);
  141. if ($status) {
  142. drush_log('Materialized View Updated', 'ok');
  143. } else {
  144. drush_set_error('Update failed.');
  145. }
  146. }