]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Queue_item.php
Merge branch 'fix-setpassword' into 'nightly'
[quix0rs-gnu-social.git] / classes / Queue_item.php
1 <?php
2 /**
3  * Table Definition for queue_item
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Queue_item extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'queue_item';                      // table name
13     public $id;                              // int(4)  primary_key not_null
14     public $frame;                           // blob not_null
15     public $transport;                       // varchar(32)
16     public $created;                         // datetime()   not_null
17     public $claimed;                         // datetime()
18
19     /* the code above is auto generated do not remove the tag below */
20     ###END_AUTOCODE
21
22     public static function schemaDef()
23     {
24         return array(
25             'fields' => array(
26                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
27                 'frame' => array('type' => 'blob', 'not null' => true, 'description' => 'data: object reference or opaque string'),
28                 'transport' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'queue for what? "email", "xmpp", "sms", "irc", ...'),
29                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
30                 'claimed' => array('type' => 'datetime', 'description' => 'date this item was claimed'),
31             ),
32             'primary key' => array('id'),
33             'indexes' => array(
34                 'queue_item_created_idx' => array('created'),
35             ),
36         );
37     }
38
39     /**
40      * @param mixed $transports name of a single queue or array of queues to pull from
41      *                          If not specified, checks all queues in the system.
42      */
43     static function top($transports=null, array $ignored_transports=array()) {
44
45         $qi = new Queue_item();
46         if ($transports) {
47             if (is_array($transports)) {
48                 // @fixme use safer escaping
49                 $list = implode("','", array_map(array($qi, 'escape'), $transports));
50                 $qi->whereAdd("transport in ('$list')");
51             } else {
52                 $qi->transport = $transports;
53             }
54         }
55         if (!empty($ignored_transports)) {
56             // @fixme use safer escaping
57             $list = implode("','", array_map(array($qi, 'escape'), $ignored_transports));
58             $qi->whereAdd("transport NOT IN ('$list')");
59         }
60         $qi->orderBy('created');
61         $qi->whereAdd('claimed is null');
62
63         $qi->limit(1);
64
65         $cnt = $qi->find(true);
66
67         if ($cnt) {
68             // XXX: potential race condition
69             // can we force it to only update if claimed is still null
70             // (or old)?
71             common_log(LOG_INFO, 'claiming queue item id = ' . $qi->getID() .
72                 ' for transport ' . $qi->transport);
73             $orig = clone($qi);
74             $qi->claimed = common_sql_now();
75             $result = $qi->update($orig);
76             if ($result) {
77                 common_log(LOG_INFO, 'claim succeeded.');
78                 return $qi;
79             } else {
80                 common_log(LOG_INFO, 'claim failed.');
81             }
82         }
83         $qi = null;
84         return null;
85     }
86
87     /**
88      * Release a claimed item.
89      */
90     function releaseClaim()
91     {
92         // DB_DataObject doesn't let us save nulls right now
93         $sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->getID());
94         $this->query($sql);
95
96         $this->claimed = null;
97         $this->encache();
98     }
99 }