runJobs.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Run pending jobs.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. use MediaWiki\Logger\LoggerFactory;
  25. /**
  26. * Maintenance script that runs pending jobs.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class RunJobs extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Run pending jobs' );
  34. $this->addOption( 'maxjobs', 'Maximum number of jobs to run', false, true );
  35. $this->addOption( 'maxtime', 'Maximum amount of wall-clock time', false, true );
  36. $this->addOption( 'type', 'Type of job to run', false, true );
  37. $this->addOption( 'procs', 'Number of processes to use', false, true );
  38. $this->addOption( 'nothrottle', 'Ignore job throttling configuration', false, false );
  39. $this->addOption( 'result', 'Set to JSON to print only a JSON response', false, true );
  40. $this->addOption( 'wait', 'Wait for new jobs instead of exiting', false, false );
  41. }
  42. public function memoryLimit() {
  43. if ( $this->hasOption( 'memory-limit' ) ) {
  44. return parent::memoryLimit();
  45. }
  46. // Don't eat all memory on the machine if we get a bad job.
  47. return "150M";
  48. }
  49. public function execute() {
  50. if ( $this->hasOption( 'procs' ) ) {
  51. $procs = intval( $this->getOption( 'procs' ) );
  52. if ( $procs < 1 || $procs > 1000 ) {
  53. $this->error( "Invalid argument to --procs", true );
  54. } elseif ( $procs != 1 ) {
  55. $fc = new ForkController( $procs );
  56. if ( $fc->start() != 'child' ) {
  57. exit( 0 );
  58. }
  59. }
  60. }
  61. $outputJSON = ( $this->getOption( 'result' ) === 'json' );
  62. $wait = $this->hasOption( 'wait' );
  63. $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
  64. if ( !$outputJSON ) {
  65. $runner->setDebugHandler( [ $this, 'debugInternal' ] );
  66. }
  67. $type = $this->getOption( 'type', false );
  68. $maxJobs = $this->getOption( 'maxjobs', false );
  69. $maxTime = $this->getOption( 'maxtime', false );
  70. $throttle = !$this->hasOption( 'nothrottle' );
  71. while ( true ) {
  72. $response = $runner->run( [
  73. 'type' => $type,
  74. 'maxJobs' => $maxJobs,
  75. 'maxTime' => $maxTime,
  76. 'throttle' => $throttle,
  77. ] );
  78. if ( $outputJSON ) {
  79. $this->output( FormatJson::encode( $response, true ) );
  80. }
  81. if (
  82. !$wait ||
  83. $response['reached'] === 'time-limit' ||
  84. $response['reached'] === 'job-limit' ||
  85. $response['reached'] === 'memory-limit'
  86. ) {
  87. break;
  88. }
  89. if ( $maxJobs !== false ) {
  90. $maxJobs -= count( $response['jobs'] );
  91. }
  92. sleep( 1 );
  93. }
  94. }
  95. /**
  96. * @param string $s
  97. */
  98. public function debugInternal( $s ) {
  99. $this->output( $s );
  100. }
  101. }
  102. $maintClass = "RunJobs";
  103. require_once RUN_MAINTENANCE_IF_MAIN;