def mesos_task_instance_from_assigned_task(assigned_task): """Deserialize MesosTaskInstance from an AssignedTask thrift.""" thermos_task = assigned_task.task.executorConfig.data if not thermos_task: raise ValueError('Task did not have a thermos config!') try: json_blob = json.loads(thermos_task) except (TypeError, ValueError) as e: raise ValueError('Could not deserialize thermos config: %s' % e) # As part of the transition for MESOS-2133, we can send either a MesosTaskInstance # or we can be sending a MesosJob. So handle both possible cases. Once everyone # is using MesosJob, then we can begin to leverage additional information that # becomes available such as cluster. if 'instance' in json_blob: return MesosTaskInstance.json_loads(thermos_task) # This is a MesosJob mti, refs = task_instance_from_job(MesosJob.json_loads(thermos_task), assigned_task.instanceId) for ref in refs: # If the ref is {{thermos.task_id}} or a subscope of # {{thermos.ports}}, it currently gets bound by the Thermos Runner, # so we must leave them unbound. # # {{thermos.user}} is a legacy binding which we can safely ignore. # # TODO(wickman) These should be rewritten by the mesos client to use # %%style%% replacements in order to allow us to better type-check configs # client-side. if ref == Ref.from_address('thermos.task_id'): continue if Ref.subscope(Ref.from_address('thermos.ports'), ref): continue if ref == Ref.from_address('thermos.user'): continue raise ValueError('Unexpected unbound refs: %s' % ' '.join(map(str, refs))) return mti
def test_task_instance_from_job(): instance = task_instance_from_job(Job(health_check_interval_secs=30), 0) assert instance is not None