2008-07-04 05:07:46 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Table Definition for queue_item
|
|
|
|
*/
|
2008-09-26 17:18:24 +01:00
|
|
|
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
|
2008-07-04 05:07:46 +01:00
|
|
|
|
2009-06-28 19:38:34 +01:00
|
|
|
class Queue_item extends Memcached_DataObject
|
2008-07-04 05:07:46 +01:00
|
|
|
{
|
|
|
|
###START_AUTOCODE
|
|
|
|
/* the code below is auto generated do not remove the above tag */
|
|
|
|
|
|
|
|
public $__table = 'queue_item'; // table name
|
2010-01-22 20:52:36 +00:00
|
|
|
public $id; // int(4) primary_key not_null
|
|
|
|
public $frame; // blob not_null
|
2008-07-04 05:07:46 +01:00
|
|
|
public $created; // datetime() not_null
|
2009-06-28 19:38:34 +01:00
|
|
|
public $claimed; // datetime()
|
2008-07-04 05:07:46 +01:00
|
|
|
|
|
|
|
/* Static get */
|
2008-12-23 19:33:23 +00:00
|
|
|
function staticGet($k,$v=null)
|
|
|
|
{ return Memcached_DataObject::staticGet('Queue_item',$k,$v); }
|
2008-07-04 05:07:46 +01:00
|
|
|
|
|
|
|
/* the code above is auto generated do not remove the tag below */
|
|
|
|
###END_AUTOCODE
|
2008-07-09 23:46:30 +01:00
|
|
|
|
2010-01-22 20:52:36 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $transports name of a single queue or array of queues to pull from
|
|
|
|
* If not specified, checks all queues in the system.
|
|
|
|
*/
|
|
|
|
static function top($transports=null) {
|
2008-12-23 19:19:07 +00:00
|
|
|
|
|
|
|
$qi = new Queue_item();
|
2010-01-22 20:52:36 +00:00
|
|
|
if ($transports) {
|
|
|
|
if (is_array($transports)) {
|
|
|
|
// @fixme use safer escaping
|
|
|
|
$list = implode("','", array_map('addslashes', $transports));
|
|
|
|
$qi->whereAdd("transport in ('$list')");
|
|
|
|
} else {
|
|
|
|
$qi->transport = $transports;
|
|
|
|
}
|
2010-01-13 03:57:15 +00:00
|
|
|
}
|
2008-12-23 19:19:07 +00:00
|
|
|
$qi->orderBy('created');
|
2008-12-23 19:21:29 +00:00
|
|
|
$qi->whereAdd('claimed is null');
|
2008-12-23 19:19:07 +00:00
|
|
|
|
|
|
|
$qi->limit(1);
|
|
|
|
|
2008-12-23 19:44:28 +00:00
|
|
|
$cnt = $qi->find(true);
|
2008-12-23 19:19:07 +00:00
|
|
|
|
|
|
|
if ($cnt) {
|
|
|
|
# XXX: potential race condition
|
2008-12-23 19:21:29 +00:00
|
|
|
# can we force it to only update if claimed is still null
|
2008-12-23 19:19:07 +00:00
|
|
|
# (or old)?
|
2010-01-22 20:52:36 +00:00
|
|
|
common_log(LOG_INFO, 'claiming queue item id = ' . $qi->id .
|
2010-01-13 03:57:15 +00:00
|
|
|
' for transport ' . $qi->transport);
|
2008-12-23 19:19:07 +00:00
|
|
|
$orig = clone($qi);
|
|
|
|
$qi->claimed = common_sql_now();
|
|
|
|
$result = $qi->update($orig);
|
|
|
|
if ($result) {
|
|
|
|
common_log(LOG_INFO, 'claim succeeded.');
|
|
|
|
return $qi;
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, 'claim failed.');
|
|
|
|
}
|
|
|
|
}
|
2008-12-23 19:21:29 +00:00
|
|
|
$qi = null;
|
|
|
|
return null;
|
2008-12-23 19:19:07 +00:00
|
|
|
}
|
2010-06-26 20:07:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release a claimed item.
|
|
|
|
*/
|
|
|
|
function releaseCLaim()
|
|
|
|
{
|
|
|
|
// DB_DataObject doesn't let us save nulls right now
|
|
|
|
$sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->id);
|
|
|
|
$this->query($sql);
|
|
|
|
|
|
|
|
$this->claimed = null;
|
|
|
|
$this->encache();
|
|
|
|
}
|
2008-07-04 05:07:46 +01:00
|
|
|
}
|