def update(self):
        if self.loadbalancers is None or len(self.loadbalancers) <= 0:
            return
        proxy_mgr = ServoLoop.proxy_mgr
        hc_mgr = ServoLoop.hc_mgr
        log_listener = ServoLoop.log_listener
        access_logger = ServoLoop.access_logger

        # prepare Listener lists
        received=[] 
        try:
            conn_idle_timeout = config.CONNECTION_IDLE_TIMEOUT
            for lb in self.loadbalancers:
                try:
                    if log_listener: # assume there is only one loadbalancer per servo
                        log_listener.set_loadbalancer(lb.name)
                    attr = lb.attributes
                    conn_idle_timeout = attr.connecting_settings.idle_timeout
                    if int(conn_idle_timeout) < 1:
                        conn_idle_timeout = 1
                    elif int(conn_idle_timeout) > 3600:
                        conn_idle_timeout = 3600
                    access_log_setting = attr.access_log
                    access_logger.loadbalancer = lb.name
                    if access_log_setting.s3_bucket_name != None:
                        access_logger.bucket_name = access_log_setting.s3_bucket_name
                    if access_log_setting.s3_bucket_prefix != None:
                        access_logger.bucket_prefix = access_log_setting.s3_bucket_prefix
                    if access_log_setting.emit_interval != None:
                        access_logger.emit_interval = int(access_log_setting.emit_interval)
                    if access_log_setting.enabled != None:
                        access_logger.enabled = access_log_setting.enabled
                except Exception, err:
                    servo.log.warning('failed to get connection idle timeout: %s' % str(err))

                if lb.health_check is not None:
                    interval = lb.health_check.interval
                    healthy_threshold = lb.health_check.healthy_threshold
                    unhealthy_threshold = lb.health_check.unhealthy_threshold
                    timeout = lb.health_check.timeout
                    target = lb.health_check.target
                    if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or  target is None: 
                        pass
                    else:
                        hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target)
                        if health_check.health_check_config is None or health_check.health_check_config != hc:
                            health_check.health_check_config = hc
                            servo.log.info('new health check config: %s' % hc)
                            hc_mgr.reset()
                instances = []
                # record instance ip address
                if lb.instances is not None and isinstance(lb.instances, Iterable):
                    instances.extend(lb.instances)
                    for inst in lb.instances:
                        inst_id=inst.instance_id
                        ipaddr=inst.instance_ip_address
                        servo.hostname_cache.register(inst_id, ipaddr)
                    
                instance_ids = [inst.instance_id for inst in instances]
                hc_mgr.set_instances(instances)
                in_service_instances = []
                for inst_id in instance_ids:                  
                    if hc_mgr.health_status(inst_id) is 'InService':
                        in_service_instances.append(inst_id)

                if lb.listeners is not None and isinstance(lb.listeners, Iterable) :
                    for listener in lb.listeners:
                        protocol=listener.protocol
                        port=listener.load_balancer_port
                        instance_port=listener.instance_port
                        instance_protocol=listener.instance_protocol 
                        ssl_cert=str(listener.ssl_certificate_id)
                        policies = self.get_listener_policies(listener.policy_names)
                        policies.extend(self.get_backend_policies(lb, instance_port))
                        l = Listener(protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, policies=policies, connection_idle_timeout=conn_idle_timeout)
                        for inst_id in in_service_instances:
                            hostname = servo.hostname_cache.get_hostname(inst_id)
                            if hostname is not None: l.add_instance(hostname) 
                        received.append(l)
        # call update_listeners
        except Exception, err:
            servo.log.error('failed to update listeners: %s' % err) 
    def start(self):
        if config.ENABLE_CLOUD_WATCH:
            hl = mon.LogListener(stat_instance)
            hl.start()
        self.__status = ServoLoop.RUNNING 
        proxy_mgr = ProxyManager()
        hc_mgr = HealthCheckManager()
        while self.__status == ServoLoop.RUNNING:
            # call elb-describe-services
            lbs = None
            try:
                access_key_id = config.get_access_key_id()
                secret_access_key = config.get_secret_access_key()
                security_token = config.get_security_token()
                con = servo.ws.connect_elb(host_name=self.__elb_host, aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, security_token=security_token)
                lbs = con.get_servo_load_balancers(self.__instance_id)
            except Exception, err:
                servo.log.error('failed to query the elb service: %s' % err)
            if lbs is None:
                servo.log.warning('failed to find the loadbalancers')
            else:
                # prepare Listener lists
                # call update_listeners
                received=[] 
                try:
                    for lb in lbs:
                        if lb.health_check is not None:
                            interval = lb.health_check.interval
                            healthy_threshold = lb.health_check.healthy_threshold
                            unhealthy_threshold = lb.health_check.unhealthy_threshold
                            timeout = lb.health_check.timeout
                            target = lb.health_check.target
                            if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or  target is None: 
                                pass
                            else:
                                hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target)
                                if health_check.health_check_config is None or health_check.health_check_config != hc:
                                    health_check.health_check_config = hc
                                    servo.log.info('new health check config: %s' % hc)
                                    hc_mgr.reset()
                        instances = []
                        if lb.instances is not None and isinstance(lb.instances, Iterable):
                            for inst in lb.instances:
                                instances.append(str(inst.id))
                        
                        hc_mgr.set_instances(instances)
                        in_service_instances = []
                        for inst_id in instances:                  
                            if hc_mgr.health_status(inst_id) is 'InService':
                                in_service_instances.append(inst_id)

                        if lb.listeners is not None and isinstance(lb.listeners, Iterable) :
                            for listener in lb.listeners:
                                protocol=listener.protocol
                                port=listener.load_balancer_port
                                instance_port=listener.instance_port
                                instance_protocol=None # TODO: boto doesn't have the field
                                ssl_cert=None # TODO: not supported
                                cookie_expiration = ServoLoop.get_cookie_expiration(listener)
                                cookie_name = ServoLoop.get_cookie_name(listener)
                                l = Listener(protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, cookie_name=cookie_name, cookie_expiration=cookie_expiration)
                                for inst_id in in_service_instances:
                                    hostname = servo.hostname_cache.get_hostname(inst_id)
                                    if hostname is not None: l.add_instance(hostname) 
                                received.append(l)
                except Exception, err:
                    servo.log.error('failed to receive listeners: %s' % err) 
                try:
                    proxy_mgr.update_listeners(received)
                    servo.log.debug('listener updated')
                except Exception, err:
                    servo.log.error('failed to update proxy listeners: %s' % err) 
                            in_service_instances.append(inst_id)

                    if lb.listeners is not None and isinstance(lb.listeners, Iterable):
                        for listener in lb.listeners:
                            protocol = listener.protocol
                            port = listener.load_balancer_port
                            instance_port = listener.instance_port
                            instance_protocol = listener.instance_protocol
                            ssl_cert = str(listener.ssl_certificate_id)
                            policies = ServoLoop.get_listener_policies(lb, listener.policy_names)
                            policies.extend(ServoLoop.get_backend_policies(lb, instance_port))
                            l = Listener(
                                protocol=protocol,
                                port=port,
                                instance_port=instance_port,
                                instance_protocol=instance_protocol,
                                ssl_cert=ssl_cert,
                                loadbalancer=lb.name,
                                policies=policies,
                                connection_idle_timeout=conn_idle_timeout,
                            )
                            for inst_id in in_service_instances:
                                hostname = servo.hostname_cache.get_hostname(inst_id)
                                if hostname is not None:
                                    l.add_instance(hostname)
                            received.append(l)
            except Exception, err:
                servo.log.error("failed to receive listeners: %s" % err)
            try:
                proxy_mgr.update_listeners(received)
                servo.log.debug("Listener updated")
            except Exception, err:
    def start(self):
        log_listener = None
        access_logger = AccessLogger()
        access_logger.start()
        if config.ENABLE_CLOUD_WATCH:
            log_listener = mon.LogListener(stat_instance)
            log_listener.access_logger = access_logger
            log_listener.start()
 
        self.__status = ServoLoop.RUNNING 
        proxy_mgr = ProxyManager()
        hc_mgr = HealthCheckManager()
        while self.__status == ServoLoop.RUNNING:
            # call elb-describe-services
            lbs = None
            try:
                access_key_id = config.get_access_key_id()
                secret_access_key = config.get_secret_access_key()
                security_token = config.get_security_token()
                con = servo.ws.connect_elb(aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, security_token=security_token)
                lbs = con.get_servo_load_balancers(self.__instance_id)
            except Exception, err:
                servo.log.error('failed to query the elb service: %s' % err)
            if lbs is None:
                servo.log.warning('failed to find the loadbalancers')
            else:
                # prepare Listener lists
                # call update_listeners
                received=[] 
                try:
                    conn_idle_timeout = config.CONNECTION_IDLE_TIMEOUT
                    for lb in lbs:
                        try:
                            if log_listener: # assume there is only one loadbalancer per servo
                                log_listener.set_loadbalancer(lb.name)
                            attr = lb.attributes
                            conn_idle_timeout = attr.connecting_settings.idle_timeout
                            if int(conn_idle_timeout) < 1:
                                conn_idle_timeout = 1
                            elif int(conn_idle_timeout) > 3600:
                                conn_idle_timeout = 3600
                            access_log_setting = attr.access_log
                            access_logger.loadbalancer = lb.name
                            if access_log_setting.s3_bucket_name != None:
                                access_logger.bucket_name = access_log_setting.s3_bucket_name
                                servo.log.debug('access log bucket name: %s' % access_logger.bucket_name)
                            if access_log_setting.s3_bucket_prefix != None:
                                access_logger.bucket_prefix = access_log_setting.s3_bucket_prefix
                                servo.log.debug('access log bucket prefix: %s' % access_logger.bucket_prefix)
                            if access_log_setting.emit_interval != None:
                                access_logger.emit_interval = int(access_log_setting.emit_interval)
                                servo.log.debug('access log emit interval: %d' % access_logger.emit_interval)
                            if access_log_setting.enabled != None:
                                access_logger.enabled = access_log_setting.enabled
                                servo.log.debug('access log enabled?: %s' % access_logger.enabled)
 
                        except Exception, err:
                            servo.log.warning('failed to get connection idle timeout: %s' % str(err))
                        if lb.health_check is not None:
                            interval = lb.health_check.interval
                            healthy_threshold = lb.health_check.healthy_threshold
                            unhealthy_threshold = lb.health_check.unhealthy_threshold
                            timeout = lb.health_check.timeout
                            target = lb.health_check.target
                            if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or  target is None: 
                                pass
                            else:
                                hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target)
                                if health_check.health_check_config is None or health_check.health_check_config != hc:
                                    health_check.health_check_config = hc
                                    servo.log.info('new health check config: %s' % hc)
                                    hc_mgr.reset()
                        instances = []
                        if lb.instances is not None and isinstance(lb.instances, Iterable):
                            instances.extend(lb.instances)
                        instance_ids = [inst.instance_id for inst in instances]
                        
                        hc_mgr.set_instances(instances)
                        in_service_instances = []
                        for inst_id in instance_ids:                  
                            if hc_mgr.health_status(inst_id) is 'InService':
                                in_service_instances.append(inst_id)

                        if lb.listeners is not None and isinstance(lb.listeners, Iterable) :
                            for listener in lb.listeners:
                                protocol=listener.protocol
                                port=listener.load_balancer_port
                                instance_port=listener.instance_port
                                instance_protocol=listener.instance_protocol 
                                ssl_cert=str(listener.ssl_certificate_id)
                                policies = ServoLoop.get_listener_policies(lb, listener.policy_names)
                                policies.extend(ServoLoop.get_backend_policies(lb, instance_port))
                                l = Listener(protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, policies=policies, connection_idle_timeout=conn_idle_timeout)
                                for inst_id in in_service_instances:
                                    hostname = servo.hostname_cache.get_hostname(inst_id)
                                    if hostname is not None: l.add_instance(hostname) 
                                received.append(l)
                except Exception, err:
                    servo.log.error('failed to receive listeners: %s' % err) 
                try:
                    proxy_mgr.update_listeners(received)
                    servo.log.debug('listener updated')
                except Exception, err:
                    servo.log.error('failed to update proxy listeners: %s' % err) 
    def start(self):
        log_listener = None
        access_logger = AccessLogger()
        access_logger.start()
        if config.ENABLE_CLOUD_WATCH:
            log_listener = mon.LogListener(stat_instance)
            log_listener.access_logger = access_logger
            log_listener.start()

        self.__status = ServoLoop.RUNNING
        proxy_mgr = ProxyManager()
        hc_mgr = HealthCheckManager()
        while self.__status == ServoLoop.RUNNING:
            # call elb-describe-services
            lbs = None
            try:
                access_key_id = config.get_access_key_id()
                secret_access_key = config.get_secret_access_key()
                security_token = config.get_security_token()
                con = servo.ws.connect_elb(
                    aws_access_key_id=access_key_id,
                    aws_secret_access_key=secret_access_key,
                    security_token=security_token)
                lbs = con.get_servo_load_balancers(self.__instance_id)
            except Exception, err:
                servo.log.error('failed to query the elb service: %s' % err)
            if lbs is None:
                servo.log.warning('failed to find the loadbalancers')
            else:
                # prepare Listener lists
                # call update_listeners
                received = []
                try:
                    conn_idle_timeout = config.CONNECTION_IDLE_TIMEOUT
                    for lb in lbs:
                        try:
                            if log_listener:  # assume there is only one loadbalancer per servo
                                log_listener.set_loadbalancer(lb.name)
                            attr = lb.attributes
                            conn_idle_timeout = attr.connecting_settings.idle_timeout
                            if int(conn_idle_timeout) < 1:
                                conn_idle_timeout = 1
                            elif int(conn_idle_timeout) > 3600:
                                conn_idle_timeout = 3600
                            access_log_setting = attr.access_log
                            access_logger.loadbalancer = lb.name
                            if access_log_setting.s3_bucket_name != None:
                                access_logger.bucket_name = access_log_setting.s3_bucket_name
                                servo.log.debug(
                                    'access log bucket name: %s' %
                                    urllib2.quote(access_logger.bucket_name))
                            if access_log_setting.s3_bucket_prefix != None:
                                access_logger.bucket_prefix = access_log_setting.s3_bucket_prefix
                                servo.log.debug(
                                    'access log bucket prefix: %s' %
                                    urllib2.quote(access_logger.bucket_prefix))
                            if access_log_setting.emit_interval != None:
                                access_logger.emit_interval = int(
                                    access_log_setting.emit_interval)
                                servo.log.debug(
                                    'access log emit interval: %d' %
                                    access_logger.emit_interval)
                            if access_log_setting.enabled != None:
                                access_logger.enabled = access_log_setting.enabled
                                servo.log.debug('access log enabled?: %s' %
                                                access_logger.enabled)

                        except Exception, err:
                            servo.log.warning(
                                'failed to get connection idle timeout: %s' %
                                str(err))
                        if lb.health_check is not None:
                            interval = lb.health_check.interval
                            healthy_threshold = lb.health_check.healthy_threshold
                            unhealthy_threshold = lb.health_check.unhealthy_threshold
                            timeout = lb.health_check.timeout
                            target = lb.health_check.target
                            if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or target is None:
                                pass
                            else:
                                hc = HealthCheckConfig(interval,
                                                       healthy_threshold,
                                                       unhealthy_threshold,
                                                       timeout, target)
                                if health_check.health_check_config is None or health_check.health_check_config != hc:
                                    health_check.health_check_config = hc
                                    servo.log.info(
                                        'new health check config: %s' % hc)
                                    hc_mgr.reset()
                        instances = []
                        if lb.instances is not None and isinstance(
                                lb.instances, Iterable):
                            instances.extend(lb.instances)
                        instance_ids = [inst.instance_id for inst in instances]

                        hc_mgr.set_instances(instances)
                        in_service_instances = []
                        for inst_id in instance_ids:
                            if hc_mgr.health_status(inst_id) is 'InService':
                                in_service_instances.append(inst_id)

                        if lb.listeners is not None and isinstance(
                                lb.listeners, Iterable):
                            for listener in lb.listeners:
                                protocol = listener.protocol
                                port = listener.load_balancer_port
                                instance_port = listener.instance_port
                                instance_protocol = listener.instance_protocol
                                ssl_cert = str(listener.ssl_certificate_id)
                                policies = ServoLoop.get_listener_policies(
                                    lb, listener.policy_names)
                                policies.extend(
                                    ServoLoop.get_backend_policies(
                                        lb, instance_port))
                                l = Listener(
                                    protocol=protocol,
                                    port=port,
                                    instance_port=instance_port,
                                    instance_protocol=instance_protocol,
                                    ssl_cert=ssl_cert,
                                    loadbalancer=lb.name,
                                    policies=policies,
                                    connection_idle_timeout=conn_idle_timeout)
                                for inst_id in in_service_instances:
                                    hostname = servo.hostname_cache.get_hostname(
                                        inst_id)
                                    if hostname is not None:
                                        l.add_instance(hostname)
                                received.append(l)
                except Exception, err:
                    servo.log.error('failed to receive listeners: %s' % err)
                try:
                    proxy_mgr.update_listeners(received)
                    servo.log.debug('listener updated')
                except Exception, err:
                    servo.log.error('failed to update proxy listeners: %s' %
                                    err)
    def start(self):
        if config.ENABLE_CLOUD_WATCH:
            hl = mon.LogListener(stat_instance)
            hl.start()
        self.__status = ServoLoop.RUNNING
        proxy_mgr = ProxyManager()
        hc_mgr = HealthCheckManager()
        while self.__status == ServoLoop.RUNNING:
            # call elb-describe-services
            lbs = None
            try:
                access_key_id = config.get_access_key_id()
                secret_access_key = config.get_secret_access_key()
                security_token = config.get_security_token()
                con = servo.ws.connect_elb(
                    host_name=self.__elb_host,
                    aws_access_key_id=access_key_id,
                    aws_secret_access_key=secret_access_key,
                    security_token=security_token)
                lbs = con.get_servo_load_balancers(self.__instance_id)
            except Exception, err:
                servo.log.error('failed to query the elb service: %s' % err)
            if lbs is None:
                servo.log.warning('failed to find the loadbalancers')
            else:
                # prepare Listener lists
                # call update_listeners
                received = []
                try:
                    for lb in lbs:
                        if lb.health_check is not None:
                            interval = lb.health_check.interval
                            healthy_threshold = lb.health_check.healthy_threshold
                            unhealthy_threshold = lb.health_check.unhealthy_threshold
                            timeout = lb.health_check.timeout
                            target = lb.health_check.target
                            if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or target is None:
                                pass
                            else:
                                hc = HealthCheckConfig(interval,
                                                       healthy_threshold,
                                                       unhealthy_threshold,
                                                       timeout, target)
                                if health_check.health_check_config is None or health_check.health_check_config != hc:
                                    health_check.health_check_config = hc
                                    servo.log.info(
                                        'new health check config: %s' % hc)
                                    hc_mgr.reset()
                        instances = []
                        if lb.instances is not None and isinstance(
                                lb.instances, Iterable):
                            for inst in lb.instances:
                                instances.append(str(inst.id))

                        hc_mgr.set_instances(instances)
                        in_service_instances = []
                        for inst_id in instances:
                            if hc_mgr.health_status(inst_id) is 'InService':
                                in_service_instances.append(inst_id)

                        if lb.listeners is not None and isinstance(
                                lb.listeners, Iterable):
                            for listener in lb.listeners:
                                protocol = listener.protocol
                                port = listener.load_balancer_port
                                instance_port = listener.instance_port
                                instance_protocol = None  # TODO: boto doesn't have the field
                                ssl_cert = None  # TODO: not supported
                                cookie_expiration = ServoLoop.get_cookie_expiration(
                                    listener)
                                cookie_name = ServoLoop.get_cookie_name(
                                    listener)
                                l = Listener(
                                    protocol=protocol,
                                    port=port,
                                    instance_port=instance_port,
                                    instance_protocol=instance_protocol,
                                    ssl_cert=ssl_cert,
                                    loadbalancer=lb.name,
                                    cookie_name=cookie_name,
                                    cookie_expiration=cookie_expiration)
                                for inst_id in in_service_instances:
                                    hostname = servo.hostname_cache.get_hostname(
                                        inst_id)
                                    if hostname is not None:
                                        l.add_instance(hostname)
                                received.append(l)
                except Exception, err:
                    servo.log.error('failed to receive listeners: %s' % err)
                try:
                    proxy_mgr.update_listeners(received)
                    servo.log.debug('listener updated')
                except Exception, err:
                    servo.log.error('failed to update proxy listeners: %s' %
                                    err)