]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Queue_item.php
Merge commit 'refs/merge-requests/120' of git://gitorious.org/statusnet/mainline...
[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 Memcached_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 $created;                         // datetime()   not_null
16     public $claimed;                         // datetime()
17
18     /* Static get */
19     function staticGet($k,$v=null)
20     { return Memcached_DataObject::staticGet('Queue_item',$k,$v); }
21
22     /* the code above is auto generated do not remove the tag below */
23     ###END_AUTOCODE
24
25     /**
26      * @param mixed $transports name of a single queue or array of queues to pull from
27      *                          If not specified, checks all queues in the system.
28      */
29     static function top($transports=null) {
30
31         $qi = new Queue_item();
32         if ($transports) {
33             if (is_array($transports)) {
34                 // @fixme use safer escaping
35                 $list = implode("','", array_map('addslashes', $transports));
36                 $qi->whereAdd("transport in ('$list')");
37             } else {
38                 $qi->transport = $transports;
39             }
40         }
41         $qi->orderBy('created');
42         $qi->whereAdd('claimed is null');
43
44         $qi->limit(1);
45
46         $cnt = $qi->find(true);
47
48         if ($cnt) {
49             # XXX: potential race condition
50             # can we force it to only update if claimed is still null
51             # (or old)?
52             common_log(LOG_INFO, 'claiming queue item id = ' . $qi->id .
53                 ' for transport ' . $qi->transport);
54             $orig = clone($qi);
55             $qi->claimed = common_sql_now();
56             $result = $qi->update($orig);
57             if ($result) {
58                 common_log(LOG_INFO, 'claim succeeded.');
59                 return $qi;
60             } else {
61                 common_log(LOG_INFO, 'claim failed.');
62             }
63         }
64         $qi = null;
65         return null;
66     }
67 }