tripal.drush.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * @file
  4. * Contains function relating to drush-integration of this module.
  5. */
  6. /**
  7. * @defgroup tripal_drush Tripal Drush Integration
  8. * @{
  9. * Contains function relating to drush-integration of various tripal modules.
  10. * @}
  11. */
  12. /**
  13. * Describes each drush command implemented by the module
  14. *
  15. * @return
  16. * The first line of description when executing the help for a given command
  17. *
  18. * @ingroup tripal_drush
  19. */
  20. function tripal_drush_help($command) {
  21. switch ($command) {
  22. // TRIPAL JOBS
  23. case 'trp-run-jobs':
  24. return dt('Launches pending jobs waiting in the queue.');
  25. break;
  26. case 'trp-rerun-job':
  27. return dt('Rerun a job in the queue.');
  28. break;
  29. case 'trp-get-currjob':
  30. return dt('Returns details about the currently running tripal job including percent complete.');
  31. break;
  32. // Placeholders for unimplmeneted jobs
  33. case 'trp-show-job':
  34. break;
  35. case 'trp-revert-jobs':
  36. break;
  37. case 'trp-cancel-job':
  38. break;
  39. case 'trp-list-jobs':
  40. break;
  41. }
  42. }
  43. /**
  44. * Registers a drush command and constructs the full help for that command.
  45. *
  46. * @return
  47. * And array of command descriptions
  48. *
  49. * @ingroup tripal_drush
  50. */
  51. function tripal_drush_command() {
  52. $items = array();
  53. $items['trp-get-currjob'] = array(
  54. 'description' => dt('Returns details about the currently running job including percent complete.'),
  55. 'arguments' => array(),
  56. 'examples' => array(
  57. 'Standard example' => 'drush trp-get-currjob',
  58. ),
  59. );
  60. $items['trp-run-jobs'] = array(
  61. 'description' => dt('Launches jobs waiting in the queue. Only one job can execute at a time unless the --parllel=1 option is provided.'),
  62. 'examples' => array(
  63. 'Single Job' => 'drush trp-run-jobs --username=administrator',
  64. 'Parallel Job' => 'drush trp-run-jobs --username=administrator --parallel=1'
  65. ),
  66. 'arguments' => array(),
  67. 'options' => array(
  68. 'user' => array(
  69. 'description' => dt('DEPRECATED. Conflicts with Drush 7.x --user argument. Please use the --username argument.'),
  70. ),
  71. 'username' => array(
  72. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  73. ),
  74. '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.'),
  75. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  76. ),
  77. );
  78. $items['trp-rerun-job'] = array(
  79. 'description' => dt('Re-run a specific job from the queue.'),
  80. 'examples' => array(
  81. 'Single Job' => 'drush trp-rerun-job --username=administrator --job_id=2',
  82. 'Parallel Job' => 'drush trp-rerun-job --username=administrator --job_id=2 --parallel=1'
  83. ),
  84. 'arguments' => array(),
  85. 'options' => array(
  86. 'user' => array(
  87. 'description' => dt('DEPRECATED. Conflicts with Drush 7.x --user argument. Please use the --username argument.'),
  88. ),
  89. 'username' => array(
  90. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  91. ),
  92. 'job_id' => array(
  93. 'description' => dt('The job ID to run.'),
  94. 'required' => TRUE,
  95. ),
  96. '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.'),
  97. ),
  98. );
  99. return $items;
  100. }
  101. /**
  102. * Set the user to run a drush job.
  103. *
  104. * @ingroup tripal_drush
  105. */
  106. function drush_tripal_set_user($username) {
  107. if ($username) {
  108. $sql = "SELECT uid FROM {users} WHERE name = :name";
  109. $results = db_query($sql, array(':name' => $username));
  110. $u = $results->fetchObject();
  111. if (!$u) {
  112. drush_print('ERROR: Please provide a valid username (--username argument) for running this job.');
  113. exit;
  114. }
  115. global $user;
  116. $user = user_load($u->uid);
  117. return $u->uid;
  118. }
  119. else {
  120. drush_print('ERROR: Please provide a username (--username argument) for running this job.');
  121. exit;
  122. }
  123. }
  124. /**
  125. * Executes jobs in the Tripal Jobs Queue.
  126. *
  127. * Executed when 'drush trp-run-job' is called.
  128. *
  129. * @ingroup tripal_drush
  130. */
  131. function drush_tripal_trp_run_jobs() {
  132. $parallel = drush_get_option('parallel');
  133. $job_id = drush_get_option('job_id');
  134. // Unfortunately later versions of Drush use the '--user' argument which
  135. // makes it incompatible with how Tripal was using it. For backwards
  136. // compatabiliy we will accept --user with a non numeric value only. The
  137. // numeric value should be for Drush. Tripal will instead use the
  138. // --username argument for the fture.
  139. $user = drush_get_option('user');
  140. $uname = drush_get_option('username');
  141. if ($user and is_numeric($user)) {
  142. }
  143. elseif ($user) {
  144. print "\nNOTE: Use of the --user argument is deprecated as it conflicts with the --user argument of Drush 7.x. Please now use --username instead.\n\n";
  145. $username = $user;
  146. }
  147. if ($uname) {
  148. $username = $uname;
  149. }
  150. drush_tripal_set_user($username);
  151. if ($parallel) {
  152. drush_print("Tripal Job Launcher (in parallel)");
  153. drush_print("Running as user '$username'");
  154. drush_print("-------------------");
  155. tripal_launch_job($parallel, $job_id);
  156. }
  157. else {
  158. drush_print("Tripal Job Launcher");
  159. drush_print("Running as user '$username'");
  160. drush_print("-------------------");
  161. tripal_launch_job(0, $job_id);
  162. }
  163. }
  164. /**
  165. * Executes jobs in the Tripal Jobs Queue.
  166. *
  167. * Executed when 'drush trp-rerun-job' is called.
  168. *
  169. * @ingroup tripal_drush
  170. */
  171. function drush_tripal_trp_rerun_job() {
  172. // Unfortunately later versions of Drush use the '--user' argument which
  173. // makes it incompatible with how Tripal was using it. For backwards
  174. // compatabiliy we will accept --user with a non numeric value only. The
  175. // numeric value should be for Drush. Tripal will instead use the
  176. // --username argument for the fture.
  177. $user = drush_get_option('user');
  178. $uname = drush_get_option('username');
  179. print "USER: '$user', UNAME: '$uname'\n";
  180. if ($user and is_numeric($user)) {
  181. }
  182. elseif ($user) {
  183. print "\nNOTE: Use of the --user argument is deprecated as it conflicts with the --user argument of Drush 7.x. Please now use --username instead.\n\n";
  184. $username = $user;
  185. }
  186. if ($uname) {
  187. $username = $uname;
  188. }
  189. $parallel = drush_get_option('parallel');
  190. $job_id = drush_get_option('job_id');
  191. drush_tripal_set_user($username);
  192. $new_job_id = tripal_rerun_job($job_id, FALSE);
  193. if ($parallel) {
  194. drush_print("Tripal Job Launcher (in parallel)");
  195. drush_print("Running as user '$username'");
  196. drush_print("-------------------");
  197. tripal_launch_job($parallel, $new_job_id);
  198. }
  199. else {
  200. drush_print("Tripal Job Launcher");
  201. drush_print("Running as user '$username'");
  202. drush_print("-------------------");
  203. tripal_launch_job(0, $new_job_id);
  204. }
  205. }
  206. /**
  207. * Prints details about the current running job.
  208. *
  209. * Executed when 'drush trp-get-currjob' is called.
  210. *
  211. * @ingroup tripal_drush
  212. */
  213. function drush_tripal_trp_get_currjob() {
  214. $sql = "
  215. SELECT *
  216. FROM {tripal_jobs} TJ
  217. WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL
  218. ";
  219. $jobs = db_query($sql);
  220. foreach ($jobs as $job) {
  221. $job_pid = $job->pid;
  222. $output = "Name: " . $job->job_name . "\n" .
  223. "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
  224. "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
  225. "Module: " . $job->modulename . "\n" .
  226. "Callback: " . $job->callback . "\n" .
  227. "Process ID: " . $job->pid . "\n" .
  228. "Progress: " . $job->progress . "%\n";
  229. drush_print($output);
  230. }
  231. if (!$job_pid) {
  232. drush_print('There are currently no running jobs.');
  233. }
  234. //log to the command line with an OK status
  235. drush_log('Running tripal-current-job', 'ok');
  236. }