|
| 1 | +# NOTE: scaffold — bodies are stubs |
| 2 | +"""Ephys recording processing DAG for U19. |
| 3 | +
|
| 4 | +Runs @hourly. Polls for active processing jobs and drives each through the |
| 5 | +linear pipeline: raw transfer → SLURM preprocessing → processed transfer → |
| 6 | +DataJoint element populate. |
| 7 | +
|
| 8 | +All task bodies are TODO stubs. Dynamic mapping is stubbed with a comment |
| 9 | +placeholder — a linear task graph is wired instead so the DAG parses cleanly. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import logging |
| 15 | +from datetime import datetime, timedelta |
| 16 | + |
| 17 | +from airflow.sdk import dag, task |
| 18 | + |
| 19 | +log = logging.getLogger(__name__) |
| 20 | + |
| 21 | +# NOTE: u19 plugin imports are inside task bodies (TODO stubs) so the DAG |
| 22 | +# parses even when plugins/ is not on PYTHONPATH at collection time. |
| 23 | + |
| 24 | +default_args = { |
| 25 | + "owner": "u19", |
| 26 | + "retries": 1, |
| 27 | + "retry_delay": timedelta(minutes=10), |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +@dag( |
| 32 | + dag_id="u19_ephys_processing", |
| 33 | + schedule="@hourly", |
| 34 | + start_date=datetime(2024, 1, 1), |
| 35 | + catchup=False, |
| 36 | + default_args=default_args, |
| 37 | + tags=["u19", "ephys", "processing"], |
| 38 | +) |
| 39 | +def u19_ephys_processing() -> None: |
| 40 | + """Ephys processing DAG. |
| 41 | +
|
| 42 | + Intended flow (one task-group per active job):: |
| 43 | +
|
| 44 | + request_raw_transfer |
| 45 | + → wait_raw_transfer |
| 46 | + → submit_slurm |
| 47 | + → wait_slurm |
| 48 | + → request_proc_transfer |
| 49 | + → wait_proc_transfer |
| 50 | + → populate_element |
| 51 | +
|
| 52 | + Each step calls the matching plugin and calls dual_write_status after |
| 53 | + advancing the job state. |
| 54 | +
|
| 55 | + TODO: replace the linear stub below with dynamic task-group mapping once |
| 56 | + ``get_active_jobs()`` is implemented, e.g.:: |
| 57 | +
|
| 58 | + @task_group |
| 59 | + def process_job(job_id: int) -> None: |
| 60 | + ... |
| 61 | +
|
| 62 | + jobs = get_active_jobs() |
| 63 | + process_job.expand(job_id=jobs) |
| 64 | + """ |
| 65 | + |
| 66 | + @task(task_id="get_active_jobs") |
| 67 | + def get_active_jobs() -> list[int]: |
| 68 | + """Return list of job_ids that are ready for processing. |
| 69 | +
|
| 70 | + TODO: query recording_process.Processing for jobs in the |
| 71 | + 'transfer_request' status; return their job_ids. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + list[int] |
| 76 | + List of ``recording_process.Processing.job_id`` values. |
| 77 | + """ |
| 78 | + # TODO: from u19_pipeline import recording_process |
| 79 | + # jobs = (recording_process.Processing & {"status_processing_id": STATUS_TRANSFER_REQUEST}).fetch("job_id") |
| 80 | + # return list(jobs) |
| 81 | + log.info("get_active_jobs stub — returning empty list") |
| 82 | + return [] |
| 83 | + |
| 84 | + @task(task_id="request_raw_transfer") |
| 85 | + def request_raw_transfer(job_ids: list[int]) -> None: |
| 86 | + """Initiate Globus raw-data transfer from PNI to processing cluster. |
| 87 | +
|
| 88 | + Wraps u19.transfers.globus_transfer. |
| 89 | + Calls dual_write_status after initiating each transfer. |
| 90 | +
|
| 91 | + TODO: for job_id in job_ids: |
| 92 | + task_id = globus_transfer(pni_ep, tiger_ep, raw_src, raw_dst, job_id=job_id) |
| 93 | + dual_write_status({"job_id": job_id}, STATUS_RAW_TRANSFER_STARTED) |
| 94 | + """ |
| 95 | + # TODO: from u19.transfers import globus_transfer |
| 96 | + # from u19.status import dual_write_status |
| 97 | + pass |
| 98 | + |
| 99 | + @task(task_id="wait_raw_transfer") |
| 100 | + def wait_raw_transfer(job_ids: list[int]) -> None: |
| 101 | + """Poll until raw Globus transfer reaches SUCCEEDED state. |
| 102 | +
|
| 103 | + Wraps u19.transfers.check_transfer_status. |
| 104 | + Calls dual_write_status when transfer completes. |
| 105 | +
|
| 106 | + TODO: poll check_transfer_status(task_id) until SUCCEEDED/FAILED |
| 107 | + dual_write_status({"job_id": job_id}, STATUS_RAW_TRANSFER_DONE) |
| 108 | + """ |
| 109 | + # TODO: from u19.transfers import check_transfer_status |
| 110 | + # from u19.status import dual_write_status |
| 111 | + pass |
| 112 | + |
| 113 | + @task(task_id="submit_slurm") |
| 114 | + def submit_slurm(job_ids: list[int]) -> None: |
| 115 | + """Generate and submit a SLURM job for each active job. |
| 116 | +
|
| 117 | + Wraps u19.slurm.submit_slurm_job. |
| 118 | + Calls dual_write_status after submission. |
| 119 | +
|
| 120 | + TODO: for job_id in job_ids: |
| 121 | + slurm_job_id = submit_slurm_job(job_id, program_selection_params) |
| 122 | + dual_write_status({"job_id": job_id}, STATUS_SLURM_SUBMITTED) |
| 123 | + """ |
| 124 | + # TODO: from u19.slurm import submit_slurm_job |
| 125 | + # from u19.status import dual_write_status |
| 126 | + pass |
| 127 | + |
| 128 | + @task(task_id="wait_slurm") |
| 129 | + def wait_slurm(job_ids: list[int]) -> None: |
| 130 | + """Poll SLURM until the job reaches a terminal state. |
| 131 | +
|
| 132 | + Wraps u19.slurm.poll_slurm_job. |
| 133 | + Calls dual_write_status when SLURM job completes. |
| 134 | +
|
| 135 | + NOTE: should become a deferrable operator in Phase 2. |
| 136 | +
|
| 137 | + TODO: for job_id in job_ids: |
| 138 | + state = poll_slurm_job(slurm_job_id, job_id=job_id) |
| 139 | + dual_write_status({"job_id": job_id}, STATUS_SLURM_DONE if state=="COMPLETED" else STATUS_ERROR) |
| 140 | + """ |
| 141 | + # TODO: from u19.slurm import poll_slurm_job |
| 142 | + # from u19.status import dual_write_status |
| 143 | + pass |
| 144 | + |
| 145 | + @task(task_id="request_proc_transfer") |
| 146 | + def request_proc_transfer(job_ids: list[int]) -> None: |
| 147 | + """Initiate Globus processed-data transfer from cluster back to PNI. |
| 148 | +
|
| 149 | + Wraps u19.transfers.globus_transfer. |
| 150 | + Calls dual_write_status after initiating each transfer. |
| 151 | +
|
| 152 | + TODO: for job_id in job_ids: |
| 153 | + task_id = globus_transfer(tiger_ep, pni_ep, proc_src, proc_dst, job_id=job_id) |
| 154 | + dual_write_status({"job_id": job_id}, STATUS_PROC_TRANSFER_STARTED) |
| 155 | + """ |
| 156 | + # TODO: from u19.transfers import globus_transfer |
| 157 | + # from u19.status import dual_write_status |
| 158 | + pass |
| 159 | + |
| 160 | + @task(task_id="wait_proc_transfer") |
| 161 | + def wait_proc_transfer(job_ids: list[int]) -> None: |
| 162 | + """Poll until processed-data Globus transfer reaches SUCCEEDED state. |
| 163 | +
|
| 164 | + Wraps u19.transfers.check_transfer_status. |
| 165 | + Calls dual_write_status when transfer completes. |
| 166 | +
|
| 167 | + TODO: poll check_transfer_status(task_id) until SUCCEEDED/FAILED |
| 168 | + dual_write_status({"job_id": job_id}, STATUS_PROC_TRANSFER_DONE) |
| 169 | + """ |
| 170 | + # TODO: from u19.transfers import check_transfer_status |
| 171 | + # from u19.status import dual_write_status |
| 172 | + pass |
| 173 | + |
| 174 | + @task(task_id="populate_element") |
| 175 | + def populate_element(job_ids: list[int]) -> None: |
| 176 | + """Populate DataJoint ephys element tables for completed jobs. |
| 177 | +
|
| 178 | + Wraps u19_pipeline.automatic_job.ephys_element_populate. |
| 179 | + Calls dual_write_status after populate completes. |
| 180 | +
|
| 181 | + TODO: for job_id in job_ids: |
| 182 | + ephys_element_populate.run(job_id) |
| 183 | + dual_write_status({"job_id": job_id}, STATUS_COMPLETE) |
| 184 | + """ |
| 185 | + # TODO: import u19_pipeline.automatic_job.ephys_element_populate as ep |
| 186 | + # from u19.status import dual_write_status |
| 187 | + pass |
| 188 | + |
| 189 | + # ------------------------------------------------------------------------- |
| 190 | + # Wire linear stub graph |
| 191 | + # ------------------------------------------------------------------------- |
| 192 | + t_jobs = get_active_jobs() |
| 193 | + t_req_raw = request_raw_transfer(t_jobs) |
| 194 | + t_wait_raw = wait_raw_transfer(t_req_raw) |
| 195 | + t_submit_slurm = submit_slurm(t_wait_raw) |
| 196 | + t_wait_slurm = wait_slurm(t_submit_slurm) |
| 197 | + t_req_proc = request_proc_transfer(t_wait_slurm) |
| 198 | + t_wait_proc = wait_proc_transfer(t_req_proc) |
| 199 | + populate_element(t_wait_proc) |
| 200 | + |
| 201 | + |
| 202 | +u19_ephys_processing() |
0 commit comments