1 from __future__ import print_function
2 from __future__ import absolute_import
3 from future import standard_library
4 standard_library.install_aliases()
5 from builtins import object
24 if sys.version_info >= (3, 0):
25 from collections import UserDict
27 from UserDict import UserDict
29 from .api import api, api_from_config, http_cache
30 from .collection import CollectionReader, CollectionWriter, ResumableCollectionWriter
31 from arvados.keep import *
32 from arvados.stream import *
33 from .arvfile import StreamFileReader
34 from .retry import RetryLoop
35 import arvados.errors as errors
36 import arvados.util as util
38 # Set up Arvados logging based on the user's configuration.
39 # All Arvados code should log under the arvados hierarchy.
40 log_handler = logging.StreamHandler()
41 log_handler.setFormatter(logging.Formatter(
42 '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s',
44 logger = logging.getLogger('arvados')
45 logger.addHandler(log_handler)
46 logger.setLevel(logging.DEBUG if config.get('ARVADOS_DEBUG')
49 def task_set_output(self, s, num_retries=5):
50 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=0):
52 return api('v1').job_tasks().update(
59 except errors.ApiError as error:
60 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
61 logger.debug("task_set_output: job_tasks().update() raised {}, retrying with {} tries left".format(repr(error),tries_left))
66 def current_task(num_retries=5):
71 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
73 task = api('v1').job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
75 task.set_output = types.MethodType(task_set_output, task)
76 task.tmpdir = os.environ['TASK_WORK']
79 except errors.ApiError as error:
80 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
81 logger.debug("current_task: job_tasks().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
86 def current_job(num_retries=5):
91 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
93 job = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
95 job.tmpdir = os.environ['JOB_WORK']
98 except errors.ApiError as error:
99 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
100 logger.debug("current_job: jobs().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
104 def getjobparam(*args):
105 return current_job()['script_parameters'].get(*args)
107 def get_job_param_mount(*args):
108 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_job()['script_parameters'].get(*args))
110 def get_task_param_mount(*args):
111 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_task()['parameters'].get(*args))
113 class JobTask(object):
114 def __init__(self, parameters=dict(), runtime_constraints=dict()):
115 print("init jobtask %s %s" % (parameters, runtime_constraints))
117 class job_setup(object):
119 def one_task_per_input_file(if_sequence=0, and_end_task=True, input_as_path=False, api_client=None):
120 if if_sequence != current_task()['sequence']:
124 api_client = api('v1')
126 job_input = current_job()['script_parameters']['input']
127 cr = CollectionReader(job_input, api_client=api_client)
129 for s in cr.all_streams():
130 for f in s.all_files():
132 task_input = os.path.join(job_input, s.name(), f.name())
134 task_input = f.as_manifest()
136 'job_uuid': current_job()['uuid'],
137 'created_by_job_task_uuid': current_task()['uuid'],
138 'sequence': if_sequence + 1,
143 api_client.job_tasks().create(body=new_task_attrs).execute()
145 api_client.job_tasks().update(uuid=current_task()['uuid'],
146 body={'success':True}
151 def one_task_per_input_stream(if_sequence=0, and_end_task=True):
152 if if_sequence != current_task()['sequence']:
154 job_input = current_job()['script_parameters']['input']
155 cr = CollectionReader(job_input)
156 for s in cr.all_streams():
157 task_input = s.tokens()
159 'job_uuid': current_job()['uuid'],
160 'created_by_job_task_uuid': current_task()['uuid'],
161 'sequence': if_sequence + 1,
166 api('v1').job_tasks().create(body=new_task_attrs).execute()
168 api('v1').job_tasks().update(uuid=current_task()['uuid'],
169 body={'success':True}