def main(): global config config = load_config() is_master = config['sender'].get('is_master', False) logger.info('[-] bootstraping sender (master: %s)...', is_master) init_sender(config) init_plugins(config.get('plugins', {})) init_vendors(config.get('vendors', []), config.get('applications', [])) send_task = spawn(send) worker_tasks = [spawn(worker) for x in xrange(100)] if is_master: if should_mock_gwatch_renewer: spawn(mock_gwatch_renewer) else: spawn(gwatch_renewer) spawn(prune_old_audit_logs_worker) rpc.init(config['sender'], dict(send_message=send_message)) rpc.run(config['sender']) interval = 60 logger.info('[*] sender bootstrapped') while True: runtime = int(time.time()) logger.info('--> sender looop started.') cache.refresh() cache.purge() if is_master: try: escalate() deactivate() poll() aggregate(runtime) except Exception: metrics.incr('task_failure') logger.exception("Exception occured in main loop.") # check status for all background greenlets and respawn if necessary if not bool(send_task): logger.error("send task failed, %s", send_task.exception) metrics.incr('task_failure') send_task = spawn(send) bad_workers = [] for i, task in enumerate(worker_tasks): if not bool(task): logger.error("worker task failed, %s", task.exception) metrics.incr('task_failure') bad_workers.append(i) for i in bad_workers: worker_tasks[i] = spawn(worker) spawn(metrics.emit) now = time.time() elapsed_time = now - runtime nap_time = max(0, interval - elapsed_time) logger.info( '--> sender loop finished in %s seconds - sleeping %s seconds', elapsed_time, nap_time) sleep(nap_time)
def test_send_through_dummy(): init_vendors([{'type': 'iris_dummy'}], ['dummy_app']) assert send_message({'mode': 'call'}) == 1 assert send_message({'application': 'dummy app', 'mode': 'call'}) == 2
def main(): global config config = load_config() start_time = time.time() logger.info('[-] bootstraping sender...') init_sender(config) init_plugins(config.get('plugins', {})) init_vendors(config.get('vendors', []), config.get('applications', [])) send_task = spawn(send) worker_tasks = [spawn(worker) for x in xrange(100)] rpc.init(config['sender'], dict(send_message=send_message)) rpc.run(config['sender']) spawn(coordinator.update_forever) gwatch_renewer_task = None prune_audit_logs_task = None interval = 60 logger.info('[*] sender bootstrapped') while True: runtime = int(time.time()) logger.info('--> sender looop started.') cache.refresh() cache.purge() # If we're currently a master, ensure our master-greenlets are running # and we're doing the master duties if coordinator.am_i_master(): if not bool(gwatch_renewer_task): if should_mock_gwatch_renewer: gwatch_renewer_task = spawn(mock_gwatch_renewer) else: gwatch_renewer_task = spawn(gwatch_renewer) if not bool(prune_audit_logs_task): prune_audit_logs_task = spawn(prune_old_audit_logs_worker) try: escalate() deactivate() poll() aggregate(runtime) except Exception: metrics.incr('task_failure') logger.exception("Exception occured in main loop.") # If we're not master, don't do the master tasks and make sure those other # greenlets are stopped if they're running else: logger.info('I am not the master so I am not doing master sender tasks.') # Stop these task greenlets if they're running. Technically this should # never happen because if we're the master, we'll likely only stop being the # master if our process exits, which would kill these greenlets anyway. if bool(gwatch_renewer_task): logger.info('I am not master anymore so stopping the gwatch renewer') gwatch_renewer_task.kill() if bool(prune_audit_logs_task): logger.info('I am not master anymore so stopping the audit logs worker') prune_audit_logs_task.kill() # check status for all background greenlets and respawn if necessary if not bool(send_task): logger.error("send task failed, %s", send_task.exception) metrics.incr('task_failure') send_task = spawn(send) bad_workers = [] for i, task in enumerate(worker_tasks): if not bool(task): logger.error("worker task failed, %s", task.exception) metrics.incr('task_failure') bad_workers.append(i) for i in bad_workers: worker_tasks[i] = spawn(worker) now = time.time() metrics.set('sender_uptime', int(now - start_time)) spawn(metrics.emit) elapsed_time = now - runtime nap_time = max(0, interval - elapsed_time) logger.info('--> sender loop finished in %s seconds - sleeping %s seconds', elapsed_time, nap_time) sleep(nap_time)