1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import print_function
6 from __future__ import absolute_import
7 from future import standard_library
8 standard_library.install_aliases()
9 from builtins import object
26 if sys.version_info >= (3, 0):
27 from collections import UserDict
29 from UserDict import UserDict
31 from .api import api, api_from_config, http_cache
32 from .collection import CollectionReader, CollectionWriter, ResumableCollectionWriter
33 from arvados.keep import *
34 from arvados.stream import *
35 from .arvfile import StreamFileReader
36 from .retry import RetryLoop
37 import arvados.errors as errors
38 import arvados.util as util
40 # Set up Arvados logging based on the user's configuration.
41 # All Arvados code should log under the arvados hierarchy.
42 log_format = '%(asctime)s %(name)s[%(process)d] %(levelname)s: %(message)s'
43 log_date_format = '%Y-%m-%d %H:%M:%S'
44 log_handler = logging.StreamHandler()
45 log_handler.setFormatter(logging.Formatter(log_format, log_date_format))
46 logger = logging.getLogger('arvados')
47 logger.addHandler(log_handler)
48 logger.setLevel(logging.DEBUG if config.get('ARVADOS_DEBUG')
51 def task_set_output(self, s, num_retries=5):
52 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=0):
54 return api('v1').job_tasks().update(
61 except errors.ApiError as error:
62 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
63 logger.debug("task_set_output: job_tasks().update() raised {}, retrying with {} tries left".format(repr(error),tries_left))
68 def current_task(num_retries=5):
73 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
75 task = api('v1').job_tasks().get(uuid=os.environ['TASK_UUID']).execute()
77 task.set_output = types.MethodType(task_set_output, task)
78 task.tmpdir = os.environ['TASK_WORK']
81 except errors.ApiError as error:
82 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
83 logger.debug("current_task: job_tasks().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
88 def current_job(num_retries=5):
93 for tries_left in RetryLoop(num_retries=num_retries, backoff_start=2):
95 job = api('v1').jobs().get(uuid=os.environ['JOB_UUID']).execute()
97 job.tmpdir = os.environ['JOB_WORK']
100 except errors.ApiError as error:
101 if retry.check_http_response_success(error.resp.status) is None and tries_left > 0:
102 logger.debug("current_job: jobs().get() raised {}, retrying with {} tries left".format(repr(error),tries_left))
106 def getjobparam(*args):
107 return current_job()['script_parameters'].get(*args)
109 def get_job_param_mount(*args):
110 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_job()['script_parameters'].get(*args))
112 def get_task_param_mount(*args):
113 return os.path.join(os.environ['TASK_KEEPMOUNT'], current_task()['parameters'].get(*args))
115 class JobTask(object):
116 def __init__(self, parameters=dict(), runtime_constraints=dict()):
117 print("init jobtask %s %s" % (parameters, runtime_constraints))
119 class job_setup(object):
121 def one_task_per_input_file(if_sequence=0, and_end_task=True, input_as_path=False, api_client=None):
122 if if_sequence != current_task()['sequence']:
126 api_client = api('v1')
128 job_input = current_job()['script_parameters']['input']
129 cr = CollectionReader(job_input, api_client=api_client)
131 for s in cr.all_streams():
132 for f in s.all_files():
134 task_input = os.path.join(job_input, s.name(), f.name())
136 task_input = f.as_manifest()
138 'job_uuid': current_job()['uuid'],
139 'created_by_job_task_uuid': current_task()['uuid'],
140 'sequence': if_sequence + 1,
145 api_client.job_tasks().create(body=new_task_attrs).execute()
147 api_client.job_tasks().update(uuid=current_task()['uuid'],
148 body={'success':True}
153 def one_task_per_input_stream(if_sequence=0, and_end_task=True):
154 if if_sequence != current_task()['sequence']:
156 job_input = current_job()['script_parameters']['input']
157 cr = CollectionReader(job_input)
158 for s in cr.all_streams():
159 task_input = s.tokens()
161 'job_uuid': current_job()['uuid'],
162 'created_by_job_task_uuid': current_task()['uuid'],
163 'sequence': if_sequence + 1,
168 api('v1').job_tasks().create(body=new_task_attrs).execute()
170 api('v1').job_tasks().update(uuid=current_task()['uuid'],
171 body={'success':True}