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