showJobs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Report number of jobs currently waiting in master database.
  4. *
  5. * Based on runJobs.php
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup Maintenance
  24. * @author Tim Starling
  25. * @author Antoine Musso
  26. */
  27. require_once __DIR__ . '/Maintenance.php';
  28. /**
  29. * Maintenance script that reports the number of jobs currently waiting
  30. * in master database.
  31. *
  32. * @ingroup Maintenance
  33. */
  34. class ShowJobs extends Maintenance {
  35. protected static $stateMethods = [
  36. 'unclaimed' => 'getAllQueuedJobs',
  37. 'delayed' => 'getAllDelayedJobs',
  38. 'claimed' => 'getAllAcquiredJobs',
  39. 'abandoned' => 'getAllAbandonedJobs',
  40. ];
  41. public function __construct() {
  42. parent::__construct();
  43. $this->addDescription( 'Show number of jobs waiting in master database' );
  44. $this->addOption( 'group', 'Show number of jobs per job type' );
  45. $this->addOption( 'list', 'Show a list of all jobs instead of counts' );
  46. $this->addOption( 'type', 'Only show/count jobs of a given type', false, true );
  47. $this->addOption( 'status', 'Filter list by state (unclaimed,delayed,claimed,abandoned)' );
  48. $this->addOption( 'limit', 'Limit of jobs listed' );
  49. }
  50. public function execute() {
  51. $typeFilter = $this->getOption( 'type', '' );
  52. $stateFilter = $this->getOption( 'status', '' );
  53. $stateLimit = (float)$this->getOption( 'limit', INF );
  54. $group = JobQueueGroup::singleton();
  55. $filteredTypes = $typeFilter
  56. ? [ $typeFilter ]
  57. : $group->getQueueTypes();
  58. $filteredStates = $stateFilter
  59. ? array_intersect_key( self::$stateMethods, [ $stateFilter => 1 ] )
  60. : self::$stateMethods;
  61. if ( $this->hasOption( 'list' ) ) {
  62. $count = 0;
  63. foreach ( $filteredTypes as $type ) {
  64. $queue = $group->get( $type );
  65. foreach ( $filteredStates as $state => $method ) {
  66. foreach ( $queue->$method() as $job ) {
  67. /** @var Job $job */
  68. $this->output( $job->toString() . " status=$state\n" );
  69. if ( ++$count >= $stateLimit ) {
  70. return;
  71. }
  72. }
  73. }
  74. }
  75. } elseif ( $this->hasOption( 'group' ) ) {
  76. foreach ( $filteredTypes as $type ) {
  77. $queue = $group->get( $type );
  78. $delayed = $queue->getDelayedCount();
  79. $pending = $queue->getSize();
  80. $claimed = $queue->getAcquiredCount();
  81. $abandoned = $queue->getAbandonedCount();
  82. $active = max( 0, $claimed - $abandoned );
  83. if ( ( $pending + $claimed + $delayed + $abandoned ) > 0 ) {
  84. $this->output(
  85. "{$type}: $pending queued; " .
  86. "$claimed claimed ($active active, $abandoned abandoned); " .
  87. "$delayed delayed\n"
  88. );
  89. }
  90. }
  91. } else {
  92. $count = 0;
  93. foreach ( $filteredTypes as $type ) {
  94. $count += $group->get( $type )->getSize();
  95. }
  96. $this->output( "$count\n" );
  97. }
  98. }
  99. }
  100. $maintClass = "ShowJobs";
  101. require_once RUN_MAINTENANCE_IF_MAIN;