Beispiel #1
0
 def __init__(self, config_path):
     cfg = ObjectDict(read_config(config_path))
     cfg.db_name = cfg['mongo.db']
     cfg['num_processes'] = int(cfg.get('num_processes', 0))
     cfg['stubo_version'] = version
     cfg['debug'] = asbool(cfg.get('debug', False))
     max_workers = int(cfg.get('max_workers', 100))
     log.info('started with {0} worker threads'.format(max_workers))
     cfg['executor'] = ThreadPoolExecutor(max_workers)
    
     try:
         cfg['statsd_client'] = StatsClient(host=cfg.get('statsd.host', 
             'localhost'), prefix=cfg.get('statsd.prefix', 'stubo')) 
         cfg['stats'] = StatsdStats()
         log.info('statsd host addr={0}, prefix={1}'.format(
                 cfg['statsd_client']._addr, cfg['statsd_client']._prefix))
     except socket.gaierror, e:
         log.warn("unable to connect to statsd: {0}".format(e))
Beispiel #2
0
    def __init__(self, config_path):
        cfg = ObjectDict(read_config(config_path))
        cfg.db_name = cfg["mongo.db"]
        cfg["num_processes"] = int(cfg.get("num_processes", 0))
        cfg["stubo_version"] = version
        cfg["debug"] = asbool(cfg.get("debug", False))
        max_workers = int(cfg.get("max_workers", 100))
        log.info("started with {0} worker threads".format(max_workers))
        cfg["executor"] = ThreadPoolExecutor(max_workers)

        try:
            cfg["statsd_client"] = StatsClient(
                host=cfg.get("statsd.host", "localhost"), prefix=cfg.get("statsd.prefix", "stubo")
            )
            cfg["stats"] = StatsdStats()
            log.info(
                "statsd host addr={0}, prefix={1}".format(cfg["statsd_client"]._addr, cfg["statsd_client"]._prefix)
            )
        except socket.gaierror, e:
            log.warn("unable to connect to statsd: {0}".format(e))
Beispiel #3
0
def purge_stubs():
    # importing helper handler from testing deps
    from stubo.testing import DummyRequestHandler
    parser = ArgumentParser(
        description="Purge stubs older than given expiry date."
    )
    parser.add_argument('-l', '--list', action='store_const', const=True,
                        dest='list_only', help="Just list the stubs to delete.")
    parser.add_argument('-e', '--expiry', default=14, dest='expiry',
                        help="expiry is number of days from now (default is 14).")
    parser.add_argument('--host', default='all', dest='host',
                        help="specify the host uri to use (defaults to all)")
    parser.add_argument('-c', '--config', dest='config',
                        help='Path to configuration file (defaults to $CWD/etc/dev.ini)',
                        metavar='FILE')

    args = parser.parse_args()
    list_only = args.list_only or False
    expiry_days = args.expiry
    expiry = datetime.today().date() - timedelta(int(expiry_days))
    host = args.host
    config = args.config or get_default_config()
    logging.config.fileConfig(config)

    settings = read_config(config)
    dbenv = default_env.copy()
    dbenv.update((k[6:], coerce_mongo_param(k[6:], v)) for k, v in \
                 settings.iteritems() if k.startswith('mongo.'))
    log.debug('mongo params: {0}'.format(dbenv))

    log.info('purge stubs whereby all sessions in the scenario were last used before {0}'.format(expiry))

    db_conn = init_mongo(dbenv).connection
    slave, master = start_redis(settings)
    response = list_scenarios(host)
    if 'error' in response:
        print response['error']
        sys.exit(-1)

    handler = DummyRequestHandler()
    session_handler = DummyRequestHandler()

    for scenario_key in response['data']['scenarios']:
        log.debug("*** scenario '{0}' ***".format(scenario_key))
        hostname, scenario = scenario_key.split(':')
        if host != 'all' and host != hostname:
            continue
        handler.host = hostname
        handler.request.host = '{0}:8001'.format(hostname)
        session_handler.host = hostname
        session_handler.request.host = '{0}:8001'.format(hostname)
        handler.request.arguments['scenario'] = [scenario]
        status = get_status(handler)
        if 'error' in status:
            log.warn('get_status error: {0}'.format(status['error']))
        else:
            scenario_last_used = []
            sessions = status['data']['sessions']
            for session in zip(*sessions)[0]:
                log.debug("*** -> session '{0}' ***".format(session))
                session_handler.request.arguments['session'] = [session]
                session_status = get_status(session_handler)
                if 'error' in session_status:
                    log.warn('get_status error: {0}'.format(status['error']))
                else:
                    last_used = session_status['data']['session'].get('last_used', '-')
                    if last_used != '-':
                        scenario_last_used.append(as_date(last_used[0:10]))

            if scenario_last_used and (max(scenario_last_used) < expiry):
                log.info("sessions in scenario '{0}' were last used '{1}' which"
                         " is before expiry date '{2}'".format(scenario_key,
                                                               max(scenario_last_used), expiry))
                if not list_only:
                    response = delete_stubs(handler, scenario_name=scenario,
                                            force=True)
                    if 'error' in response:
                        log.error('delete stubs error: {0}'.format(response['error']))
                    else:
                        log.info('deleted stubs: {0}'.format(response['data']))