]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Queue_item.php
Merge remote-tracking branch 'upstream/master' into social-master
[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() . ' for transport ' . $qi->transport);
72             $orig = clone($qi);
73             $qi->claimed = common_sql_now();
74             $result = $qi->update($orig);
75             if ($result) {
76                 common_log(LOG_DEBUG, 'claim succeeded.');
77                 return $qi;
78             } else {
79                 common_log(LOG_ERR, 'claim of queue item id= ' . $qi->getID() . ' for transport ' . $qi->transport . ' failed.');
80             }
81         }
82         $qi = null;
83         return null;
84     }
85
86     /**
87      * Release a claimed item.
88      */
89     function releaseClaim()
90     {
91         // DB_DataObject doesn't let us save nulls right now
92         $sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->getID());
93         $this->query($sql);
94
95         $this->claimed = null;
96         $this->encache();
97     }
98 }