tripal_core.drush.inc 5.0 KB

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