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();
84 * Takes care of running through the queue items, returning when
85 * the limits setup in __construct are met.
87 * @return true on workqueue finished, false if there are still items in the queue
89 public function runQueue()
91 while ($this->canContinue()) {
94 } catch (RunQueueOutOfWorkException $e) {
98 common_debug('Opportunistic queue manager passed execution time/item handling limit without being out of work.');