|
| 1 | +import logging |
| 2 | +import tempfile |
| 3 | +import threading |
| 4 | + |
| 5 | +import os |
| 6 | +from abc import ABCMeta, abstractmethod |
| 7 | + |
| 8 | +from typing import Dict, Text, Any, Tuple, Set, List |
| 9 | + |
| 10 | +from cwltool.builder import Builder |
| 11 | +from cwltool.errors import WorkflowException |
| 12 | +from cwltool.mutation import MutationManager |
| 13 | +from cwltool.job import JobBase |
| 14 | +from cwltool.process import relocateOutputs, cleanIntermediate, Process |
| 15 | + |
| 16 | + |
| 17 | +_logger = logging.getLogger("cwltool") |
| 18 | + |
| 19 | +defaultStreamHandler = logging.StreamHandler() |
| 20 | +_logger.addHandler(defaultStreamHandler) |
| 21 | +_logger.setLevel(logging.INFO) |
| 22 | + |
| 23 | + |
| 24 | +class JobExecutor(object): |
| 25 | + __metaclass__ = ABCMeta |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + # type: (...) -> None |
| 29 | + self.final_output = [] # type: List |
| 30 | + self.final_status = [] # type: List |
| 31 | + self.output_dirs = set() # type: Set |
| 32 | + |
| 33 | + def __call__(self, *args, **kwargs): |
| 34 | + return self.execute(*args, **kwargs) |
| 35 | + |
| 36 | + def output_callback(self, out, processStatus): |
| 37 | + self.final_status.append(processStatus) |
| 38 | + self.final_output.append(out) |
| 39 | + |
| 40 | + @abstractmethod |
| 41 | + def run_jobs(self, |
| 42 | + t, # type: Process |
| 43 | + job_order_object, # type: Dict[Text, Any] |
| 44 | + logger, |
| 45 | + **kwargs # type: Any |
| 46 | + ): |
| 47 | + pass |
| 48 | + |
| 49 | + def execute(self, t, # type: Process |
| 50 | + job_order_object, # type: Dict[Text, Any] |
| 51 | + logger=_logger, |
| 52 | + **kwargs # type: Any |
| 53 | + ): |
| 54 | + # type: (...) -> Tuple[Dict[Text, Any], Text] |
| 55 | + |
| 56 | + if "basedir" not in kwargs: |
| 57 | + raise WorkflowException("Must provide 'basedir' in kwargs") |
| 58 | + |
| 59 | + finaloutdir = os.path.abspath(kwargs.get("outdir")) if kwargs.get("outdir") else None |
| 60 | + kwargs["outdir"] = tempfile.mkdtemp(prefix=kwargs["tmp_outdir_prefix"]) if kwargs.get( |
| 61 | + "tmp_outdir_prefix") else tempfile.mkdtemp() |
| 62 | + self.output_dirs.add(kwargs["outdir"]) |
| 63 | + kwargs["mutation_manager"] = MutationManager() |
| 64 | + |
| 65 | + jobReqs = None |
| 66 | + if "cwl:requirements" in job_order_object: |
| 67 | + jobReqs = job_order_object["cwl:requirements"] |
| 68 | + elif ("cwl:defaults" in t.metadata and "cwl:requirements" in t.metadata["cwl:defaults"]): |
| 69 | + jobReqs = t.metadata["cwl:defaults"]["cwl:requirements"] |
| 70 | + if jobReqs: |
| 71 | + for req in jobReqs: |
| 72 | + t.requirements.append(req) |
| 73 | + |
| 74 | + self.run_jobs(t, job_order_object, logger, **kwargs) |
| 75 | + |
| 76 | + if self.final_output and self.final_output[0] and finaloutdir: |
| 77 | + self.final_output[0] = relocateOutputs(self.final_output[0], finaloutdir, |
| 78 | + self.output_dirs, kwargs.get("move_outputs"), |
| 79 | + kwargs["make_fs_access"]("")) |
| 80 | + |
| 81 | + if kwargs.get("rm_tmpdir"): |
| 82 | + cleanIntermediate(self.output_dirs) |
| 83 | + |
| 84 | + if self.final_output and self.final_status: |
| 85 | + return (self.final_output[0], self.final_status[0]) |
| 86 | + else: |
| 87 | + return (None, "permanentFail") |
| 88 | + |
| 89 | + |
| 90 | +class SingleJobExecutor(JobExecutor): |
| 91 | + def run_jobs(self, |
| 92 | + t, # type: Process |
| 93 | + job_order_object, # type: Dict[Text, Any] |
| 94 | + logger, |
| 95 | + **kwargs # type: Any |
| 96 | + ): |
| 97 | + jobiter = t.job(job_order_object, |
| 98 | + self.output_callback, |
| 99 | + **kwargs) |
| 100 | + |
| 101 | + try: |
| 102 | + for r in jobiter: |
| 103 | + if r: |
| 104 | + builder = kwargs.get("builder", None) # type: Builder |
| 105 | + if builder is not None: |
| 106 | + r.builder = builder |
| 107 | + if r.outdir: |
| 108 | + self.output_dirs.add(r.outdir) |
| 109 | + r.run(**kwargs) |
| 110 | + else: |
| 111 | + logger.error("Workflow cannot make any more progress.") |
| 112 | + break |
| 113 | + except WorkflowException: |
| 114 | + raise |
| 115 | + except Exception as e: |
| 116 | + logger.exception("Got workflow error") |
| 117 | + raise WorkflowException(Text(e)) |
| 118 | + |
| 119 | + |
| 120 | +class MultithreadedJobExecutor(JobExecutor): |
| 121 | + def __init__(self): |
| 122 | + super(MultithreadedJobExecutor, self).__init__() |
| 123 | + self.threads = set() |
| 124 | + self.exceptions = [] |
| 125 | + |
| 126 | + def run_job(self, |
| 127 | + job, # type: JobBase |
| 128 | + **kwargs # type: Any |
| 129 | + ): |
| 130 | + # type: (...) -> None |
| 131 | + def runner(): |
| 132 | + try: |
| 133 | + job.run(**kwargs) |
| 134 | + except WorkflowException as e: |
| 135 | + self.exceptions.append(e) |
| 136 | + except Exception as e: |
| 137 | + self.exceptions.append(WorkflowException(Text(e))) |
| 138 | + |
| 139 | + self.threads.remove(thread) |
| 140 | + |
| 141 | + thread = threading.Thread(target=runner) |
| 142 | + thread.daemon = True |
| 143 | + self.threads.add(thread) |
| 144 | + thread.start() |
| 145 | + |
| 146 | + def wait_for_next_completion(self): # type: () -> None |
| 147 | + if self.exceptions: |
| 148 | + raise self.exceptions[0] |
| 149 | + |
| 150 | + def run_jobs(self, |
| 151 | + t, # type: Process |
| 152 | + job_order_object, # type: Dict[Text, Any] |
| 153 | + logger, |
| 154 | + **kwargs # type: Any |
| 155 | + ): |
| 156 | + |
| 157 | + jobiter = t.job(job_order_object, self.output_callback, **kwargs) |
| 158 | + |
| 159 | + for r in jobiter: |
| 160 | + if r: |
| 161 | + builder = kwargs.get("builder", None) # type: Builder |
| 162 | + if builder is not None: |
| 163 | + r.builder = builder |
| 164 | + if r.outdir: |
| 165 | + self.output_dirs.add(r.outdir) |
| 166 | + self.run_job(r, **kwargs) |
| 167 | + else: |
| 168 | + if len(self.threads): |
| 169 | + self.wait_for_next_completion() |
| 170 | + else: |
| 171 | + logger.error("Workflow cannot make any more progress.") |
| 172 | + break |
| 173 | + |
| 174 | + while len(self.threads) > 0: |
| 175 | + self.wait_for_next_completion() |
0 commit comments