tripal.drush.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. 'Max-jobs Job' => 'drush trp-run-jobs --username=administrator --parallel=1 --max_jobs=10',
  66. ),
  67. 'arguments' => array(),
  68. 'options' => array(
  69. 'user' => array(
  70. 'description' => dt('DEPRECATED. Conflicts with Drush 7.x --user argument. Please use the --username argument.'),
  71. ),
  72. 'username' => array(
  73. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  74. ),
  75. '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.'),
  76. 'job_id' => dt('Provide a job_id to run a specific job. Only jobs that have not been run already can be used'),
  77. 'max_jobs' => dt('Indicate the maximum number of concurrent jobs. Default is -1 (unlimited). Ignore if not running parallel jobs'),
  78. 'single' => dt('Execute only one queued job'),
  79. ),
  80. );
  81. $items['trp-rerun-job'] = array(
  82. 'description' => dt('Re-run a specific job from the queue.'),
  83. 'examples' => array(
  84. 'Single Job' => 'drush trp-rerun-job --username=administrator --job_id=2',
  85. 'Parallel Job' => 'drush trp-rerun-job --username=administrator --job_id=2 --parallel=1',
  86. 'Max-jobs Job' => 'drush trp-run-jobs --username=administrator --parallel=1 --max_jobs=10',
  87. ),
  88. 'arguments' => array(),
  89. 'options' => array(
  90. 'user' => array(
  91. 'description' => dt('DEPRECATED. Conflicts with Drush 7.x --user argument. Please use the --username argument.'),
  92. ),
  93. 'username' => array(
  94. 'description' => dt('The Drupal user name for which the job should be run. The permissions for this user will be used.'),
  95. ),
  96. 'job_id' => array(
  97. 'description' => dt('The job ID to run.'),
  98. 'required' => TRUE,
  99. ),
  100. '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.'),
  101. 'max_jobs' => dt('Indicate the maximum number of concurrent jobs. Default is -1 (unlimited). Ignore if not running parallel jobs'),
  102. 'single' => dt('Execute only one queued job'),
  103. ),
  104. );
  105. return $items;
  106. }
  107. /**
  108. * Set the user to run a drush job.
  109. *
  110. * @ingroup tripal_drush
  111. */
  112. function drush_tripal_set_user($username) {
  113. if ($username) {
  114. $sql = "SELECT uid FROM {users} WHERE name = :name";
  115. $results = db_query($sql, array(':name' => $username));
  116. $u = $results->fetchObject();
  117. if (!$u) {
  118. drush_print(date('Y-m-d H:i:s'));
  119. drush_print('ERROR: Please provide a valid username (--username argument) for running this job.');
  120. exit;
  121. }
  122. global $user;
  123. $user = user_load($u->uid);
  124. return $u->uid;
  125. }
  126. else {
  127. drush_print(date('Y-m-d H:i:s'));
  128. drush_print('ERROR: Please provide a username (--username argument) for running this job.');
  129. exit;
  130. }
  131. }
  132. /**
  133. * Executes jobs in the Tripal Jobs Queue.
  134. *
  135. * Executed when 'drush trp-run-job' is called.
  136. *
  137. * @ingroup tripal_drush
  138. */
  139. function drush_tripal_trp_run_jobs() {
  140. $parallel = drush_get_option('parallel');
  141. $job_id = drush_get_option('job_id');
  142. $max_jobs = drush_get_option('max_jobs', -1);
  143. $single = drush_get_option('single', 0);
  144. // Unfortunately later versions of Drush use the '--user' argument which
  145. // makes it incompatible with how Tripal was using it. For backwards
  146. // compatabiliy we will accept --user with a non numeric value only. The
  147. // numeric value should be for Drush. Tripal will instead use the
  148. // --username argument for the fture.
  149. $user = drush_get_option('user');
  150. $uname = drush_get_option('username');
  151. if ($user and is_numeric($user)) {
  152. }
  153. elseif ($user) {
  154. 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";
  155. $username = $user;
  156. }
  157. if ($uname) {
  158. $username = $uname;
  159. }
  160. drush_tripal_set_user($username);
  161. drush_print("\n" . date('Y-m-d H:i:s'));
  162. if ($parallel) {
  163. drush_print("Tripal Job Launcher (in parallel)");
  164. if ($max_jobs !== -1) drush_print("Maximum number of jobs is " . $max_jobs);
  165. drush_print("Running as user '$username'");
  166. drush_print("-------------------");
  167. tripal_launch_job($parallel, $job_id, $max_jobs, $single);
  168. }
  169. else {
  170. drush_print("Tripal Job Launcher");
  171. drush_print("Running as user '$username'");
  172. drush_print("-------------------");
  173. tripal_launch_job(0, $job_id, $max_jobs, $single);
  174. }
  175. }
  176. /**
  177. * Executes jobs in the Tripal Jobs Queue.
  178. *
  179. * Executed when 'drush trp-rerun-job' is called.
  180. *
  181. * @ingroup tripal_drush
  182. */
  183. function drush_tripal_trp_rerun_job() {
  184. // Unfortunately later versions of Drush use the '--user' argument which
  185. // makes it incompatible with how Tripal was using it. For backwards
  186. // compatabiliy we will accept --user with a non numeric value only. The
  187. // numeric value should be for Drush. Tripal will instead use the
  188. // --username argument for the fture.
  189. $user = drush_get_option('user');
  190. $uname = drush_get_option('username');
  191. print date('Y-m-d H:i:s') . ": USER: '$user', UNAME: '$uname'\n";
  192. if ($user and is_numeric($user)) {
  193. }
  194. elseif ($user) {
  195. 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";
  196. $username = $user;
  197. }
  198. if ($uname) {
  199. $username = $uname;
  200. }
  201. $parallel = drush_get_option('parallel');
  202. $job_id = drush_get_option('job_id');
  203. $max_jobs = drush_get_option('max_jobs', -1);
  204. $single = drush_get_option('single', 0);
  205. drush_tripal_set_user($username);
  206. $new_job_id = tripal_rerun_job($job_id, FALSE);
  207. drush_print("\n" . date('Y-m-d H:i:s'));
  208. if ($parallel) {
  209. drush_print("Tripal Job Launcher (in parallel)");
  210. drush_print("Running as user '$username'");
  211. drush_print("-------------------");
  212. tripal_launch_job($parallel, $new_job_id, $max_jobs, $single);
  213. }
  214. else {
  215. drush_print("Tripal Job Launcher");
  216. drush_print("Running as user '$username'");
  217. drush_print("-------------------");
  218. tripal_launch_job(0, $new_job_id, $max_jobs, $single);
  219. }
  220. }
  221. /**
  222. * Prints details about the current running job.
  223. *
  224. * Executed when 'drush trp-get-currjob' is called.
  225. *
  226. * @ingroup tripal_drush
  227. */
  228. function drush_tripal_trp_get_currjob() {
  229. $sql = "
  230. SELECT *
  231. FROM {tripal_jobs} TJ
  232. WHERE TJ.end_time IS NULL and NOT TJ.start_time IS NULL
  233. ";
  234. $jobs = db_query($sql);
  235. foreach ($jobs as $job) {
  236. $job_pid = $job->pid;
  237. $output = "Name: " . $job->job_name . "\n" .
  238. "Submitted: " . date(DATE_RFC822, $job->submit_date) . "\n" .
  239. "Started: " . date(DATE_RFC822, $job->start_time) . "\n" .
  240. "Module: " . $job->modulename . "\n" .
  241. "Callback: " . $job->callback . "\n" .
  242. "Process ID: " . $job->pid . "\n" .
  243. "Progress: " . $job->progress . "%\n".
  244. "Current Date: " . date('Y-m-d H:i:s') . "\n";
  245. drush_print(date('Y-m-d H:i:s'));
  246. drush_print($output);
  247. }
  248. if (!$job_pid) {
  249. drush_print(date('Y-m-d H:i:s'));
  250. drush_print('There are currently no running jobs.');
  251. }
  252. //log to the command line with an OK status
  253. drush_log('Running tripal-current-job', 'ok');
  254. }