Elgg  Version 4.3
NotificationsQueue.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
5 use Elgg\Config;
10 
18 
22  const QUEUE_NAME = 'notifications';
23 
27  protected $config;
28 
36  public function __construct(string $name, \Elgg\Database $db, \Elgg\Config $config) {
37  parent::__construct($name, $db);
38 
39  $this->config = $config;
40  }
41 
45  public function dequeue() {
46  $delay = (int) $this->config->notifications_queue_delay;
47  if ($delay < 1) {
48  // no delay, so rely on parent logic
49  return parent::dequeue();
50  }
51 
52  // get a record for processing
53  $select = Select::fromTable(self::TABLE_NAME);
54  $select->select('*')
55  ->where($select->compare('name', '=', $this->name, ELGG_VALUE_STRING))
56  ->andWhere($select->expr()->isNull('worker'))
57  ->andWhere($select->compare('timestamp', '<', $this->getCurrentTime("-{$delay} seconds")->getTimestamp(), ELGG_VALUE_TIMESTAMP))
58  ->orderBy('id', 'ASC')
59  ->setMaxResults(1);
60 
61  $row = $this->db->getDataRow($select);
62  if (empty($row)) {
63  return;
64  }
65 
66  // lock a record for processing
67  $update = Update::table(self::TABLE_NAME);
68  $update->set('worker', $update->param($this->workerId, ELGG_VALUE_STRING))
69  ->where($update->compare('name', '=', $this->name, ELGG_VALUE_STRING))
70  ->andWhere($update->compare('id', '=', $row->id, ELGG_VALUE_ID))
71  ->andWhere($update->expr()->isNull('worker'));
72 
73  if ($this->db->updateData($update, true) !== 1) {
74  return;
75  }
76 
77  // remove locked record from database
78  $delete = Delete::fromTable(self::TABLE_NAME);
79  $delete->where($delete->compare('id', '=', $row->id, ELGG_VALUE_ID));
80 
81  $this->db->deleteData($delete);
82 
83  return unserialize($row->data);
84  }
85 }
dequeue()
Remove an item from the queue.mixed
__construct(string $name,\Elgg\Database $db,\Elgg\Config $config)
Create a queue.
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
if(!$enabled) if(PHP_SAPI!== 'cli')
$delete
const ELGG_VALUE_ID
Definition: constants.php:129
FIFO queue that uses the database for persistence.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:130
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:127
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
Database queue for notifications.