Skip to content

Commit

Permalink
delete from database, just like we delete from MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Oct 13, 2024
1 parent c24548b commit 8dbf626
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
46 changes: 24 additions & 22 deletions pioreactor/background_jobs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def _publish_setting(self, setting: str) -> None:
)

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

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

# remove attrs from MQTT
self._clear_mqtt_cache()
self._clear_caches()

self._log_state(self.state)

Expand Down Expand Up @@ -951,7 +951,7 @@ def _confirm_state_in_broker(self, message: pt.MQTTMessage) -> None:
sleep(1)
self._publish_setting("state")

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

for attr, metadata_on_attr in self.published_settings.items():
if not metadata_on_attr.get("persist", False):
with JobManager() as jm:
for setting, metadata_on_attr in self.published_settings.items():
if not metadata_on_attr.get("persist", False):
self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}",
None,
retain=True,
)
jm.upsert_setting(self._job_id, setting, None)

self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}",
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}/$settable",
None,
retain=True,
)
self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}/$datatype",
None,
retain=True,
)
self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{setting}/$unit",
None,
retain=True,
)

self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}/$settable",
None,
retain=True,
)
self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}/$datatype",
None,
retain=True,
)
self.publish(
f"pioreactor/{self.unit}/{self.experiment}/{self.job_name}/{attr}/$unit",
None,
retain=True,
)

def _check_for_duplicate_activity(self) -> None:
if is_pio_job_running(self.job_name) and not is_testing_env():
Expand Down
27 changes: 18 additions & 9 deletions pioreactor/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def publish_setting(self, setting: str, value: Any) -> None:
f"pioreactor/{self.unit}/{self.experiment}/{self.name}/{setting}", value, retain=True
)
with JobManager() as jm:
jm.upsert_setting(self._job_id, setting, str(value) if value is not None else "")
jm.upsert_setting(self._job_id, setting, value)


@contextmanager
Expand Down Expand Up @@ -609,14 +609,23 @@ def register_and_set_running(
assert isinstance(self.cursor.lastrowid, int)
return self.cursor.lastrowid

def upsert_setting(self, job_id: JobMetadataKey, setting: str, value: str) -> None:
update_query = """
INSERT INTO pio_job_published_settings (setting, value, job_id)
VALUES (:setting, :value, :job_id)
ON CONFLICT (setting, job_id) DO
UPDATE SET value = :value;
"""
self.cursor.execute(update_query, {"setting": setting, "value": value, "job_id": job_id})
def upsert_setting(self, job_id: JobMetadataKey, setting: str, value: Any) -> None:
if value is None:
# delete
delete_query = """
DELETE FROM pio_job_published_settings WHERE setting = :setting and job_id = :job_id
"""
self.cursor.execute(delete_query, {"setting": setting, "job_id": job_id})
else:
# upsert
update_query = """
INSERT INTO pio_job_published_settings (setting, value, job_id)
VALUES (:setting, :value, :job_id)
ON CONFLICT (setting, job_id) DO
UPDATE SET value = :value;
"""
self.cursor.execute(update_query, {"setting": setting, "value": str(value), "job_id": job_id})

self.conn.commit()
return

Expand Down
1 change: 1 addition & 0 deletions update_scripts/upcoming/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mv "$SCRIPT_DIR"/huey.service $HUEY_SERVICE_FILE

# Reload systemd to apply changes
sudo systemctl daemon-reload
sudo systemctl restart huey.service

sudo chown pioreactor:www-data /var/www/pioreactorui/__pycache__ || :
sudo chown pioreactor:www-data /var/www/pioreactorui/pioreactorui/__pycache__ || :
Expand Down

0 comments on commit 8dbf626

Please sign in to comment.