]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Queue_item.php
Merge commit 'refs/merge-requests/164' 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 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 $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     public static function schemaDef()
26     {
27         return array(
28             'fields' => array(
29                 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
30                 'frame' => array('type' => 'blob', 'not null' => true, 'description' => 'data: object reference or opaque string'),
31                 'transport' => array('type' => 'varchar', 'length' => 8, 'not null' => true, 'description' => 'queue for what? "email", "xmpp", "sms", "irc", ...'), // @fixme 8 chars is too short; bump up.
32                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
33                 'claimed' => array('type' => 'datetime', 'description' => 'date this item was claimed'),
34             ),
35             'primary key' => array('id'),
36             'indexes' => array(
37                 'queue_item_created_idx' => array('created'),
38             ),
39         );
40     }
41
42     /**
43      * @param mixed $transports name of a single queue or array of queues to pull from
44      *                          If not specified, checks all queues in the system.
45      */
46     static function top($transports=null) {
47
48         $qi = new Queue_item();
49         if ($transports) {
50             if (is_array($transports)) {
51                 // @fixme use safer escaping
52                 $list = implode("','", array_map(array($qi, 'escape'), $transports));
53                 $qi->whereAdd("transport in ('$list')");
54             } else {
55                 $qi->transport = $transports;
56             }
57         }
58         $qi->orderBy('created');
59         $qi->whereAdd('claimed is null');
60
61         $qi->limit(1);
62
63         $cnt = $qi->find(true);
64
65         if ($cnt) {
66             // XXX: potential race condition
67             // can we force it to only update if claimed is still null
68             // (or old)?
69             common_log(LOG_INFO, 'claiming queue item id = ' . $qi->id .
70                 ' for transport ' . $qi->transport);
71             $orig = clone($qi);
72             $qi->claimed = common_sql_now();
73             $result = $qi->update($orig);
74             if ($result) {
75                 common_log(LOG_INFO, 'claim succeeded.');
76                 return $qi;
77             } else {
78                 common_log(LOG_INFO, 'claim failed.');
79             }
80         }
81         $qi = null;
82         return null;
83     }
84
85     /**
86      * Release a claimed item.
87      */
88     function releaseCLaim()
89     {
90         // DB_DataObject doesn't let us save nulls right now
91         $sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->id);
92         $this->query($sql);
93
94         $this->claimed = null;
95         $this->encache();
96     }
97 }