20 from .api import api, http_cache
21 from collection import CollectionReader, CollectionWriter, ResumableCollectionWriter
24 from arvfile import StreamFileReader
25 from retry import RetryLoop
29 # Set up Arvados logging based on the user's configuration.
30 # All Arvados code should log under the arvados hierarchy.
31 log_handler = logging.StreamHandler()
32 log_handler.setFormatter(logging.Formatter(
33 '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s',
35 logger = logging.getLogger('arvados')
36 logger.addHandler(log_handler)
37 logger.setLevel(logging.DEBUG if config.get('ARVADOS_DEBUG')
40 def task_set_output(self, s, num_retries=5):
41 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=0):
43 return api('v1').job_tasks().update(
50 except errors.ApiError as error:
51 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
52 logger.debug("task_set_output: job_tasks().update() raised {}, retrying with {} tries left".format(repr(error),tries_left))
57 def current_task(num_retries=5):
62 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
64 task = api('v1').job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
65 task = UserDict.UserDict(task)
66 task.set_output = types.MethodType(task_set_output, task)
67 task.tmpdir = os.environ['TASK_WORK']
70 except errors.ApiError as error:
71 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
72 logger.debug("current_task: job_tasks().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
77 def current_job(num_retries=5):
82 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
84 job = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
85 job = UserDict.UserDict(job)
86 job.tmpdir = os.environ['JOB_WORK']
89 except errors.ApiError as error:
90 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
91 logger.debug("current_job: jobs().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
95 def getjobparam(*args):
96 return current_job()['script_parameters'].get(*args)
98 def get_job_param_mount(*args):
99 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_job()['script_parameters'].get(*args))
101 def get_task_param_mount(*args):
102 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_task()['parameters'].get(*args))
104 class JobTask(object):
105 def __init__(self, parameters=dict(), runtime_constraints=dict()):
106 print "init jobtask %s %s" % (parameters, runtime_constraints)
110 def one_task_per_input_file(if_sequence=0, and_end_task=True, input_as_path=False, api_client=None):
111 if if_sequence != current_task()['sequence']:
115 api_client = api('v1')
117 job_input = current_job()['script_parameters']['input']
118 cr = CollectionReader(job_input, api_client=api_client)
120 for s in cr.all_streams():
121 for f in s.all_files():
123 task_input = os.path.join(job_input, s.name(), f.name())
125 task_input = f.as_manifest()
127 'job_uuid': current_job()['uuid'],
128 'created_by_job_task_uuid': current_task()['uuid'],
129 'sequence': if_sequence + 1,
134 api_client.job_tasks().create(body=new_task_attrs).execute()
136 api_client.job_tasks().update(uuid=current_task()['uuid'],
137 body={'success':True}
142 def one_task_per_input_stream(if_sequence=0, and_end_task=True):
143 if if_sequence != current_task()['sequence']:
145 job_input = current_job()['script_parameters']['input']
146 cr = CollectionReader(job_input)
147 for s in cr.all_streams():
148 task_input = s.tokens()
150 'job_uuid': current_job()['uuid'],
151 'created_by_job_task_uuid': current_task()['uuid'],
152 'sequence': if_sequence + 1,
157 api('v1').job_tasks().create(body=new_task_attrs).execute()
159 api('v1').job_tasks().update(uuid=current_task()['uuid'],
160 body={'success':True}