3 * GNU social queue-manager-on-visit class
5 * Will run events for a certain time, or until finished.
7 * Configure remote key if wanted with $config['opportunisticqm']['qmkey'] and
8 * use with /main/runqueue?qmkey=abc123
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/
17 class OpportunisticQueueManager extends DBQueueManager
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;
24 protected $started_at = null;
25 protected $handled_items = 0;
27 const MAXEXECTIME = 10; // typically just used for the /main/cron action, only used if php.ini max_execution_time is 0
29 public function __construct(array $args=array()) {
30 foreach (get_class_vars(get_class($this)) as $key=>$val) {
31 if (array_key_exists($key, $args)) {
32 $this->$key = $args[$key];
37 if ($this->started_at === null) {
38 $this->started_at = time();
41 if ($this->max_execution_time === null) {
42 $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
45 if ($this->max_execution_margin === null) {
46 $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.
49 return parent::__construct();
52 protected function verifyKey()
54 if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
55 throw new RunQueueBadKeyException($this->qmkey);
59 public function canContinue()
61 $time_passed = time() - $this->started_at;
63 // Only continue if limit values are sane
64 if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
67 // If too much time has passed, stop
68 if ($time_passed >= $this->max_execution_time || $time_passed > ini_get('max_execution_time') - $this->max_execution_margin) {
71 // If we have a max-item-limit, check if it has been passed
72 if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
79 public function poll()
81 $this->handled_items++;
82 if (!parent::poll()) {
83 throw new RunQueueOutOfWorkException();
88 // OpportunisticQM shouldn't discard items it can't handle, we're
89 // only here to take care of what we _can_ handle!
90 protected function noHandlerFound(Queue_item $qi, $rep=null) {
91 $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Releasing claim for queue item without a handler");
92 $this->_fail($qi, true); // true here means "releaseOnly", so no error statistics since it's not an _error_
95 protected function _fail(Queue_item $qi, $releaseOnly=false)
97 parent::_fail($qi, $releaseOnly);
98 $this->_log(LOG_DEBUG, "[{$qi->transport}:item {$qi->id}] Ignoring this transport for the rest of this execution");
99 $this->ignoreTransport($qi->transport);
103 * Takes care of running through the queue items, returning when
104 * the limits setup in __construct are met.
106 * @return true on workqueue finished, false if there are still items in the queue
108 public function runQueue()
110 while ($this->canContinue()) {
113 } catch (RunQueueOutOfWorkException $e) {
117 common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');