def get_happy_tasks(app, service, nerve_ns, system_paasta_config, min_task_uptime=None, check_haproxy=False): """Given a MarathonApp object, return the subset of tasks which are considered healthy. With the default options, this returns tasks where at least one of the defined Marathon healthchecks passes. For it to do anything interesting, set min_task_uptime or check_haproxy. :param app: A MarathonApp object. :param service: The name of the service. :param nerve_ns: The nerve namespace :param min_task_uptime: Minimum number of seconds that a task must be running before we consider it healthy. Useful if tasks take a while to start up. :param check_haproxy: Whether to check the local haproxy to make sure this task has been registered and discovered. """ tasks = app.tasks happy = [] now = datetime.datetime.utcnow() if check_haproxy: tasks_in_smartstack = [] service_namespace = compose_job_id(service, nerve_ns) service_namespace_config = marathon_tools.load_service_namespace_config( service, nerve_ns) discover_location_type = service_namespace_config.get_discover() unique_values = mesos_tools.get_mesos_slaves_grouped_by_attribute( discover_location_type) for value, hosts in unique_values.iteritems(): synapse_host = hosts[0] tasks_in_smartstack.extend( get_registered_marathon_tasks( synapse_host, system_paasta_config.get_synapse_port(), system_paasta_config.get_synapse_haproxy_url_format(), service_namespace, tasks, )) tasks = tasks_in_smartstack for task in tasks: if min_task_uptime is not None: if (now - task.started_at).total_seconds() < min_task_uptime: continue # if there are healthchecks defined for the app but none have executed yet, then task is unhappy if len(app.health_checks) > 0 and len(task.health_check_results) == 0: continue # if there are health check results, check if at least one healthcheck is passing if not marathon_tools.is_task_healthy( task, require_all=False, default_healthy=True): continue happy.append(task) return happy
def get_happy_tasks(app, service, nerve_ns, system_paasta_config, min_task_uptime=None, check_haproxy=False): """Given a MarathonApp object, return the subset of tasks which are considered healthy. With the default options, this returns tasks where at least one of the defined Marathon healthchecks passes. For it to do anything interesting, set min_task_uptime or check_haproxy. :param app: A MarathonApp object. :param service: The name of the service. :param nerve_ns: The nerve namespace :param min_task_uptime: Minimum number of seconds that a task must be running before we consider it healthy. Useful if tasks take a while to start up. :param check_haproxy: Whether to check the local haproxy to make sure this task has been registered and discovered. """ tasks = app.tasks happy = [] now = datetime.datetime.utcnow() if check_haproxy: tasks_in_smartstack = [] service_namespace = compose_job_id(service, nerve_ns) service_namespace_config = marathon_tools.load_service_namespace_config(service, nerve_ns) discover_location_type = service_namespace_config.get_discover() unique_values = mesos_tools.get_mesos_slaves_grouped_by_attribute( slaves=mesos_tools.get_slaves(), attribute=discover_location_type ) for value, hosts in unique_values.iteritems(): synapse_hostname = hosts[0]['hostname'] tasks_in_smartstack.extend(get_registered_marathon_tasks( synapse_hostname, system_paasta_config.get_synapse_port(), system_paasta_config.get_synapse_haproxy_url_format(), service_namespace, tasks, )) tasks = tasks_in_smartstack for task in tasks: if task.started_at is None: # Can't be healthy if it hasn't started continue if min_task_uptime is not None: if (now - task.started_at).total_seconds() < min_task_uptime: continue # if there are healthchecks defined for the app but none have executed yet, then task is unhappy if len(app.health_checks) > 0 and len(task.health_check_results) == 0: continue # if there are health check results, check if at least one healthcheck is passing if not marathon_tools.is_task_healthy(task, require_all=False, default_healthy=True): continue happy.append(task) return happy