Beispiel #1
0
 def get(self, timeout=None):
     """Return a published message or None if a timeout has happend.
     """
     if timeout:
         timeout *= 1000.
     self.subscriber.connect(self.destination)
     poller = zmq.Poller()
     poller.register(self.subscriber, zmq.POLLIN)
     try:
         while(True):
             try:
                 ret = poller.poll(timeout=timeout)
                 if ret:
                     if ret[0][0] == self.subscriber:
                         msg = Message.decode(
                             self.subscriber.recv(zmq.NOBLOCK))
                         yield msg
                     else:
                         logger.error("WHAT THE HECK")
                 else:
                     # timeout
                     yield None
             except zmq.ZMQError:
                 logger.exception('recv failed')
     finally:
         poller.unregister(self.subscriber)
         self.subscriber.close()        
Beispiel #2
0
 def get(self, timeout=None):
     """Return a published message or None if a timeout has happend.
     """
     if timeout:
         timeout *= 1000.
     self.subscriber.connect(self.destination)
     poller = zmq.Poller()
     poller.register(self.subscriber, zmq.POLLIN)
     try:
         while (True):
             try:
                 ret = poller.poll(timeout=timeout)
                 if ret:
                     if ret[0][0] == self.subscriber:
                         msg = Message.decode(
                             self.subscriber.recv(zmq.NOBLOCK))
                         yield msg
                     else:
                         logger.error("WHAT THE HECK")
                 else:
                     # timeout
                     yield None
             except zmq.ZMQError:
                 logger.exception('recv failed')
     finally:
         poller.unregister(self.subscriber)
         self.subscriber.close()
Beispiel #3
0
def check_and_publish(datatype, rpc_metadata, publish, heartbeat):
    """Check for new files of type *datatype*, with the given *rpc_metadata*
    and publish them through *publish*.
    """
    stamp_config = DatexLastStamp(datatype)

    def younger_than_stamp_files():
        """Uses glob polling to get new files.
        """
        fdir, fglob = datex_config.get_path(datatype)
        del fglob
        fstamp = stamp_config.get_last_stamp()
        for fname, ftime in _get_file_list(datatype,
                                           time_start=fstamp + TIME_EPSILON):
            if datex_config.distribute(datatype):
                yield os.path.join(fdir, fname)
            stamp_config.update_last_stamp(ftime)

    # Give the publisher a little time to initialize
    # (e.g reconnections from subscribers)
    time.sleep(1)
    logger.info('publisher starting for datatype %s' % datatype)
    if heartbeat:
        last_heartbeat = datetime.now() - timedelta(seconds=heartbeat + 1)
    try:
        while (True):
            if (heartbeat and
                (datetime.now() - last_heartbeat).seconds >= heartbeat):
                # Send a heartbeat
                msg = Message('/hearbeat/' + datatype, 'heartbeat',
                              datetime.utcnow().isoformat())
                logger.info('sending: ' + str(msg))
                try:
                    publish.send(str(msg))
                    last_heartbeat = datetime.now()
                except zmq.ZMQError:
                    logger.exception('publish failed')
            for filedesc in younger_than_stamp_files():
                # Publish new files
                data = copy.copy(rpc_metadata)
                data['uri'] += os.path.basename(filedesc)
                msg = Message('/' + datatype, 'file', data)
                logger.info('sending: ' + str(msg))
                try:
                    publish.send(str(msg))
                except zmq.ZMQError:
                    logger.exception('publish failed')
            time.sleep(TIME_WAKEUP)
    except (KeyboardInterrupt, SystemExit):
        pass
    finally:
        logger.info('publisher stopping')
        publish.close()
Beispiel #4
0
def check_and_publish(datatype, rpc_metadata, publish, heartbeat):
    """Check for new files of type *datatype*, with the given *rpc_metadata*
    and publish them through *publish*.
    """
    stamp_config = DatexLastStamp(datatype)

    def younger_than_stamp_files():
        """Uses glob polling to get new files.
        """
        fdir, fglob = datex_config.get_path(datatype)
        del fglob
        fstamp = stamp_config.get_last_stamp()
        for fname, ftime in _get_file_list(datatype,
                                           time_start=fstamp + TIME_EPSILON):
            if datex_config.distribute(datatype):
                yield os.path.join(fdir, fname)
            stamp_config.update_last_stamp(ftime)

    # Give the publisher a little time to initialize
    # (e.g reconnections from subscribers)
    time.sleep(1)
    logger.info('publisher starting for datatype %s'%datatype)
    if heartbeat:
        last_heartbeat = datetime.now() - timedelta(seconds=heartbeat + 1)
    try:
        while(True):
            if(heartbeat and
               (datetime.now() - last_heartbeat).seconds >= heartbeat):
                # Send a heartbeat
                msg = Message('/hearbeat/' + datatype, 'heartbeat', datetime.utcnow().isoformat())
                logger.info('sending: ' + str(msg))
                try:
                    publish.send(str(msg))
                    last_heartbeat = datetime.now()
                except zmq.ZMQError:
                    logger.exception('publish failed')
            for filedesc in younger_than_stamp_files():
                # Publish new files
                data = copy.copy(rpc_metadata)
                data['uri'] += os.path.basename(filedesc)
                msg = Message('/' + datatype, 'file', data)
                logger.info('sending: ' + str(msg))
                try:
                    publish.send(str(msg))
                except zmq.ZMQError:
                    logger.exception('publish failed')
            time.sleep(TIME_WAKEUP)
    except (KeyboardInterrupt, SystemExit):
        pass
    finally:
        logger.info('publisher stopping')
        publish.close()