opportunisticqueuemanager.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * GNU social queue-manager-on-visit class
  4. *
  5. * Will run events for a certain time, or until finished.
  6. *
  7. * Configure remote key if wanted with $config['opportunisticqm']['qmkey'] and
  8. * use with /main/runqueue?qmkey=abc123
  9. *
  10. * @category Cron
  11. * @package GNUsocial
  12. * @author Mikael Nordfeldth <mmn@hethane.se>
  13. * @copyright 2013 Free Software Foundation, Inc.
  14. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  15. * @link http://status.net/
  16. */
  17. class OpportunisticQueueManager extends QueueManager
  18. {
  19. protected $qmkey = false;
  20. protected $max_execution_time = null;
  21. protected $max_execution_margin = null; // margin to PHP's max_execution_time
  22. protected $max_queue_items = null;
  23. protected $started_at = null;
  24. protected $handled_items = 0;
  25. protected $verbosity = null;
  26. // typically just used for the /main/cron action, only used if php.ini max_execution_time is 0
  27. const MAXEXECTIME = 20;
  28. public function __construct(array $args = []) {
  29. foreach (get_class_vars(get_class($this)) as $key=>$val) {
  30. if (array_key_exists($key, $args)) {
  31. $this->$key = $args[$key];
  32. }
  33. }
  34. $this->verifyKey();
  35. if ($this->started_at === null) {
  36. $this->started_at = time();
  37. }
  38. if ($this->max_execution_time === null) {
  39. $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
  40. }
  41. if ($this->max_execution_margin === null) {
  42. // think PHP's max exec time, minus this value to have time for timeouts etc.
  43. $this->max_execution_margin = common_config('http', 'connect_timeout') + 1;
  44. }
  45. return parent::__construct();
  46. }
  47. protected function verifyKey()
  48. {
  49. if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
  50. throw new RunQueueBadKeyException($this->qmkey);
  51. }
  52. }
  53. public function enqueue($object, $key)
  54. {
  55. // Nothing to do, should never get called
  56. throw new ServerException('OpportunisticQueueManager::enqueue should never be called');
  57. }
  58. public function canContinue()
  59. {
  60. $time_passed = time() - $this->started_at;
  61. // Only continue if limit values are sane
  62. if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
  63. return false;
  64. }
  65. // If too much time has passed, stop
  66. if ($time_passed >= $this->max_execution_time ||
  67. $time_passed > ini_get('max_execution_time') - $this->max_execution_margin) {
  68. return false;
  69. }
  70. // If we have a max-item-limit, check if it has been passed
  71. if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
  72. return false;
  73. }
  74. return true;
  75. }
  76. public function poll()
  77. {
  78. $qm = $this->get();
  79. if ($qm->pollInterval() <= 0 && ! $qm instanceof UnQueueManager) {
  80. // Special case for UnQueueManager as it is a default plugin
  81. // and does not require queues, since it does everything immediately
  82. throw new ServerException('OpportunisticQM cannot work together' .
  83. 'with queues that do not implement polling');
  84. }
  85. ++$this->handled_items;
  86. return $qm->poll();
  87. }
  88. /**
  89. * Takes care of running through the queue items, returning when
  90. * the limits setup in __construct are met.
  91. *
  92. * @return true on workqueue finished, false if there are still items in the queue
  93. */
  94. public function runQueue()
  95. {
  96. while ($this->canContinue()) {
  97. if (!$this->poll()) {
  98. // Out of work
  99. return true;
  100. }
  101. }
  102. if ($this->handled_items > 0) {
  103. common_debug('Opportunistic queue manager passed execution time/item ' .
  104. 'handling limit without being out of work.');
  105. return true;
  106. } elseif ($this->verbosity > 1) {
  107. common_debug('Opportunistic queue manager did not have time to start ' .
  108. 'on this action (max: ' . $this->max_execution_time .
  109. ' exceeded: ' . abs(time() - $this->started_at) . ').');
  110. }
  111. return false;
  112. }
  113. }