tripal_daemon.drush.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * Implementation of the Tripal Daemon Drush commands.
  5. */
  6. /**
  7. * Implements hook_drush_help().
  8. */
  9. function tripal_daemon_drush_help($command) {
  10. switch ($command) {
  11. case 'drush:tripal-jobs-daemon':
  12. return dt('Use Tripal Jobs Deamon to manage Tripal Job execution.');
  13. }
  14. }
  15. /**
  16. * Implements hook_drush_command().
  17. */
  18. function tripal_daemon_drush_command() {
  19. $items = array();
  20. $items['tripal-jobs-daemon'] = array(
  21. 'description' => dt('Use Tripal Jobs Deamon to manage Tripal Job execution.'),
  22. 'arguments' => array(
  23. 'start' => 'Start the daemon.',
  24. 'status' => 'Display status information about the daemon.',
  25. 'stop' => 'Stop the daemon.',
  26. 'show-log' => 'Show the log file.',
  27. ),
  28. 'options' => array(
  29. '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.'),
  30. 'max_jobs' => dt('Indicate the maximum number of concurrent jobs. Default is 3; use -1 (unlimited). Ignore if not running parallel jobs'),
  31. 'num_lines' => 'The number of lines of the log file to show.',
  32. 'child' => array(
  33. 'hidden' => TRUE,
  34. 'description' => 'This option should only be passed via '
  35. . 'drush_invoke_process and essentially just allows my command '
  36. . 'to not fork bomb',
  37. ),
  38. ),
  39. 'examples' => array(
  40. 'drush trpjob-daemon start' => 'Start the daemon.',
  41. 'drush trpjob-daemon status' => 'Show the current status of the daemon.',
  42. 'drush trpjob-daemon stop' => 'Stop the daemon.',
  43. 'drush trpjob-daemon show-log' => 'Show the last 10 lines of the log file.',
  44. 'drush trpjob-daemon show-log --num_lines=50' => 'Show the last 10 lines of the log file.',
  45. ),
  46. 'aliases' => array('trpjob-daemon'),
  47. );
  48. return $items;
  49. }
  50. /**
  51. * Drush Command for Daemonized management of Tripal Jobs.
  52. *
  53. * Simply plugs into the Daemon API for easier running. This is equivalent to
  54. * drush jobs-daemon $action tripal_daemon.
  55. *
  56. * @param string $action
  57. * One of 'start','stop','restart',status','show-log'. Meant to indicate what
  58. * you want the daemon to do.
  59. */
  60. function drush_tripal_daemon_tripal_jobs_daemon($action) {
  61. $parallel = drush_get_option('parallel', FALSE);
  62. $max_jobs = drush_get_option('max_jobs', 3);
  63. // Check if we have the right version of Drush Daemon to support passing
  64. // options to the daemon class.
  65. $have_support = FALSE;
  66. if (function_exists('drushd_instantiate_daemon')) {
  67. $have_support = TRUE;
  68. }
  69. // We need to handle start ourselves in order to handle parallel processing
  70. if ($parallel AND ($action == 'start')) {
  71. // Check if we have the right version of Drush Daemon to support passing
  72. // options to the daemon class.
  73. if (function_exists('drushd_instantiate_daemon')) {
  74. // First, instantiate the daemon.
  75. $daemon = drushd_instantiate_daemon('tripal_daemon');
  76. // We always start our daemons in daemon-mode. Thus when the daemon is first
  77. // started from drush, we need to fork the process. However, we don't want
  78. // our children to fork continuously or we will end up with a fork_bomb.
  79. // Thus when we start our child process we pass in the "child" option which
  80. // tells our drush command not to fork again but instead to just run
  81. // the daemon.
  82. if (!drush_get_option('child')) {
  83. drush_invoke_process(
  84. '@self',
  85. 'tripal-jobs-daemon',
  86. array('start'),
  87. array('child' => TRUE, 'parallel' => $parallel, 'max_jobs' => $max_jobs),
  88. array('fork' => TRUE)
  89. );
  90. drush_print(dt("Use 'drush tripal-jobs-daemon status' to check the "
  91. . "status of the daemon just started and 'drush tripal-jobs-daemon stop' to stop it.\n"));
  92. }
  93. else {
  94. $daemon->setParallel($parallel, $max_jobs);
  95. $daemon->run();
  96. }
  97. }
  98. else {
  99. drush_set_error(dt('Error: You need version 2.3 of Drush Daemon to run
  100. parallel jobs. Either update your version of Drush Daemon or start the
  101. Tripal Daemon without the parallel option.'));
  102. }
  103. }
  104. // Otherwise just let Drush daemon handle it ;-).
  105. else {
  106. drush_drushd_daemon($action, 'tripal_daemon');
  107. }
  108. }