1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | <?php // // Open Web Analytics - An Open Source Web Analytics Framework // // Copyright 2006 Peter Adams. All rights reserved. // // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // $Id$ // if ( ! class_exists( 'eventQueue' ) ) { require_once( OWA_BASE_CLASS_DIR.'eventQueue.php' ); } /** * Database backed Event Queue Implementation * * @author Peter Adams <peter@openwebanalytics.com> * @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.4.0 */ class owa_dbEventQueue extends eventQueue { var $db; var $items_per_fetch = 50; function __construct($queue_dir = '') { $this->db = owa_coreAPI::dbSingleton(); return parent::__construct(); } function addToQueue($event) { $qi = owa_coreAPI::entityFactory('base.queue_item'); $serialized_event = serialize( $event ); $qi->set( 'id', $qi->generateId( $serialized_event) ); $qi->set( 'event_type', $event->getEventType() ); $qi->set( 'status', 'unhandled' ); $qi->set( 'priority', $this->determinPriority( $event->getEventType() ) ); $qi->set( 'event', $serialized_event ); $qi->set( 'insertion_timestamp', $this->makeTimestamp() ); $qi->set( 'insertion_datestamp', $this->makeDatestamp() ); $qi->save(); } function markAsFailed($item_id, $error_msg = '') { $qi = owa_coreAPI::entityFactory('base.queue_item'); $qi->load($item_id); $inserted_timestamp = $qi->get('insertion_timestamp'); if ($inserted_timestamp) { $qi->set( 'failed_attempt_count' , $qi->get( 'failed_attempt_count' ) + 1 ); $qi->set( 'last_attempt_timestamp', $this->makeTimestamp() ); $qi->set( 'not_before_timestamp', $this->determineNextAttempt($qi->get('event_type'), $qi->get('failed_attempt_count') ) ); $qi->set( 'last_error_msg', $error_msg); $qi->save(); } } function markAsHandled($item_id) { $qi = owa_coreAPI::entityFactory('base.queue_item'); $qi->load($item_id); $inserted_timestamp = $qi->get('insertion_timestamp'); if ($inserted_timestamp) { $qi->set( 'status', 'handled' ); $qi->set( 'handled_timestamp', $this->makeTimestamp() ); $qi->save(); } } function getNextItems($limit = '') { if ( ! $limit ) { $limit = $this->items_per_fetch; } $this->db->select( '*' ); $this->db->from( 'owa_queue_item' ); $this->db->where( 'status', 'unhandled' ); $this->db->where( 'not_before_timestamp', time(), '<' ); $this->db->orderBy( 'insertion_timestamp' , 'ASC' ); $this->db->limit( $limit ); $items = $this->db->getAllRows(); if ( $items ) { $entities = array(); foreach ( $items as $item ) { $qi = owa_coreAPI::entityFactory( 'base.queue_item' ); $qi->setProperties( $item ); $entities[] = $qi; } if ( $limit > 1 ) { return $entities; } else { return $entities[0]; } } } function getNextItem() { return $this->getNextItems(1); } function determineNextAttempt($event_type, $failed_count) { return $this->makeTimeStamp(time() + 30); } function makeTimestamp() { return time(); } // safe for mysql timestamp column type function makeDatestamp($time = '') { if ( ! $time ) { $time = time(); } return gmdate("Y-m-d H:i:s", $time); } function determinPriority($event_type) { return 99; } function processQueue() { $more = true; while( $more ) { $items = $this->getNextItems(); if ( $items ) { foreach ( $items as $item ) { owa_coreAPI::debug('About to dispatch queue item id: ' . $item->get( 'id' ) ); $event = unserialize( $item->get('event') ); $dispatch = owa_coreAPI::getEventDispatch(); $ret = $dispatch->notify( $event ); owa_coreAPI::debug($ret); $id = $item->get( 'id' ); if ( $ret === OWA_EHS_EVENT_HANDLED ) { $this->markAsHandled( $id ); owa_coreAPI::debug("EHS: marked item ($id) as handled."); } else { $this->markAsFailed( $id ); owa_coreAPI::debug("EHS: marked item ($id) as failed."); } } } else { $more = false; } } } } ?> |