|
| 1 | +import typing as ty |
| 2 | +from pathlib import Path |
| 3 | +import cloudpickle as cp |
| 4 | +import logging |
| 5 | +import attrs |
| 6 | +import psij |
| 7 | +from pydra.engine.job import Job |
| 8 | +from pydra.workers import base |
| 9 | + |
| 10 | +logger = logging.getLogger("pydra.worker") |
| 11 | + |
| 12 | +if ty.TYPE_CHECKING: |
| 13 | + from pydra.engine.result import Result |
| 14 | + |
| 15 | + |
| 16 | +@attrs.define |
| 17 | +class Worker(base.Worker): |
| 18 | + """A worker to execute tasks using PSI/J.""" |
| 19 | + |
| 20 | + subtype = "local" |
| 21 | + |
| 22 | + def make_spec(self, cmd=None, arg=None): |
| 23 | + """ |
| 24 | + Create a PSI/J job specification. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + cmd : str, optional |
| 29 | + Executable command. Defaults to None. |
| 30 | + arg : list, optional |
| 31 | + List of arguments. Defaults to None. |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + psij.JobDef |
| 36 | + PSI/J job specification. |
| 37 | + """ |
| 38 | + spec = psij.JobSpec() |
| 39 | + spec.executable = cmd |
| 40 | + spec.arguments = arg |
| 41 | + |
| 42 | + return spec |
| 43 | + |
| 44 | + def make_job(self, spec, attributes): |
| 45 | + """ |
| 46 | + Create a PSI/J job. |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + task : psij.JobDef |
| 51 | + PSI/J job specification. |
| 52 | + attributes : any |
| 53 | + Job attributes. |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + psij.Job |
| 58 | + PSI/J job. |
| 59 | + """ |
| 60 | + job = psij.Job() |
| 61 | + job.spec = spec |
| 62 | + return job |
| 63 | + |
| 64 | + async def run( |
| 65 | + self, |
| 66 | + job: Job[base.TaskType], |
| 67 | + rerun: bool = False, |
| 68 | + ) -> "Result": |
| 69 | + """ |
| 70 | + Run a job (coroutine wrapper). |
| 71 | +
|
| 72 | + Raises |
| 73 | + ------ |
| 74 | + Exception |
| 75 | + If stderr is not empty. |
| 76 | +
|
| 77 | + Returns |
| 78 | + ------- |
| 79 | + None |
| 80 | + """ |
| 81 | + jex = psij.JobExecutor.get_instance(self.subtype) |
| 82 | + absolute_path = Path(__file__).parent |
| 83 | + |
| 84 | + cache_dir = job.cache_dir |
| 85 | + file_path = cache_dir / "runnable_function.pkl" |
| 86 | + with open(file_path, "wb") as file: |
| 87 | + cp.dump(job.run, file) |
| 88 | + func_path = absolute_path / "run_pickled.py" |
| 89 | + spec = self.make_spec("python", [func_path, file_path]) |
| 90 | + |
| 91 | + if rerun: |
| 92 | + spec.arguments.append("--rerun") |
| 93 | + |
| 94 | + spec.stdout_path = cache_dir / "demo.stdout" |
| 95 | + spec.stderr_path = cache_dir / "demo.stderr" |
| 96 | + |
| 97 | + psij_job = self.make_job(spec, None) |
| 98 | + jex.submit(psij_job) |
| 99 | + psij_job.wait() |
| 100 | + |
| 101 | + if spec.stderr_path.stat().st_size > 0: |
| 102 | + with open(spec.stderr_path, "r") as stderr_file: |
| 103 | + stderr_contents = stderr_file.read() |
| 104 | + raise Exception( |
| 105 | + f"stderr_path '{spec.stderr_path}' is not empty. Contents:\n{stderr_contents}" |
| 106 | + ) |
| 107 | + |
| 108 | + return job.result() |
| 109 | + |
| 110 | + def close(self): |
| 111 | + """Finalize the internal pool of tasks.""" |
| 112 | + pass |
0 commit comments