reaper.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:treadmill 作者: Morgan-Stanley 项目源码 文件源码
def init():
    """Return top level command handler"""

    @click.command()
    @click.option('--cell', required=True,
                  envvar='TREADMILL_CELL',
                  callback=cli.handle_context_opt,
                  expose_value=False)
    @click.option('--once', help='Run once.', is_flag=True, default=False)
    @click.option('--interval', help='Wait interval between checks.',
                  default=_DEFAULT_INTERVAL)
    @click.option('--threshold', help='Number of failed checks before reap.',
                  default=1)
    @click.option('--proto', help='Endpoint protocol.', default='tcp',
                  type=click.Choice(['tcp', 'udp']))
    @click.argument('pattern')
    @click.argument('endpoint')
    @click.argument('command', nargs=-1)
    def reaper(once, interval, threshold, proto, pattern, endpoint, command):
        """Removes unhealthy instances of the app.

        The health check script reads from STDIN and prints to STDOUT.

        The input it list of instance host:port, similar to discovery.

        Output - list of instances that did not pass health check.

        For example, specifying awk '{print $1}' as COMMAND will remove all
        instances.
        """
        command = list(command)

        failed = collections.Counter()
        while True:
            failed.update(_health_check(pattern, proto, endpoint, command))
            for instance, count in failed.items():
                _LOGGER.info('Failed: %s, count: %s', instance, count)

            reaped = _reap([instance for instance, count in failed.items()
                            if count > threshold])

            for instance in reaped:
                del failed[instance]

            if once:
                break

            time.sleep(utils.to_seconds(interval))

    return reaper
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号