Skip to content

Commit

Permalink
feat: use literal class names for configs and deprecate `DAQ_JOB_TYPE…
Browse files Browse the repository at this point in the history
…_TO_CLASS`
  • Loading branch information
furkan-bilgin committed Nov 8, 2024
1 parent e4b02e5 commit 6d86cea
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion configs/examples/alert_slack.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
daq_job_type = "alert_slack"
daq_job_type = "DAQJobAlertSlack"
slack_webhook_url = "https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxxxxx"
2 changes: 1 addition & 1 deletion configs/examples/handle_alerts.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daq_job_type = "handle_alerts"
daq_job_type = "DAQJobHandleAlerts"

[store_config.csv]
file_path = "alerts.csv"
Expand Down
2 changes: 1 addition & 1 deletion configs/examples/handle_stats.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daq_job_type = "handle_stats"
daq_job_type = "DAQJobHandleStats"

[store_config.csv]
file_path = "stats.csv"
Expand Down
2 changes: 1 addition & 1 deletion configs/examples/healthcheck.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daq_job_type = "healthcheck"
daq_job_type = "DAQJobHealthcheck"

# Alert if restarted
[[healthcheck_stats]]
Expand Down
2 changes: 1 addition & 1 deletion configs/examples/n1081b.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daq_job_type = "n1081b"
daq_job_type = "DAQJobN1081B"
host = "1.2.3.4"
port = "8080"
password = "password"
Expand Down
2 changes: 1 addition & 1 deletion configs/examples/remote.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
daq_job_type = "remote"
daq_job_type = "DAQJobRemote"
zmq_local_url = "tcp://localhost:10191"
zmq_remote_urls = ["tcp://1.2.3.4:10191"]
2 changes: 1 addition & 1 deletion configs/examples/serve_http.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daq_job_type = "serve_http"
daq_job_type = "DAQJobServeHTTP"
serve_path = "out/"
host = "0.0.0.0"
port = 8000
2 changes: 1 addition & 1 deletion configs/examples/store_csv.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
daq_job_type = "store_csv"
daq_job_type = "DAQJobStoreCSV"
2 changes: 1 addition & 1 deletion configs/examples/store_root.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
daq_job_type = "store_root"
daq_job_type = "DAQJobStoreROOT"
2 changes: 1 addition & 1 deletion configs/examples/test.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
daq_job_type = "test"
daq_job_type = "DAQJobTest"
rand_min = 1
rand_max = 100

Expand Down
21 changes: 17 additions & 4 deletions src/daq/daq_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@
from daq.base import DAQJob, DAQJobThread
from daq.models import DAQJobConfig
from daq.types import DAQ_JOB_TYPE_TO_CLASS
from utils.subclasses import all_subclasses

ALL_DAQ_JOBS = all_subclasses(DAQJob)


def build_daq_job(toml_config: bytes) -> DAQJob:
generic_daq_job_config = msgspec.toml.decode(toml_config, type=DAQJobConfig)

if generic_daq_job_config.daq_job_type not in DAQ_JOB_TYPE_TO_CLASS:
daq_job_class = None

if generic_daq_job_config.daq_job_type in DAQ_JOB_TYPE_TO_CLASS:
daq_job_class = DAQ_JOB_TYPE_TO_CLASS[generic_daq_job_config.daq_job_type]
logging.warning(
f"DAQ job type '{generic_daq_job_config.daq_job_type}' is deprecated, please use the '{daq_job_class.__name__}' instead"
)
else:
for daq_job in ALL_DAQ_JOBS:
if daq_job.__name__ == generic_daq_job_config.daq_job_type:
daq_job_class = daq_job

if daq_job_class is None:
raise Exception(f"Invalid DAQ job type: {generic_daq_job_config.daq_job_type}")

# Get DAQ and DAQ config clasess based on daq_job_type
daq_job_class = DAQ_JOB_TYPE_TO_CLASS[generic_daq_job_config.daq_job_type]
# Get DAQ config clase based on daq_job_type
daq_job_config_class: DAQJobConfig = daq_job_class.config_type

# Load the config in
Expand Down
22 changes: 20 additions & 2 deletions src/daq/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,30 @@ class StorableDAQJobConfig(DAQJobConfig):
store_config: DAQJobStoreConfig


class DAQJobStoreConfigCSV(Struct):
class DAQJobStoreTarget(Struct):
instances: Optional[list["DAQJobStoreTargetInstance"]] = None
jobs: Optional[list["DAQJobStoreTargetJob"]] = None


class DAQJobStoreTargetInstance(Struct):
supervisor_id: Optional[str] = None
is_self: Optional[bool] = None


class DAQJobStoreTargetJob(Struct):
job_name: str


class DAQJobStoreConfigBase(Struct, kw_only=True):
target: Optional[DAQJobStoreTarget] = None


class DAQJobStoreConfigCSV(DAQJobStoreConfigBase):
file_path: str
add_date: bool
overwrite: Optional[bool] = None


class DAQJobStoreConfigROOT(Struct):
class DAQJobStoreConfigROOT(DAQJobStoreConfigBase):
file_path: str
add_date: bool
4 changes: 4 additions & 0 deletions src/utils/subclasses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def all_subclasses(cls):
return set(cls.__subclasses__()).union(
[s for c in cls.__subclasses__() for s in all_subclasses(c)]
)

0 comments on commit 6d86cea

Please sign in to comment.