def run(self, *args, **kwargs): self.logger.info('Publisher [{0}], starting...'.format(self.name)) callback = { S1.msg_publish(): self._on_publish_updates, S1.msg_register(): self._on_register, S1.msg_update_avatar(): self._on_update_avatar, } channels = [S1.publisher_channel_name('all'), S1.publisher_channel_name(self.name)] channels.extend([name for name in self.providers.keys() if name != self.name]) # this will start infinite loop (in Pubsub) self.listener(channels, callback) self.logger.warning('Publisher [{0}], listener exit!'.format(self.name))
def run(self, *args, **kwargs): cfg = config.load_config(kwargs['config_path'], 'queue.json') period_s = cfg['period_s'] if 'period_s' in cfg else 10 self.logger.info('Queue v[{0}], poll period=[{1}]s, starting...'.format(config.version, period_s)) try: while True: # get the next items from the queue # set look ahead value to half of the wait time items = self.data.buffer.get_next_queue_items(period_s / 2.0) self.logger.info('{0} items...'.format(len(items))) # post notifications for each item for itm in items: self.logger.info('Notifying: {0}'.format(itm)) # item format: "gid:target" item = itm.split(':') self.broadcast_command(S1.publisher_channel_name(item[1]), S1.msg_publish(), item[0]) # sleep random interval s = random.randrange(period_s - (period_s / 10.0), period_s + (period_s / 10.0)) self.logger.info('Sleeping {0} seconds...'.format(s)) time.sleep(s) except Exception as e: self.logger.warning('Queue is terminating (exception): {0}'.format(e)) self.logger.exception(traceback.format_exc())
def __init__(self, logger, name, data, provider_names, config_path, dummy=False): """ @type data: Data @type logger: Logger @type provider_names: list """ super(Publisher, self).__init__(logger, name, data, provider_names, config_path, dummy) self.providers = {S1.publisher_channel_name(p): PublisherProviders.create(p, logger, data, config_path) for p in provider_names}
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S') logger = logging.getLogger(__name__) logger.addHandler( config.getLogHandler(os.path.join(args.log_path, 'poller_test.log'))) logger.level = logging.DEBUG db = data.Data(logger, args.redis_host, args.redis_port, args.redis_db) while True: logger.warning( 'Invoking registration for all, next poll in {0} seconds'.format( args.period)) with open(args.gid_set) as f_set: gid_set = [gid.strip() for gid in f_set.readlines()] logger.info('Read [{0}] gids'.format(len(gid_set))) for n in range(0, len(gid_set)): gid = gid_set[randint(0, len(gid_set) - 1)] logger.info('Invoking registration for [{0}]'.format(gid)) db.pubsub.broadcast_command(S1.publisher_channel_name('twitter'), S1.msg_register(), gid) t = randint(5, 20) logger.info('Sleeping for [{0}]'.format(t)) time.sleep(t) #get delay and wait time.sleep(args.period)