/modules/base/classes/dbEventQueue.php

https://github.com/sseshachala/Open-Web-Analytics · PHP · 187 lines · 121 code · 39 blank · 27 comment · 12 complexity · 21c90302040434df1b35f245d763a9b3 MD5 · raw file

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