1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import division
6 from builtins import next
7 from builtins import object
8 from builtins import str
9 from future.utils import viewvalues, viewitems
19 from functools import partial
24 from cwltool.errors import WorkflowException
25 import cwltool.workflow
26 from schema_salad.sourceline import SourceLine
27 import schema_salad.validate as validate
28 from schema_salad.ref_resolver import file_uri, uri_file_path
32 from arvados.keep import KeepClient
33 from arvados.errors import ApiError
35 import arvados_cwl.util
36 from .arvcontainer import RunnerContainer, cleanup_name_for_collection
37 from .runner import Runner, upload_docker, upload_job_order, upload_workflow_deps, make_builder
38 from .arvtool import ArvadosCommandTool, validate_cluster_target, ArvadosExpressionTool
39 from .arvworkflow import ArvadosWorkflow, upload_workflow
40 from .fsaccess import CollectionFsAccess, CollectionFetcher, collectionResolver, CollectionCache, pdh_size
41 from .perf import Perf
42 from .pathmapper import NoFollowPathMapper
43 from cwltool.task_queue import TaskQueue
44 from .context import ArvLoadingContext, ArvRuntimeContext
45 from ._version import __version__
47 from cwltool.process import shortname, UnsupportedRequirement, use_custom_schema
48 from cwltool.utils import adjustFileObjs, adjustDirObjs, get_listing, visit_class, aslist
49 from cwltool.command_line_tool import compute_checksums
50 from cwltool.load_tool import load_tool
52 logger = logging.getLogger('arvados.cwl-runner')
53 metrics = logging.getLogger('arvados.cwl-runner.metrics')
55 DEFAULT_PRIORITY = 500
57 class RuntimeStatusLoggingHandler(logging.Handler):
59 Intercepts logging calls and report them as runtime statuses on runner
62 def __init__(self, runtime_status_update_func):
63 super(RuntimeStatusLoggingHandler, self).__init__()
64 self.runtime_status_update = runtime_status_update_func
65 self.updatingRuntimeStatus = False
67 def emit(self, record):
69 if record.levelno >= logging.ERROR:
71 elif record.levelno >= logging.WARNING:
73 if kind is not None and self.updatingRuntimeStatus is not True:
74 self.updatingRuntimeStatus = True
76 log_msg = record.getMessage()
78 # If the logged message is multi-line, use its first line as status
79 # and the rest as detail.
80 status, detail = log_msg.split('\n', 1)
81 self.runtime_status_update(
83 "%s: %s" % (record.name, status),
87 self.runtime_status_update(
89 "%s: %s" % (record.name, record.getMessage())
92 self.updatingRuntimeStatus = False
95 class ArvCwlExecutor(object):
96 """Execute a CWL tool or workflow, submit work (using containers API),
97 wait for them to complete, and report output.
101 def __init__(self, api_client,
109 arvargs = argparse.Namespace()
110 arvargs.work_api = None
111 arvargs.output_name = None
112 arvargs.output_tags = None
113 arvargs.thread_count = 1
114 arvargs.collection_cache_size = None
115 arvargs.git_info = True
117 self.api = api_client
119 self.workflow_eval_lock = threading.Condition(threading.RLock())
120 self.final_output = None
121 self.final_status = None
122 self.num_retries = num_retries
124 self.stop_polling = threading.Event()
127 self.final_output_collection = None
128 self.output_name = arvargs.output_name
129 self.output_tags = arvargs.output_tags
130 self.project_uuid = None
131 self.intermediate_output_ttl = 0
132 self.intermediate_output_collections = []
133 self.trash_intermediate = False
134 self.thread_count = arvargs.thread_count
135 self.poll_interval = 12
136 self.loadingContext = None
137 self.should_estimate_cache_size = True
138 self.fs_access = None
139 self.secret_store = None
141 self.fast_submit = False
142 self.git_info = arvargs.git_info
144 if keep_client is not None:
145 self.keep_client = keep_client
147 self.keep_client = arvados.keep.KeepClient(api_client=self.api, num_retries=self.num_retries)
149 if arvargs.collection_cache_size:
150 collection_cache_size = arvargs.collection_cache_size*1024*1024
151 self.should_estimate_cache_size = False
153 collection_cache_size = 256*1024*1024
155 self.collection_cache = CollectionCache(self.api, self.keep_client, self.num_retries,
156 cap=collection_cache_size)
158 self.fetcher_constructor = partial(CollectionFetcher,
160 fs_access=CollectionFsAccess("", collection_cache=self.collection_cache),
161 num_retries=self.num_retries)
164 expected_api = ["containers"]
165 for api in expected_api:
167 methods = self.api._rootDesc.get('resources')[api]['methods']
168 if ('httpMethod' in methods['create'] and
169 (arvargs.work_api == api or arvargs.work_api is None)):
175 if not self.work_api:
176 if arvargs.work_api is None:
177 raise Exception("No supported APIs")
179 raise Exception("Unsupported API '%s', expected one of %s" % (arvargs.work_api, expected_api))
181 if self.work_api == "jobs":
183 *******************************
184 The 'jobs' API is no longer supported.
185 *******************************""")
188 self.loadingContext = ArvLoadingContext(vars(arvargs))
189 self.loadingContext.fetcher_constructor = self.fetcher_constructor
190 self.loadingContext.resolver = partial(collectionResolver, self.api, num_retries=self.num_retries)
191 self.loadingContext.construct_tool_object = self.arv_make_tool
193 # Add a custom logging handler to the root logger for runtime status reporting
194 # if running inside a container
195 if arvados_cwl.util.get_current_container(self.api, self.num_retries, logger):
196 root_logger = logging.getLogger('')
198 # Remove existing RuntimeStatusLoggingHandlers if they exist
199 handlers = [h for h in root_logger.handlers if not isinstance(h, RuntimeStatusLoggingHandler)]
200 root_logger.handlers = handlers
202 handler = RuntimeStatusLoggingHandler(self.runtime_status_update)
203 root_logger.addHandler(handler)
205 self.toplevel_runtimeContext = ArvRuntimeContext(vars(arvargs))
206 self.toplevel_runtimeContext.make_fs_access = partial(CollectionFsAccess,
207 collection_cache=self.collection_cache)
209 self.defer_downloads = arvargs.submit and arvargs.defer_downloads
210 self.varying_url_params = arvargs.varying_url_params
212 validate_cluster_target(self, self.toplevel_runtimeContext)
215 def arv_make_tool(self, toolpath_object, loadingContext):
216 if "class" in toolpath_object and toolpath_object["class"] == "CommandLineTool":
217 return ArvadosCommandTool(self, toolpath_object, loadingContext)
218 elif "class" in toolpath_object and toolpath_object["class"] == "Workflow":
219 return ArvadosWorkflow(self, toolpath_object, loadingContext)
220 elif "class" in toolpath_object and toolpath_object["class"] == "ExpressionTool":
221 return ArvadosExpressionTool(self, toolpath_object, loadingContext)
223 raise Exception("Unknown tool %s" % toolpath_object.get("class"))
225 def output_callback(self, out, processStatus):
226 with self.workflow_eval_lock:
227 if processStatus == "success":
228 logger.info("Overall process status is %s", processStatus)
231 logger.error("Overall process status is %s", processStatus)
234 self.api.pipeline_instances().update(uuid=self.pipeline["uuid"],
235 body={"state": state}).execute(num_retries=self.num_retries)
236 self.final_status = processStatus
237 self.final_output = out
238 self.workflow_eval_lock.notifyAll()
241 def start_run(self, runnable, runtimeContext):
242 self.task_queue.add(partial(runnable.run, runtimeContext),
243 self.workflow_eval_lock, self.stop_polling)
245 def process_submitted(self, container):
246 with self.workflow_eval_lock:
247 self.processes[container.uuid] = container
249 def process_done(self, uuid, record):
250 with self.workflow_eval_lock:
251 j = self.processes[uuid]
252 logger.info("%s %s is %s", self.label(j), uuid, record["state"])
253 self.task_queue.add(partial(j.done, record),
254 self.workflow_eval_lock, self.stop_polling)
255 del self.processes[uuid]
257 def runtime_status_update(self, kind, message, detail=None):
259 Updates the runtime_status field on the runner container.
260 Called when there's a need to report errors, warnings or just
261 activity statuses, for example in the RuntimeStatusLoggingHandler.
264 if kind not in ('error', 'warning'):
265 # Ignore any other status kind
268 with self.workflow_eval_lock:
271 current = arvados_cwl.util.get_current_container(self.api, self.num_retries, logger)
272 except Exception as e:
273 logger.info("Couldn't get current container: %s", e)
276 runtime_status = current.get('runtime_status', {})
278 original_updatemessage = updatemessage = runtime_status.get(kind, "")
279 if not updatemessage:
280 updatemessage = message
282 # Subsequent messages tacked on in detail
283 original_updatedetail = updatedetail = runtime_status.get(kind+'Detail', "")
285 if updatedetail.count("\n") < maxlines:
288 updatedetail += message + "\n"
291 updatedetail += detail + "\n"
293 if updatedetail.count("\n") >= maxlines:
294 updatedetail += "\nSome messages may have been omitted. Check the full log."
296 if updatemessage == original_updatemessage and updatedetail == original_updatedetail:
297 # don't waste time doing an update if nothing changed
298 # (usually because we exceeded the max lines)
301 runtime_status.update({
303 kind+'Detail': updatedetail,
307 self.api.containers().update(uuid=current['uuid'],
309 'runtime_status': runtime_status,
310 }).execute(num_retries=self.num_retries)
311 except Exception as e:
312 logger.info("Couldn't update runtime_status: %s", e)
314 def wrapped_callback(self, cb, obj, st):
315 with self.workflow_eval_lock:
317 self.workflow_eval_lock.notifyAll()
319 def get_wrapped_callback(self, cb):
320 return partial(self.wrapped_callback, cb)
322 def on_message(self, event):
323 if event.get("object_uuid") in self.processes and event["event_type"] == "update":
324 uuid = event["object_uuid"]
325 if event["properties"]["new_attributes"]["state"] == "Running":
326 with self.workflow_eval_lock:
327 j = self.processes[uuid]
328 if j.running is False:
330 j.update_pipeline_component(event["properties"]["new_attributes"])
331 logger.info("%s %s is Running", self.label(j), uuid)
332 elif event["properties"]["new_attributes"]["state"] in ("Complete", "Failed", "Cancelled", "Final"):
333 self.process_done(uuid, event["properties"]["new_attributes"])
335 def label(self, obj):
336 return "[%s %s]" % (self.work_api[0:-1], obj.name)
338 def poll_states(self):
339 """Poll status of containers listed in the processes dict.
341 Runs in a separate thread.
345 remain_wait = self.poll_interval
348 self.stop_polling.wait(remain_wait)
349 if self.stop_polling.is_set():
351 with self.workflow_eval_lock:
352 keys = list(self.processes)
354 remain_wait = self.poll_interval
357 begin_poll = time.time()
358 if self.work_api == "containers":
359 table = self.poll_api.container_requests()
361 pageSize = self.poll_api._rootDesc.get('maxItemsPerResponse', 1000)
364 page = keys[:pageSize]
366 proc_states = table.list(filters=[["uuid", "in", page]]).execute(num_retries=self.num_retries)
368 logger.exception("Error checking states on API server: %s")
369 remain_wait = self.poll_interval
372 for p in proc_states["items"]:
374 "object_uuid": p["uuid"],
375 "event_type": "update",
380 keys = keys[pageSize:]
382 finish_poll = time.time()
383 remain_wait = self.poll_interval - (finish_poll - begin_poll)
385 logger.exception("Fatal error in state polling thread.")
386 with self.workflow_eval_lock:
387 self.processes.clear()
388 self.workflow_eval_lock.notifyAll()
390 self.stop_polling.set()
392 def add_intermediate_output(self, uuid):
394 self.intermediate_output_collections.append(uuid)
396 def trash_intermediate_output(self):
397 logger.info("Cleaning up intermediate output collections")
398 for i in self.intermediate_output_collections:
400 self.api.collections().delete(uuid=i).execute(num_retries=self.num_retries)
402 logger.warning("Failed to delete intermediate output: %s", sys.exc_info()[1], exc_info=(sys.exc_info()[1] if self.debug else False))
403 except (KeyboardInterrupt, SystemExit):
406 def check_features(self, obj, parentfield=""):
407 if isinstance(obj, dict):
408 if obj.get("class") == "DockerRequirement":
409 if obj.get("dockerOutputDirectory"):
410 if not obj.get("dockerOutputDirectory").startswith('/'):
411 raise SourceLine(obj, "dockerOutputDirectory", validate.ValidationException).makeError(
412 "Option 'dockerOutputDirectory' must be an absolute path.")
413 if obj.get("class") == "InplaceUpdateRequirement":
414 if obj["inplaceUpdate"] and parentfield == "requirements":
415 raise SourceLine(obj, "class", UnsupportedRequirement).makeError("InplaceUpdateRequirement not supported for keep collections.")
416 for k,v in viewitems(obj):
417 self.check_features(v, parentfield=k)
418 elif isinstance(obj, list):
419 for i,v in enumerate(obj):
420 with SourceLine(obj, i, UnsupportedRequirement, logger.isEnabledFor(logging.DEBUG)):
421 self.check_features(v, parentfield=parentfield)
423 def make_output_collection(self, name, storage_classes, tagsString, output_properties, outputObj):
424 outputObj = copy.deepcopy(outputObj)
427 def capture(fileobj):
428 files.append(fileobj)
430 adjustDirObjs(outputObj, capture)
431 adjustFileObjs(outputObj, capture)
433 generatemapper = NoFollowPathMapper(files, "", "", separateDirs=False)
435 final = arvados.collection.Collection(api_client=self.api,
436 keep_client=self.keep_client,
437 num_retries=self.num_retries)
439 for k,v in generatemapper.items():
440 if v.type == "Directory" and v.resolved.startswith("_:"):
442 if v.type == "CreateFile" and (k.startswith("_:") or v.resolved.startswith("_:")):
443 with final.open(v.target, "wb") as f:
444 f.write(v.resolved.encode("utf-8"))
447 if not v.resolved.startswith("keep:"):
448 raise Exception("Output source is not in keep or a literal")
449 sp = v.resolved.split("/")
450 srccollection = sp[0][5:]
452 reader = self.collection_cache.get(srccollection)
453 srcpath = urllib.parse.unquote("/".join(sp[1:]) if len(sp) > 1 else ".")
454 final.copy(srcpath, v.target, source_collection=reader, overwrite=False)
455 except arvados.errors.ArgumentError as e:
456 logger.error("Creating CollectionReader for '%s' '%s': %s", k, v, e)
459 logger.error("While preparing output collection: %s", e)
462 def rewrite(fileobj):
463 fileobj["location"] = generatemapper.mapper(fileobj["location"]).target
464 for k in ("listing", "contents", "nameext", "nameroot", "dirname"):
468 adjustDirObjs(outputObj, rewrite)
469 adjustFileObjs(outputObj, rewrite)
471 with final.open("cwl.output.json", "w") as f:
472 res = str(json.dumps(outputObj, sort_keys=True, indent=4, separators=(',',': '), ensure_ascii=False))
476 final.save_new(name=name, owner_uuid=self.project_uuid, storage_classes=storage_classes,
477 ensure_unique_name=True, properties=output_properties)
479 logger.info("Final output collection %s \"%s\" (%s)", final.portable_data_hash(),
480 final.api_response()["name"],
481 final.manifest_locator())
483 final_uuid = final.manifest_locator()
484 tags = tagsString.split(',')
486 self.api.links().create(body={
487 "head_uuid": final_uuid, "link_class": "tag", "name": tag
488 }).execute(num_retries=self.num_retries)
490 def finalcollection(fileobj):
491 fileobj["location"] = "keep:%s/%s" % (final.portable_data_hash(), fileobj["location"])
493 adjustDirObjs(outputObj, finalcollection)
494 adjustFileObjs(outputObj, finalcollection)
496 return (outputObj, final)
498 def set_crunch_output(self):
499 if self.work_api == "containers":
500 current = arvados_cwl.util.get_current_container(self.api, self.num_retries, logger)
504 self.api.containers().update(uuid=current['uuid'],
506 'output': self.final_output_collection.portable_data_hash(),
507 'output_properties': self.final_output_collection.get_properties(),
508 }).execute(num_retries=self.num_retries)
509 self.api.collections().update(uuid=self.final_output_collection.manifest_locator(),
512 }).execute(num_retries=self.num_retries)
514 logger.exception("Setting container output")
517 def apply_reqs(self, job_order_object, tool):
518 if "https://w3id.org/cwl/cwl#requirements" in job_order_object:
519 if tool.metadata.get("http://commonwl.org/cwltool#original_cwlVersion") == 'v1.0':
520 raise WorkflowException(
521 "`cwl:requirements` in the input object is not part of CWL "
522 "v1.0. You can adjust to use `cwltool:overrides` instead; or you "
523 "can set the cwlVersion to v1.1 or greater and re-run with "
525 job_reqs = job_order_object["https://w3id.org/cwl/cwl#requirements"]
527 tool.requirements.append(req)
530 def get_git_info(tool):
531 in_a_git_repo = False
535 if tool.tool["id"].startswith("file://"):
536 # check if git is installed
538 filepath = uri_file_path(tool.tool["id"])
539 cwd = os.path.dirname(filepath)
540 subprocess.run(["git", "log", "--format=%H", "-n1", "HEAD"], cwd=cwd, check=True, capture_output=True, text=True)
542 except Exception as e:
548 git_commit = subprocess.run(["git", "log", "--format=%H", "-n1", "HEAD"], cwd=cwd, capture_output=True, text=True).stdout
549 git_date = subprocess.run(["git", "log", "--format=%cD", "-n1", "HEAD"], cwd=cwd, capture_output=True, text=True).stdout
550 git_committer = subprocess.run(["git", "log", "--format=%cn <%ce>", "-n1", "HEAD"], cwd=cwd, capture_output=True, text=True).stdout
551 git_branch = subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd, capture_output=True, text=True).stdout
552 git_origin = subprocess.run(["git", "remote", "get-url", "origin"], cwd=cwd, capture_output=True, text=True).stdout
553 git_status = subprocess.run(["git", "status", "--untracked-files=no", "--porcelain"], cwd=cwd, capture_output=True, text=True).stdout
554 git_describe = subprocess.run(["git", "describe", "--always", "--tags"], cwd=cwd, capture_output=True, text=True).stdout
555 git_toplevel = subprocess.run(["git", "rev-parse", "--show-toplevel"], cwd=cwd, capture_output=True, text=True).stdout
556 git_path = filepath[len(git_toplevel):]
559 "http://arvados.org/cwl#gitCommit": git_commit.strip(),
560 "http://arvados.org/cwl#gitDate": git_date.strip(),
561 "http://arvados.org/cwl#gitCommitter": git_committer.strip(),
562 "http://arvados.org/cwl#gitBranch": git_branch.strip(),
563 "http://arvados.org/cwl#gitOrigin": git_origin.strip(),
564 "http://arvados.org/cwl#gitStatus": git_status.strip(),
565 "http://arvados.org/cwl#gitDescribe": git_describe.strip(),
566 "http://arvados.org/cwl#gitPath": git_path.strip(),
569 for g in ("http://arvados.org/cwl#gitCommit",
570 "http://arvados.org/cwl#gitDate",
571 "http://arvados.org/cwl#gitCommitter",
572 "http://arvados.org/cwl#gitBranch",
573 "http://arvados.org/cwl#gitOrigin",
574 "http://arvados.org/cwl#gitStatus",
575 "http://arvados.org/cwl#gitDescribe",
576 "http://arvados.org/cwl#gitPath"):
577 if g in tool.metadata:
578 gitproperties[g] = tool.metadata[g]
582 def set_container_request_properties(self, container, properties):
583 resp = self.api.container_requests().list(filters=[["container_uuid", "=", container["uuid"]]], select=["uuid", "properties"]).execute(num_retries=self.num_retries)
584 for cr in resp["items"]:
585 cr["properties"].update({k.replace("http://arvados.org/cwl#", "arv:"): v for k, v in properties.items()})
586 self.api.container_requests().update(uuid=cr["uuid"], body={"container_request": {"properties": cr["properties"]}}).execute(num_retries=self.num_retries)
588 def arv_executor(self, updated_tool, job_order, runtimeContext, logger=None):
589 self.debug = runtimeContext.debug
591 git_info = self.get_git_info(updated_tool) if self.git_info else {}
593 logger.info("Git provenance")
596 logger.info(" %s: %s", g.split("#", 1)[1], git_info[g])
598 workbench1 = self.api.config()["Services"]["Workbench1"]["ExternalURL"]
599 workbench2 = self.api.config()["Services"]["Workbench2"]["ExternalURL"]
600 controller = self.api.config()["Services"]["Controller"]["ExternalURL"]
601 logger.info("Using cluster %s (%s)", self.api.config()["ClusterID"], workbench2 or workbench1 or controller)
603 if not self.fast_submit:
604 updated_tool.visit(self.check_features)
607 self.fs_access = runtimeContext.make_fs_access(runtimeContext.basedir)
608 self.secret_store = runtimeContext.secret_store
610 self.trash_intermediate = runtimeContext.trash_intermediate
611 if self.trash_intermediate and self.work_api != "containers":
612 raise Exception("--trash-intermediate is only supported with --api=containers.")
614 self.intermediate_output_ttl = runtimeContext.intermediate_output_ttl
615 if self.intermediate_output_ttl and self.work_api != "containers":
616 raise Exception("--intermediate-output-ttl is only supported with --api=containers.")
617 if self.intermediate_output_ttl < 0:
618 raise Exception("Invalid value %d for --intermediate-output-ttl, cannot be less than zero" % self.intermediate_output_ttl)
620 if runtimeContext.submit_request_uuid and self.work_api != "containers":
621 raise Exception("--submit-request-uuid requires containers API, but using '{}' api".format(self.work_api))
623 runtimeContext = runtimeContext.copy()
625 default_storage_classes = ",".join([k for k,v in self.api.config().get("StorageClasses", {"default": {"Default": True}}).items() if v.get("Default") is True])
626 if runtimeContext.storage_classes == "default":
627 runtimeContext.storage_classes = default_storage_classes
628 if runtimeContext.intermediate_storage_classes == "default":
629 runtimeContext.intermediate_storage_classes = default_storage_classes
631 if not runtimeContext.name:
632 self.name = updated_tool.tool.get("label") or updated_tool.metadata.get("label") or os.path.basename(updated_tool.tool["id"])
633 if git_info.get("http://arvados.org/cwl#gitDescribe"):
634 self.name = "%s (%s)" % (self.name, git_info.get("http://arvados.org/cwl#gitDescribe"))
635 runtimeContext.name = self.name
637 if runtimeContext.copy_deps is None and (runtimeContext.create_workflow or runtimeContext.update_workflow):
638 # When creating or updating workflow record, by default
639 # always copy dependencies and ensure Docker images are up
641 runtimeContext.copy_deps = True
642 runtimeContext.match_local_docker = True
644 if runtimeContext.update_workflow and self.project_uuid is None:
645 # If we are updating a workflow, make sure anything that
646 # gets uploaded goes into the same parent project, unless
647 # an alternate --project-uuid was provided.
648 existing_wf = self.api.workflows().get(uuid=runtimeContext.update_workflow).execute()
649 runtimeContext.project_uuid = existing_wf["owner_uuid"]
651 self.project_uuid = runtimeContext.project_uuid
653 # Upload local file references in the job order.
654 with Perf(metrics, "upload_job_order"):
655 job_order = upload_job_order(self, "%s input" % runtimeContext.name,
656 updated_tool, job_order, runtimeContext)
658 # the last clause means: if it is a command line tool, and we
659 # are going to wait for the result, and always_submit_runner
660 # is false, then we don't submit a runner process.
662 submitting = (runtimeContext.update_workflow or
663 runtimeContext.create_workflow or
664 (runtimeContext.submit and not
665 (updated_tool.tool["class"] == "CommandLineTool" and
666 runtimeContext.wait and
667 not runtimeContext.always_submit_runner)))
669 loadingContext = self.loadingContext.copy()
670 loadingContext.do_validate = False
671 loadingContext.disable_js_validation = True
672 if submitting and not self.fast_submit:
673 loadingContext.do_update = False
674 # Document may have been auto-updated. Reload the original
675 # document with updating disabled because we want to
676 # submit the document with its original CWL version, not
677 # the auto-updated one.
678 with Perf(metrics, "load_tool original"):
679 tool = load_tool(updated_tool.tool["id"], loadingContext)
683 # Upload direct dependencies of workflow steps, get back mapping of files to keep references.
684 # Also uploads docker images.
685 if not self.fast_submit:
686 logger.info("Uploading workflow dependencies")
687 with Perf(metrics, "upload_workflow_deps"):
688 merged_map = upload_workflow_deps(self, tool, runtimeContext)
692 # Recreate process object (ArvadosWorkflow or
693 # ArvadosCommandTool) because tool document may have been
694 # updated by upload_workflow_deps in ways that modify
695 # inheritance of hints or requirements.
696 loadingContext.loader = tool.doc_loader
697 loadingContext.avsc_names = tool.doc_schema
698 loadingContext.metadata = tool.metadata
699 with Perf(metrics, "load_tool"):
700 tool = load_tool(tool.tool, loadingContext)
702 if runtimeContext.update_workflow or runtimeContext.create_workflow:
703 # Create a pipeline template or workflow record and exit.
704 if self.work_api == "containers":
705 uuid = upload_workflow(self, tool, job_order,
706 runtimeContext.project_uuid,
708 uuid=runtimeContext.update_workflow,
709 submit_runner_ram=runtimeContext.submit_runner_ram,
710 name=runtimeContext.name,
711 merged_map=merged_map,
712 submit_runner_image=runtimeContext.submit_runner_image,
714 self.stdout.write(uuid + "\n")
715 return (None, "success")
717 self.apply_reqs(job_order, tool)
719 self.ignore_docker_for_reuse = runtimeContext.ignore_docker_for_reuse
720 self.eval_timeout = runtimeContext.eval_timeout
722 runtimeContext.use_container = True
723 runtimeContext.tmpdir_prefix = "tmp"
724 runtimeContext.work_api = self.work_api
726 if not self.output_name:
727 self.output_name = "Output from workflow %s" % runtimeContext.name
729 self.output_name = cleanup_name_for_collection(self.output_name)
731 if self.work_api == "containers":
732 if self.ignore_docker_for_reuse:
733 raise Exception("--ignore-docker-for-reuse not supported with containers API.")
734 runtimeContext.outdir = "/var/spool/cwl"
735 runtimeContext.docker_outdir = "/var/spool/cwl"
736 runtimeContext.tmpdir = "/tmp"
737 runtimeContext.docker_tmpdir = "/tmp"
739 if runtimeContext.priority < 1 or runtimeContext.priority > 1000:
740 raise Exception("--priority must be in the range 1..1000.")
742 if self.should_estimate_cache_size:
745 def estimate_collection_cache(obj):
746 if obj.get("location", "").startswith("keep:"):
747 m = pdh_size.match(obj["location"][5:])
748 if m and m.group(1) not in visited:
749 visited.add(m.group(1))
750 estimated_size[0] += int(m.group(2))
751 visit_class(job_order, ("File", "Directory"), estimate_collection_cache)
752 runtimeContext.collection_cache_size = max(((estimated_size[0]*192) // (1024*1024))+1, 256)
753 self.collection_cache.set_cap(runtimeContext.collection_cache_size*1024*1024)
755 logger.info("Using collection cache size %s MiB", runtimeContext.collection_cache_size)
758 if runtimeContext.submit:
759 # Submit a runner job to run the workflow for us.
760 if self.work_api == "containers":
762 tool = RunnerContainer(self, updated_tool,
763 tool, loadingContext, runtimeContext.enable_reuse,
766 submit_runner_ram=runtimeContext.submit_runner_ram,
767 name=runtimeContext.name,
768 on_error=runtimeContext.on_error,
769 submit_runner_image=runtimeContext.submit_runner_image,
770 intermediate_output_ttl=runtimeContext.intermediate_output_ttl,
771 merged_map=merged_map,
772 priority=runtimeContext.priority,
773 secret_store=self.secret_store,
774 collection_cache_size=runtimeContext.collection_cache_size,
775 collection_cache_is_default=self.should_estimate_cache_size,
778 runtimeContext.runnerjob = tool.tool["id"]
780 if runtimeContext.cwl_runner_job is not None:
781 self.uuid = runtimeContext.cwl_runner_job.get('uuid')
783 jobiter = tool.job(job_order,
784 self.output_callback,
787 if runtimeContext.submit and not runtimeContext.wait:
788 runnerjob = next(jobiter)
789 runnerjob.run(runtimeContext)
790 self.stdout.write(runnerjob.uuid+"\n")
791 return (None, "success")
793 current_container = arvados_cwl.util.get_current_container(self.api, self.num_retries, logger)
794 if current_container:
795 logger.info("Running inside container %s", current_container.get("uuid"))
796 self.set_container_request_properties(current_container, git_info)
798 self.poll_api = arvados.api('v1', timeout=runtimeContext.http_timeout)
799 self.polling_thread = threading.Thread(target=self.poll_states)
800 self.polling_thread.start()
802 self.task_queue = TaskQueue(self.workflow_eval_lock, self.thread_count)
805 self.workflow_eval_lock.acquire()
807 # Holds the lock while this code runs and releases it when
808 # it is safe to do so in self.workflow_eval_lock.wait(),
809 # at which point on_message can update job state and
810 # process output callbacks.
812 loopperf = Perf(metrics, "jobiter")
814 for runnable in jobiter:
817 if self.stop_polling.is_set():
820 if self.task_queue.error is not None:
821 raise self.task_queue.error
824 with Perf(metrics, "run"):
825 self.start_run(runnable, runtimeContext)
827 if (self.task_queue.in_flight + len(self.processes)) > 0:
828 self.workflow_eval_lock.wait(3)
830 logger.error("Workflow is deadlocked, no runnable processes and not waiting on any pending processes.")
833 if self.stop_polling.is_set():
839 while (self.task_queue.in_flight + len(self.processes)) > 0:
840 if self.task_queue.error is not None:
841 raise self.task_queue.error
842 self.workflow_eval_lock.wait(3)
844 except UnsupportedRequirement:
847 if sys.exc_info()[0] is KeyboardInterrupt or sys.exc_info()[0] is SystemExit:
848 logger.error("Interrupted, workflow will be cancelled")
849 elif isinstance(sys.exc_info()[1], WorkflowException):
850 logger.error("Workflow execution failed:\n%s", sys.exc_info()[1], exc_info=(sys.exc_info()[1] if self.debug else False))
852 logger.exception("Workflow execution failed")
855 self.api.pipeline_instances().update(uuid=self.pipeline["uuid"],
856 body={"state": "Failed"}).execute(num_retries=self.num_retries)
858 if self.work_api == "containers" and not current_container:
859 # Not running in a crunch container, so cancel any outstanding processes.
860 for p in self.processes:
862 self.api.container_requests().update(uuid=p,
863 body={"priority": "0"}
864 ).execute(num_retries=self.num_retries)
868 self.workflow_eval_lock.release()
869 self.task_queue.drain()
870 self.stop_polling.set()
871 self.polling_thread.join()
872 self.task_queue.join()
874 if self.final_status == "UnsupportedRequirement":
875 raise UnsupportedRequirement("Check log for details.")
877 if self.final_output is None:
878 raise WorkflowException("Workflow did not return a result.")
880 if runtimeContext.submit and isinstance(tool, Runner):
881 logger.info("Final output collection %s", tool.final_output)
882 if workbench2 or workbench1:
883 logger.info("Output at %scollections/%s", workbench2 or workbench1, tool.final_output)
885 if self.output_tags is None:
886 self.output_tags = ""
889 storage_class_req, _ = tool.get_requirement("http://arvados.org/cwl#OutputStorageClass")
890 if storage_class_req and storage_class_req.get("finalStorageClass"):
891 storage_classes = aslist(storage_class_req["finalStorageClass"])
893 storage_classes = runtimeContext.storage_classes.strip().split(",")
895 output_properties = {}
896 output_properties_req, _ = tool.get_requirement("http://arvados.org/cwl#OutputCollectionProperties")
897 if output_properties_req:
898 builder = make_builder(job_order, tool.hints, tool.requirements, runtimeContext, tool.metadata)
899 for pr in output_properties_req["outputProperties"]:
900 output_properties[pr["propertyName"]] = builder.do_eval(pr["propertyValue"])
902 self.final_output, self.final_output_collection = self.make_output_collection(self.output_name, storage_classes,
903 self.output_tags, output_properties,
905 self.set_crunch_output()
907 if runtimeContext.compute_checksum:
908 adjustDirObjs(self.final_output, partial(get_listing, self.fs_access))
909 adjustFileObjs(self.final_output, partial(compute_checksums, self.fs_access))
911 if self.trash_intermediate and self.final_status == "success":
912 self.trash_intermediate_output()
914 return (self.final_output, self.final_status)