opportunisticqueuemanager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_queue_items = null;
  22. protected $started_at = null;
  23. protected $handled_items = 0;
  24. const MAXEXECTIME = 30; // typically just used for the /main/cron action
  25. public function __construct(array $args=array()) {
  26. foreach (get_class_vars(get_class($this)) as $key=>$val) {
  27. if (array_key_exists($key, $args)) {
  28. $this->$key = $args[$key];
  29. }
  30. }
  31. $this->verifyKey();
  32. if ($this->started_at === null) {
  33. $this->started_at = time();
  34. }
  35. if ($this->max_execution_time === null) {
  36. $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
  37. }
  38. return parent::__construct();
  39. }
  40. protected function verifyKey()
  41. {
  42. if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
  43. throw new RunQueueBadKeyException($this->qmkey);
  44. }
  45. }
  46. public function canContinue()
  47. {
  48. $time_passed = time() - $this->started_at;
  49. // Only continue if limit values are sane
  50. if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
  51. return false;
  52. }
  53. // If too much time has passed, stop
  54. if ($time_passed >= $this->max_execution_time) {
  55. return false;
  56. }
  57. // If we have a max-item-limit, check if it has been passed
  58. if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. public function poll()
  64. {
  65. $this->handled_items++;
  66. if (!parent::poll()) {
  67. throw new RunQueueOutOfWorkException();
  68. }
  69. return true;
  70. }
  71. /**
  72. * Takes care of running through the queue items, returning when
  73. * the limits setup in __construct are met.
  74. *
  75. * @return true on workqueue finished, false if there are still items in the queue
  76. */
  77. public function runQueue()
  78. {
  79. while ($this->canContinue()) {
  80. try {
  81. $this->poll();
  82. } catch (RunQueueOutOfWorkException $e) {
  83. return true;
  84. }
  85. }
  86. common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');
  87. return false;
  88. }
  89. }