Elgg  Version master
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 }
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
dequeue()
Remove an item from the queue.mixed
__construct(string $name,\Elgg\Database $db,\Elgg\Config $config)
Create a queue.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH)!== '/') if(file_exists(elgg_get_root_path(). 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
The Elgg database.
Definition: Database.php:26
$delete
const ELGG_VALUE_ID
Definition: constants.php:114
FIFO queue that uses the database for persistence.
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
Database queue for notifications.