NullJob.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Degenerate job that does nothing.
  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 JobQueue
  22. */
  23. /**
  24. * Degenerate job that does nothing, but can optionally replace itself
  25. * in the queue and/or sleep for a brief time period. These can be used
  26. * to represent "no-op" jobs or test lock contention and performance.
  27. *
  28. * @par Example:
  29. * Inserting a null job in the configured job queue:
  30. * @code
  31. * $ php maintenance/eval.php
  32. * > $queue = JobQueueGroup::singleton();
  33. * > $job = new NullJob( [ 'lives' => 10 ] );
  34. * > $queue->push( $job );
  35. * @endcode
  36. * You can then confirm the job has been enqueued by using the showJobs.php
  37. * maintenance utility:
  38. * @code
  39. * $ php maintenance/showJobs.php --group
  40. * null: 1 queue; 0 claimed (0 active, 0 abandoned)
  41. * $
  42. * @endcode
  43. *
  44. * @ingroup JobQueue
  45. */
  46. class NullJob extends Job implements GenericParameterJob {
  47. /**
  48. * @param array $params Job parameters (lives, usleep)
  49. */
  50. function __construct( array $params ) {
  51. parent::__construct( 'null', $params );
  52. if ( !isset( $this->params['lives'] ) ) {
  53. $this->params['lives'] = 1;
  54. }
  55. if ( !isset( $this->params['usleep'] ) ) {
  56. $this->params['usleep'] = 0;
  57. }
  58. $this->removeDuplicates = !empty( $this->params['removeDuplicates'] );
  59. }
  60. public function run() {
  61. if ( $this->params['usleep'] > 0 ) {
  62. usleep( $this->params['usleep'] );
  63. }
  64. if ( $this->params['lives'] > 1 ) {
  65. $params = $this->params;
  66. $params['lives']--;
  67. $job = new self( $params );
  68. JobQueueGroup::singleton()->push( $job );
  69. }
  70. return true;
  71. }
  72. }