tripal_core.drush.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. tripal_jobs_launch($do_parallel);
  78. } else {
  79. tripal_jobs_launch();
  80. }
  81. }
  82. /**
  83. * Prints details about the current running job
  84. *
  85. * NOTE: The following code is executed when 'drush trpjob-curr' or 'drush tripal-current-job' is called
  86. */
  87. function drush_tripal_core_tripal_current_job () {
  88. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  89. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  90. $jobs = db_query($sql);
  91. while ($job = db_fetch_object($jobs)) {
  92. $job_pid = $job->pid;
  93. $output = "Name: ".$job->job_name."\n"
  94. ."Submitted: ".date(DATE_RFC822,$job->submit_date)."\n"
  95. ."Started: ".date(DATE_RFC822,$job->start_time)."\n"
  96. ."Module: ".$job->modulename."\n"
  97. ."Callback: ".$job->callback."\n"
  98. ."Process ID: ".$job->pid."\n"
  99. ."Progress: ".$job->progress."%\n";
  100. drush_print($output);
  101. }
  102. if (!$job_pid) {
  103. drush_print('There are currently no running jobs.');
  104. }
  105. //log to the command line with an OK status
  106. drush_log('Running tripal-current-job', 'ok');
  107. }
  108. /**
  109. * Updates the specified materialized view
  110. *
  111. * @param $mview_id
  112. * The ID of the materialized view (tripal_mview.mview_id)
  113. * @param $table_name
  114. * The name of the table storing the materialized view (tripal_mview.mv_table)
  115. *
  116. * Note: Either $mview_id OR $table_name is required
  117. */
  118. function drush_tripal_core_tripal_update_mview () {
  119. $mview_id = drush_get_option('mview_id');
  120. $table_name = drush_get_option('table_name');
  121. // Either table_name or mview is required
  122. if (!$mview_id) {
  123. if ($table_name) {
  124. // if table_name supplied use that to get mview_id
  125. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
  126. $r = db_fetch_object(db_query($sql, $table_name));
  127. if (!$r->mview_id) {
  128. drush_set_error('No Materialized View associated with that table_name.');
  129. }
  130. $mview_id=$r->mview_id;
  131. } else {
  132. drush_set_error('Either mview_id OR table_name are required.');
  133. }
  134. }
  135. drush_print('Updating the Materialized View with ID='.$mview_id);
  136. $status = tripal_update_mview($mview_id);
  137. if ($status) {
  138. drush_log('Materialized View Updated', 'ok');
  139. } else {
  140. drush_set_error('Update failed.');
  141. }
  142. }