opportunisticqueuemanager.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 DBQueueManager
  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. const MAXEXECTIME = 20; // typically just used for the /main/cron action, only used if php.ini max_execution_time is 0
  27. public function __construct(array $args=array()) {
  28. foreach (get_class_vars(get_class($this)) as $key=>$val) {
  29. if (array_key_exists($key, $args)) {
  30. $this->$key = $args[$key];
  31. }
  32. }
  33. $this->verifyKey();
  34. if ($this->started_at === null) {
  35. $this->started_at = time();
  36. }
  37. if ($this->max_execution_time === null) {
  38. $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
  39. }
  40. if ($this->max_execution_margin === null) {
  41. $this->max_execution_margin = common_config('http', 'connect_timeout') + 1; // think PHP's max exec time, minus this value to have time for timeouts etc.
  42. }
  43. return parent::__construct();
  44. }
  45. protected function verifyKey()
  46. {
  47. if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
  48. throw new RunQueueBadKeyException($this->qmkey);
  49. }
  50. }
  51. public function canContinue()
  52. {
  53. $time_passed = time() - $this->started_at;
  54. // Only continue if limit values are sane
  55. if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
  56. return false;
  57. }
  58. // If too much time has passed, stop
  59. if ($time_passed >= $this->max_execution_time || $time_passed > ini_get('max_execution_time') - $this->max_execution_margin) {
  60. return false;
  61. }
  62. // If we have a max-item-limit, check if it has been passed
  63. if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
  64. return false;
  65. }
  66. return true;
  67. }
  68. public function poll()
  69. {
  70. $this->handled_items++;
  71. if (!parent::poll()) {
  72. throw new RunQueueOutOfWorkException();
  73. }
  74. return true;
  75. }
  76. // OpportunisticQM shouldn't discard items it can't handle, we're
  77. // only here to take care of what we _can_ handle!
  78. protected function noHandlerFound(Queue_item $qi, $rep=null) {
  79. $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Releasing claim for queue item without a handler");
  80. $this->_fail($qi, true); // true here means "releaseOnly", so no error statistics since it's not an _error_
  81. }
  82. protected function _fail(Queue_item $qi, $releaseOnly=false)
  83. {
  84. parent::_fail($qi, $releaseOnly);
  85. $this->_log(LOG_DEBUG, "[{$qi->transport}:item {$qi->id}] Ignoring this transport for the rest of this execution");
  86. $this->ignoreTransport($qi->transport);
  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. try {
  98. $this->poll();
  99. } catch (RunQueueOutOfWorkException $e) {
  100. return true;
  101. }
  102. }
  103. if ($this->handled_items > 0) {
  104. common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');
  105. } elseif ($this->verbosity > 1) {
  106. common_debug('Opportunistic queue manager did not have time to start on this action (max: '.$this->max_execution_time.' exceeded: '.abs(time()-$this->started_at).').');
  107. }
  108. return false;
  109. }
  110. }