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_queue_items = null;
23 protected $started_at = null;
24 protected $handled_items = 0;
26 const MAXEXECTIME = 30; // typically just used for the /main/cron action
28 public function __construct(array $args=array()) {
29 foreach (get_class_vars(get_class($this)) as $key=>$val) {
30 if (array_key_exists($key, $args)) {
31 $this->$key = $args[$key];
36 if ($this->started_at === null) {
37 $this->started_at = time();
40 if ($this->max_execution_time === null) {
41 $this->max_execution_time = ini_get('max_execution_time') ?: self::MAXEXECTIME;
44 return parent::__construct();
47 protected function verifyKey()
49 if ($this->qmkey !== common_config('opportunisticqm', 'qmkey')) {
50 throw new RunQueueBadKeyException($this->qmkey);
54 public function canContinue()
56 $time_passed = time() - $this->started_at;
58 // Only continue if limit values are sane
59 if ($time_passed <= 0 && (!is_null($this->max_queue_items) && $this->max_queue_items <= 0)) {
62 // If too much time has passed, stop
63 if ($time_passed >= $this->max_execution_time) {
66 // If we have a max-item-limit, check if it has been passed
67 if (!is_null($this->max_queue_items) && $this->handled_items >= $this->max_queue_items) {
74 public function poll()
76 $this->handled_items++;
77 if (!parent::poll()) {
78 throw new RunQueueOutOfWorkException();
83 // OpportunisticQM shouldn't discard items it can't handle, we're
84 // only here to take care of what we _can_ handle!
85 protected function noHandlerFound(Queue_item $qi, $rep=null) {
86 $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Releasing claim for queue item without a handler");
87 $this->_fail($qi, true); // true here means "releaseOnly", so no error statistics since it's not an _error_
90 protected function _fail(Queue_item $qi, $releaseOnly=false)
92 parent::_fail($qi, $releaseOnly);
93 $this->_log(LOG_DEBUG, "[{$qi->transport}:item {$qi->id}] Ignoring this transport for the rest of this execution");
94 $this->ignoreTransport($qi->transport);
98 * Takes care of running through the queue items, returning when
99 * the limits setup in __construct are met.
101 * @return true on workqueue finished, false if there are still items in the queue
103 public function runQueue()
105 while ($this->canContinue()) {
108 } catch (RunQueueOutOfWorkException $e) {
112 common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');