2008-07-04 13:07:46 +09:00
|
|
|
<?php
|
2020-06-29 07:41:46 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2008-07-04 13:07:46 +09:00
|
|
|
/**
|
|
|
|
* Table Definition for queue_item
|
|
|
|
*/
|
2020-06-29 07:41:46 +09:00
|
|
|
|
|
|
|
defined('GNUSOCIAL') || die();
|
2008-07-04 13:07:46 +09:00
|
|
|
|
2011-08-23 06:52:02 +09:00
|
|
|
class Queue_item extends Managed_DataObject
|
2008-07-04 13:07:46 +09:00
|
|
|
{
|
|
|
|
###START_AUTOCODE
|
|
|
|
/* the code below is auto generated do not remove the above tag */
|
|
|
|
|
|
|
|
public $__table = 'queue_item'; // table name
|
2010-01-23 05:52:36 +09:00
|
|
|
public $id; // int(4) primary_key not_null
|
|
|
|
public $frame; // blob not_null
|
2013-09-18 08:21:53 +09:00
|
|
|
public $transport; // varchar(32)
|
2020-06-29 07:41:46 +09:00
|
|
|
public $created; // datetime()
|
2009-06-29 03:38:34 +09:00
|
|
|
public $claimed; // datetime()
|
2008-07-04 13:07:46 +09:00
|
|
|
|
|
|
|
/* the code above is auto generated do not remove the tag below */
|
|
|
|
###END_AUTOCODE
|
2008-07-10 07:46:30 +09:00
|
|
|
|
2011-08-23 06:52:02 +09:00
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'fields' => array(
|
|
|
|
'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
|
|
|
|
'frame' => array('type' => 'blob', 'not null' => true, 'description' => 'data: object reference or opaque string'),
|
2013-09-18 08:21:53 +09:00
|
|
|
'transport' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'queue for what? "email", "xmpp", "sms", "irc", ...'),
|
2020-06-29 07:41:46 +09:00
|
|
|
'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
|
2011-08-23 06:52:02 +09:00
|
|
|
'claimed' => array('type' => 'datetime', 'description' => 'date this item was claimed'),
|
|
|
|
),
|
|
|
|
'primary key' => array('id'),
|
|
|
|
'indexes' => array(
|
|
|
|
'queue_item_created_idx' => array('created'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-01-23 05:52:36 +09: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.
|
|
|
|
*/
|
2020-06-29 07:41:46 +09:00
|
|
|
public static function top($transports = null, array $ignored_transports = [])
|
|
|
|
{
|
2008-12-24 04:19:07 +09:00
|
|
|
$qi = new Queue_item();
|
2010-01-23 05:52:36 +09:00
|
|
|
if ($transports) {
|
|
|
|
if (is_array($transports)) {
|
2020-08-24 22:16:00 +09:00
|
|
|
$qi->whereAddIn(
|
|
|
|
'transport',
|
|
|
|
$transports,
|
|
|
|
$qi->columnType('transport')
|
|
|
|
);
|
2010-01-23 05:52:36 +09:00
|
|
|
} else {
|
|
|
|
$qi->transport = $transports;
|
|
|
|
}
|
2010-01-13 12:57:15 +09:00
|
|
|
}
|
2015-07-18 08:09:50 +09:00
|
|
|
if (!empty($ignored_transports)) {
|
2020-08-24 22:16:00 +09:00
|
|
|
$qi->whereAddIn(
|
|
|
|
'!transport',
|
|
|
|
$ignored_transports,
|
|
|
|
$qi->columnType('transport')
|
|
|
|
);
|
2015-07-18 08:09:50 +09:00
|
|
|
}
|
2020-08-24 22:16:00 +09:00
|
|
|
$qi->whereAdd('claimed IS NULL');
|
2008-12-24 04:19:07 +09:00
|
|
|
$qi->orderBy('created');
|
|
|
|
|
|
|
|
$qi->limit(1);
|
|
|
|
|
2008-12-24 04:44:28 +09:00
|
|
|
$cnt = $qi->find(true);
|
2008-12-24 04:19:07 +09:00
|
|
|
|
|
|
|
if ($cnt) {
|
2011-03-23 00:54:23 +09:00
|
|
|
// XXX: potential race condition
|
|
|
|
// can we force it to only update if claimed is still null
|
|
|
|
// (or old)?
|
2015-12-31 21:00:20 +09:00
|
|
|
common_log(LOG_INFO, 'claiming queue item id = ' . $qi->getID() . ' for transport ' . $qi->transport);
|
2008-12-24 04:19:07 +09:00
|
|
|
$orig = clone($qi);
|
|
|
|
$qi->claimed = common_sql_now();
|
|
|
|
$result = $qi->update($orig);
|
|
|
|
if ($result) {
|
2015-12-31 21:00:20 +09:00
|
|
|
common_log(LOG_DEBUG, 'claim succeeded.');
|
2008-12-24 04:19:07 +09:00
|
|
|
return $qi;
|
|
|
|
} else {
|
2015-12-31 21:04:50 +09:00
|
|
|
common_log(LOG_ERR, 'claim of queue item id= ' . $qi->getID() . ' for transport ' . $qi->transport . ' failed.');
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 22:16:00 +09:00
|
|
|
unset($qi);
|
2008-12-24 04:21:29 +09:00
|
|
|
return null;
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2010-06-27 04:07:32 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release a claimed item.
|
|
|
|
*/
|
2020-06-29 07:41:46 +09:00
|
|
|
public function releaseClaim()
|
2010-06-27 04:07:32 +09:00
|
|
|
{
|
2020-07-28 01:10:33 +09:00
|
|
|
// @fixme Consider $this->sqlValue('NULL')
|
|
|
|
$ret = $this->query(sprintf(
|
2020-08-10 23:46:30 +09:00
|
|
|
'UPDATE queue_item SET claimed = NULL WHERE id = %d',
|
2020-07-28 01:10:33 +09:00
|
|
|
$this->getID()
|
|
|
|
));
|
2010-06-27 04:07:32 +09:00
|
|
|
|
2020-07-28 01:10:33 +09:00
|
|
|
if ($ret) {
|
|
|
|
$this->claimed = null;
|
|
|
|
$this->encache();
|
|
|
|
}
|
2010-06-27 04:07:32 +09:00
|
|
|
}
|
2008-07-04 13:07:46 +09:00
|
|
|
}
|