Skip to content

Commit

Permalink
Add doWriteAlerts and excpetion when neither produce nor write is set
Browse files Browse the repository at this point in the history
  • Loading branch information
bsmartradio committed Feb 8, 2024
1 parent 55ecfac commit b6e01f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion python/lsst/ap/association/packageAlerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class PackageAlertsConfig(pexConfig.Config):
default=False,
)

doWriteAlerts = pexConfig.Field(
dtype=bool,
doc="Write alerts to disk if true. Set to true by default",
default=True,
)


class PackageAlertsTask(pipeBase.Task):
"""Tasks for packaging Dia and Pipelines data into Avro alert packages.
Expand Down Expand Up @@ -207,12 +213,18 @@ def run(self,
if self.config.doProduceAlerts and "confluent_kafka" in sys.modules:
self.produceAlerts(alerts, ccdVisitId)

else:
elif self.config.doProduceAlerts and "confluent_kafka" not in sys.modules:
raise Exception("Produce alerts is set but confluent_kafka is not in the environment.")

if self.config.doWriteAlerts:
with open(os.path.join(self.config.alertWriteLocation,
f"{ccdVisitId}.avro"),
"wb") as f:
self.alertSchema.store_alerts(f, alerts)

if not self.config.doProduceAlerts and not self.config.doWriteAlerts:
raise Exception("Neither produce alerts nor write alerts is set.")

def _patchDiaSources(self, diaSources):
"""Add the ``programId`` column to the data.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_packageAlerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,22 @@ def testRun_with_produce(self, mock_produceAlerts):

self.assertEqual(mock_produceAlerts.call_count, 1)

def testRun_without_produce_or_write(self):
"""Test that packageAlerts calls produceAlerts when doProduceAlerts
is set to True.
"""
packConfig = PackageAlertsConfig(doProduceAlerts=False,
doWriteAlerts=False)
packageAlerts = PackageAlertsTask(config=packConfig)

with self.assertRaisesRegex(Exception, "Neither produce alerts"):
packageAlerts.run(self.diaSources,
self.diaObjects,
self.diaSourceHistory,
self.diaForcedSources,
self.exposure,
self.exposure)

def test_serialize_alert_round_trip(self, **kwargs):

ConfigClass = PackageAlertsConfig()
Expand Down

0 comments on commit b6e01f0

Please sign in to comment.