def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ assert event_object topic = self._topic(event_object) to_name = (self._send_name.exchange, topic) log.trace("Publishing event message to %s", to_name) current_time = int(get_ion_ts()) #Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest("The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) > ( current_time + VALID_EVENT_TIME_PERIOD ): raise BadRequest("This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD) : raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) #Validate this object #TODO - enable this once the resource agent issue sending a dict is figured out #event_object._validate() #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest("The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise try: # store published event but only if we specified an event_repo if PERSIST_ON_PUBLISH and self.event_repo: self.event_repo.put_event(event_object) except Exception as ex: log.exception("Failed to store published event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ if not event_object: raise BadRequest("Must provide event_object") event_object.base_types = event_object._get_extends() topic = self._topic( event_object ) # Routing key generated using type_, base_types, origin, origin_type, sub_type to_name = (self._send_name.exchange, topic) current_time = get_ion_ts_millis() # Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest("The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) > (current_time + VALID_EVENT_TIME_PERIOD): raise BadRequest("This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD): raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) # Set the actor id based on if not event_object.actor_id: event_object.actor_id = self._get_actor_id() # Validate this object - ideally the validator should pass on problems, but for now just log # any errors and keep going, since seeing invalid situations are better than skipping validation. try: event_object._validate() except Exception, e: log.exception(e)
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ assert event_object topic = self._topic(event_object) to_name = (self._send_name.exchange, topic) log.trace("Publishing %s event message %s:%s -> %s", event_object.type_, event_object.origin_type, event_object.origin, to_name) current_time = int(get_ion_ts()) #Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest("The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) > ( current_time + VALID_EVENT_TIME_PERIOD ): raise BadRequest("This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD) : raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) #Validate this object - ideally the validator should pass on problems, but for now just log #any errors and keep going, since seeing invalid situations are better than skipping validation. try: event_object._validate() except Exception, e: log.exception(e)
def test_is_valid_ts(self): # Not a string self.assertEqual(is_valid_ts(1332424), False) # Too short self.assertEqual(is_valid_ts("1332424"), False) # Not numeric self.assertEqual(is_valid_ts("bfd1332424"), False) # Neither numeric or positive self.assertEqual(is_valid_ts("-332424"), False) # Too long self.assertEqual(is_valid_ts("109392939394556"), False) # Just right ts = get_ion_ts() self.assertEqual(is_valid_ts(ts), True)
def test_is_valid_ts(self): #Not a string self.assertEqual(is_valid_ts(1332424), False) #Too short self.assertEqual(is_valid_ts('1332424'), False) #Not numeric self.assertEqual(is_valid_ts('bfd1332424'), False) #Neither numeric or positive self.assertEqual(is_valid_ts('-332424'), False) #Too long self.assertEqual(is_valid_ts('109392939394556'), False) #Just right ts = get_ion_ts() self.assertEqual(is_valid_ts(ts), True)
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ assert event_object topic = self._topic(event_object) to_name = (self._send_name.exchange, topic) log.trace("Publishing event message to %s", to_name) current_time = int(get_ion_ts()) #Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest( "The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) > (current_time + VALID_EVENT_TIME_PERIOD): raise BadRequest( "This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) #Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD): raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) #Validate this object #TODO - enable this once the resource agent issue sending a dict is figured out #event_object._validate() #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest( "The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise try: # store published event but only if we specified an event_repo if PERSIST_ON_PUBLISH and self.event_repo: self.event_repo.put_event(event_object) except Exception as ex: log.exception("Failed to store published event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ if not event_object: raise BadRequest("Must provide event_object") event_object.base_types = event_object._get_extends() topic = self._topic( event_object ) # Routing key generated using type_, base_types, origin, origin_type, sub_type container = (hasattr(self, '_process') and hasattr( self._process, 'container') and self._process.container ) or BaseEndpoint._get_container_instance() if container and container.has_capability( container.CCAP.EXCHANGE_MANAGER): # make sure we are an xp, if not, upgrade if not isinstance(self._send_name, XOTransport): default_nt = NameTrio(self.get_events_exchange_point()) if isinstance(self._send_name, NameTrio) \ and self._send_name.exchange == default_nt.exchange \ and self._send_name.queue == default_nt.queue \ and self._send_name.binding == default_nt.binding: self._send_name = container.create_xp(self._events_xp) else: self._send_name = container.create_xp(self._send_name) xp = self._send_name to_name = xp.create_route(topic) else: to_name = (self._send_name.exchange, topic) current_time = get_ion_ts_millis() # Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest( "The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) > (current_time + VALID_EVENT_TIME_PERIOD): raise BadRequest( "This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD): raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) # Set the actor id based on if not event_object.actor_id: event_object.actor_id = self._get_actor_id() #Validate this object - ideally the validator should pass on problems, but for now just log #any errors and keep going, since seeing invalid situations are better than skipping validation. try: event_object._validate() except Exception as e: log.exception(e) #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest( "The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise return event_object
def publish_event_object(self, event_object): """ Publishes an event of given type for the given origin. Event_type defaults to an event_type set when initializing the EventPublisher. Other kwargs fill out the fields of the event. This operation will fail with an exception. @param event_object the event object to be published @retval event_object the event object which was published """ if not event_object: raise BadRequest("Must provide event_object") event_object.base_types = event_object._get_extends() topic = self._topic(event_object) # Routing key generated using type_, base_types, origin, origin_type, sub_type container = (hasattr(self, '_process') and hasattr(self._process, 'container') and self._process.container) or BaseEndpoint._get_container_instance() if container and container.has_capability(container.CCAP.EXCHANGE_MANAGER): # make sure we are an xp, if not, upgrade if not isinstance(self._send_name, XOTransport): default_nt = NameTrio(self.get_events_exchange_point()) if isinstance(self._send_name, NameTrio) \ and self._send_name.exchange == default_nt.exchange \ and self._send_name.queue == default_nt.queue \ and self._send_name.binding == default_nt.binding: self._send_name = container.create_xp(self._events_xp) else: self._send_name = container.create_xp(self._send_name) xp = self._send_name to_name = xp.create_route(topic) else: to_name = (self._send_name.exchange, topic) current_time = get_ion_ts_millis() # Ensure valid created timestamp if supplied if event_object.ts_created: if not is_valid_ts(event_object.ts_created): raise BadRequest("The ts_created value is not a valid timestamp: '%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) > ( current_time + VALID_EVENT_TIME_PERIOD ): raise BadRequest("This ts_created value is too far in the future:'%s'" % (event_object.ts_created)) # Reject events that are older than specified time if int(event_object.ts_created) < (current_time - VALID_EVENT_TIME_PERIOD) : raise BadRequest("This ts_created value is too old:'%s'" % (event_object.ts_created)) else: event_object.ts_created = str(current_time) # Set the actor id based on if not event_object.actor_id: event_object.actor_id = self._get_actor_id() #Validate this object - ideally the validator should pass on problems, but for now just log #any errors and keep going, since seeing invalid situations are better than skipping validation. try: event_object._validate() except Exception as e: log.exception(e) #Ensure the event object has a unique id if '_id' in event_object: raise BadRequest("The event object cannot contain a _id field '%s'" % (event_object)) #Generate a unique ID for this event event_object._id = create_unique_event_id() try: self.publish(event_object, to_name=to_name) except Exception as ex: log.exception("Failed to publish event (%s): '%s'" % (ex.message, event_object)) raise return event_object