Skip to content

Commit 8dbf626

Browse files
delete from database, just like we delete from MQTT
1 parent c24548b commit 8dbf626

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

pioreactor/background_jobs/base.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def _publish_setting(self, setting: str) -> None:
674674
)
675675

676676
with JobManager() as jm:
677-
jm.upsert_setting(self._job_id, setting_name, str(value) if value is not None else "")
677+
jm.upsert_setting(self._job_id, setting_name, value)
678678

679679
def _set_up_exit_protocol(self) -> None:
680680
# here, we set up how jobs should disconnect and exit.
@@ -796,7 +796,7 @@ def disconnected(self) -> None:
796796
self.logger.debug(e, exc_info=True)
797797

798798
# remove attrs from MQTT
799-
self._clear_mqtt_cache()
799+
self._clear_caches()
800800

801801
self._log_state(self.state)
802802

@@ -951,7 +951,7 @@ def _confirm_state_in_broker(self, message: pt.MQTTMessage) -> None:
951951
sleep(1)
952952
self._publish_setting("state")
953953

954-
def _clear_mqtt_cache(self) -> None:
954+
def _clear_caches(self) -> None:
955955
"""
956956
From homie: Devices can remove old properties and nodes by publishing a zero-length payload on the respective topics.
957957
Use "persist" to keep it from clearing.
@@ -962,29 +962,31 @@ def _clear_mqtt_cache(self) -> None:
962962
retain=True,
963963
)
964964

965-
for attr, metadata_on_attr in self.published_settings.items():
966-
if not metadata_on_attr.get("persist", False):
965+
with JobManager() as jm:
966+
for setting, metadata_on_attr in self.published_settings.items():
967+
if not metadata_on_attr.get("persist", False):
968+
self.publish(
969+
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}",
970+
None,
971+
retain=True,
972+
)
973+
jm.upsert_setting(self._job_id, setting, None)
974+
967975
self.publish(
968-
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}",
976+
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}/$settable",
977+
None,
978+
retain=True,
979+
)
980+
self.publish(
981+
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}/$datatype",
982+
None,
983+
retain=True,
984+
)
985+
self.publish(
986+
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}/$unit",
969987
None,
970988
retain=True,
971989
)
972-
973-
self.publish(
974-
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}/$settable",
975-
None,
976-
retain=True,
977-
)
978-
self.publish(
979-
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}/$datatype",
980-
None,
981-
retain=True,
982-
)
983-
self.publish(
984-
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}/$unit",
985-
None,
986-
retain=True,
987-
)
988990

989991
def _check_for_duplicate_activity(self) -> None:
990992
if is_pio_job_running(self.job_name) and not is_testing_env():

pioreactor/utils/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def publish_setting(self, setting: str, value: Any) -> None:
254254
f"pioreactor/{self.unit}/{self.experiment}/{self.name}/{setting}", value, retain=True
255255
)
256256
with JobManager() as jm:
257-
jm.upsert_setting(self._job_id, setting, str(value) if value is not None else "")
257+
jm.upsert_setting(self._job_id, setting, value)
258258

259259

260260
@contextmanager
@@ -609,14 +609,23 @@ def register_and_set_running(
609609
assert isinstance(self.cursor.lastrowid, int)
610610
return self.cursor.lastrowid
611611

612-
def upsert_setting(self, job_id: JobMetadataKey, setting: str, value: str) -> None:
613-
update_query = """
614-
INSERT INTO pio_job_published_settings (setting, value, job_id)
615-
VALUES (:setting, :value, :job_id)
616-
ON CONFLICT (setting, job_id) DO
617-
UPDATE SET value = :value;
618-
"""
619-
self.cursor.execute(update_query, {"setting": setting, "value": value, "job_id": job_id})
612+
def upsert_setting(self, job_id: JobMetadataKey, setting: str, value: Any) -> None:
613+
if value is None:
614+
# delete
615+
delete_query = """
616+
DELETE FROM pio_job_published_settings WHERE setting = :setting and job_id = :job_id
617+
"""
618+
self.cursor.execute(delete_query, {"setting": setting, "job_id": job_id})
619+
else:
620+
# upsert
621+
update_query = """
622+
INSERT INTO pio_job_published_settings (setting, value, job_id)
623+
VALUES (:setting, :value, :job_id)
624+
ON CONFLICT (setting, job_id) DO
625+
UPDATE SET value = :value;
626+
"""
627+
self.cursor.execute(update_query, {"setting": setting, "value": str(value), "job_id": job_id})
628+
620629
self.conn.commit()
621630
return
622631

update_scripts/upcoming/update.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mv "$SCRIPT_DIR"/huey.service $HUEY_SERVICE_FILE
1313

1414
# Reload systemd to apply changes
1515
sudo systemctl daemon-reload
16+
sudo systemctl restart huey.service
1617

1718
sudo chown pioreactor:www-data /var/www/pioreactorui/__pycache__ || :
1819
sudo chown pioreactor:www-data /var/www/pioreactorui/pioreactorui/__pycache__ || :

0 commit comments

Comments
 (0)