tripal_core.drush.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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'),
  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. 'alias' => array('trpmv-up')
  50. );
  51. return $items;
  52. }
  53. /**
  54. * Prints details about the current running job
  55. *
  56. * NOTE: The following code is executed when drush tjob or drush tripal-current-job is called
  57. */
  58. function drush_tripal_core_tripal_current_job () {
  59. $sql = "SELECT * FROM {tripal_jobs} TJ ".
  60. "WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL ";
  61. $jobs = db_query($sql);
  62. while ($job = db_fetch_object($jobs)) {
  63. $job_pid = $job->pid;
  64. $output = "Name: ".$job->job_name."\n"
  65. ."Submitted: ".date(DATE_RFC822,$job->submit_date)."\n"
  66. ."Started: ".date(DATE_RFC822,$job->start_time)."\n"
  67. ."Module: ".$job->modulename."\n"
  68. ."Callback: ".$job->callback."\n"
  69. ."Process ID: ".$job->pid."\n"
  70. ."Progress: ".$job->progress."%\n";
  71. drush_print($output);
  72. }
  73. if (!$job_pid) {
  74. drush_print('There are currently no running jobs.');
  75. }
  76. //log to the command line with an OK status
  77. drush_log('Running tripal-current-job', 'ok');
  78. }
  79. /**
  80. * Updates the specified materialized view
  81. *
  82. * @param $mview_id
  83. * The ID of the materialized view (tripal_mview.mview_id)
  84. * @param $table_name
  85. * The name of the table storing the materialized view (tripal_mview.mv_table)
  86. *
  87. * Note: Either $mview_id OR $table_name is required
  88. */
  89. function drush_tripal_core_tripal_update_mview () {
  90. $mview_id = drush_get_option('mview_id');
  91. $table_name = drush_get_option('table_name');
  92. // Either table_name or mview is required
  93. if (!$mview_id) {
  94. if ($table_name) {
  95. // if table_name supplied use that to get mview_id
  96. $sql = "SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'";
  97. $r = db_fetch_object(db_query($sql, $table_name));
  98. if (!$r->mview_id) {
  99. drush_set_error('No Materialized View associated with that table_name.');
  100. }
  101. $mview_id=$r->mview_id;
  102. } else {
  103. drush_set_error('Either mview_id OR table_name are required.');
  104. }
  105. }
  106. drush_print('Updating the Materialized View with ID='.$mview_id);
  107. $status = tripal_update_mview($mview_id);
  108. if ($status) {
  109. drush_log('Materialized View Updated', 'ok');
  110. } else {
  111. drush_set_error('Update failed.');
  112. }
  113. }