| 
									
										
										
										
											2008-07-04 00:07:46 -04:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Table Definition for queue_item | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-09-26 12:18:24 -04:00
										 |  |  | require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; | 
					
						
							| 
									
										
										
										
											2008-07-04 00:07:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  | class Queue_item extends Managed_DataObject | 
					
						
							| 
									
										
										
										
											2008-07-04 00:07:46 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     ###START_AUTOCODE
 | 
					
						
							|  |  |  |     /* the code below is auto generated do not remove the above tag */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public $__table = 'queue_item';                      // table name
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:52:36 -08:00
										 |  |  |     public $id;                              // int(4)  primary_key not_null
 | 
					
						
							|  |  |  |     public $frame;                           // blob not_null
 | 
					
						
							| 
									
										
										
										
											2013-09-18 01:21:53 +02:00
										 |  |  |     public $transport;                       // varchar(32)
 | 
					
						
							| 
									
										
										
										
											2008-07-04 00:07:46 -04:00
										 |  |  |     public $created;                         // datetime()   not_null
 | 
					
						
							| 
									
										
										
										
											2009-06-28 14:38:34 -04:00
										 |  |  |     public $claimed;                         // datetime()
 | 
					
						
							| 
									
										
										
										
											2008-07-04 00:07:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* the code above is auto generated do not remove the tag below */ | 
					
						
							|  |  |  |     ###END_AUTOCODE
 | 
					
						
							| 
									
										
										
										
											2008-07-09 18:46:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04: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 01:21:53 +02:00
										 |  |  |                 'transport' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'queue for what? "email", "xmpp", "sms", "irc", ...'), | 
					
						
							| 
									
										
										
										
											2011-08-22 17:52:02 -04:00
										 |  |  |                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), | 
					
						
							|  |  |  |                 'claimed' => array('type' => 'datetime', 'description' => 'date this item was claimed'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('id'), | 
					
						
							|  |  |  |             'indexes' => array( | 
					
						
							|  |  |  |                 'queue_item_created_idx' => array('created'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:52:36 -08: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. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-18 01:09:50 +02:00
										 |  |  |     static function top($transports=null, array $ignored_transports=array()) { | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $qi = new Queue_item(); | 
					
						
							| 
									
										
										
										
											2010-01-22 12:52:36 -08:00
										 |  |  |         if ($transports) { | 
					
						
							|  |  |  |             if (is_array($transports)) { | 
					
						
							|  |  |  |                 // @fixme use safer escaping
 | 
					
						
							| 
									
										
										
										
											2010-11-19 15:06:26 -08:00
										 |  |  |                 $list = implode("','", array_map(array($qi, 'escape'), $transports)); | 
					
						
							| 
									
										
										
										
											2010-01-22 12:52:36 -08:00
										 |  |  |                 $qi->whereAdd("transport in ('$list')"); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $qi->transport = $transports; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-01-12 19:57:15 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-18 01:09:50 +02:00
										 |  |  |         if (!empty($ignored_transports)) { | 
					
						
							|  |  |  |             // @fixme use safer escaping
 | 
					
						
							|  |  |  |             $list = implode("','", array_map(array($qi, 'escape'), $ignored_transports)); | 
					
						
							|  |  |  |             $qi->whereAdd("transport NOT IN ('$list')"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |         $qi->orderBy('created'); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:21:29 -05:00
										 |  |  |         $qi->whereAdd('claimed is null'); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $qi->limit(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-23 14:44:28 -05:00
										 |  |  |         $cnt = $qi->find(true); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($cnt) { | 
					
						
							| 
									
										
										
										
											2011-03-22 11:54:23 -04:00
										 |  |  |             // XXX: potential race condition
 | 
					
						
							|  |  |  |             // can we force it to only update if claimed is still null
 | 
					
						
							|  |  |  |             // (or old)?
 | 
					
						
							| 
									
										
										
										
											2015-06-04 22:27:53 +02:00
										 |  |  |             common_log(LOG_INFO, 'claiming queue item id = ' . $qi->getID() . | 
					
						
							| 
									
										
										
										
											2010-01-12 19:57:15 -08:00
										 |  |  |                 ' for transport ' . $qi->transport); | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05: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 14:21:29 -05:00
										 |  |  |         $qi = null; | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2008-12-23 14:19:07 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-06-26 15:07:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Release a claimed item. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-04-09 12:15:34 +02:00
										 |  |  |     function releaseClaim() | 
					
						
							| 
									
										
										
										
											2010-06-26 15:07:32 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         // DB_DataObject doesn't let us save nulls right now
 | 
					
						
							| 
									
										
										
										
											2015-06-04 22:27:53 +02:00
										 |  |  |         $sql = sprintf("UPDATE queue_item SET claimed=NULL WHERE id=%d", $this->getID()); | 
					
						
							| 
									
										
										
										
											2010-06-26 15:07:32 -04:00
										 |  |  |         $this->query($sql); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->claimed = null; | 
					
						
							|  |  |  |         $this->encache(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-07-04 00:07:46 -04:00
										 |  |  | } |