3 * Table Definition for queue_item
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
7 class Queue_item extends Managed_DataObject
10 /* the code below is auto generated do not remove the above tag */
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()
19 /* the code above is auto generated do not remove the tag below */
22 public static function schemaDef()
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'),
32 'primary key' => array('id'),
34 'queue_item_created_idx' => array('created'),
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.
43 static function top($transports=null, array $ignored_transports=array()) {
45 $qi = new Queue_item();
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')");
52 $qi->transport = $transports;
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')");
60 $qi->orderBy('created');
61 $qi->whereAdd('claimed is null');
65 $cnt = $qi->find(true);
68 // XXX: potential race condition
69 // can we force it to only update if claimed is still null
71 common_log(LOG_INFO, 'claiming queue item id = ' . $qi->getID() . ' for transport ' . $qi->transport);
73 $qi->claimed = common_sql_now();
74 $result = $qi->update($orig);
76 common_log(LOG_DEBUG, 'claim succeeded.');
79 common_log(LOG_ERR, 'claim of queue item id= ' . $qi->getID() . ' for transport ' . $qi->transport . ' failed.');
87 * Release a claimed item.
89 function releaseClaim()
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());
95 $this->claimed = null;