Skip to content

Commit

Permalink
Try to defer exporter error
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Sep 11, 2023
1 parent 30282eb commit 0920039
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
14 changes: 4 additions & 10 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,11 @@ def _on_start(self, event: StartEvent) -> None:

try:
# Set up the postgresql_exporter options.
if not self._setup_exporter():
logger.info("Monitoring password not ready")
event.defer()
return
self._setup_exporter()
except snap.SnapError:
logger.exception("failed to set up postgresql_exporter options")
self.unit.status = BlockedStatus("failed to set up postgresql_exporter options")
event.defer()
return

# Open port
Expand All @@ -973,25 +971,21 @@ def _on_start(self, event: StartEvent) -> None:
# Bootstrap the cluster in the leader unit.
self._start_primary(event)

def _setup_exporter(self) -> bool:
def _setup_exporter(self) -> None:
"""Set up postgresql_exporter options."""
cache = snap.SnapCache()
postgres_snap = cache[POSTGRESQL_SNAP_NAME]
password = self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY)
if not password:
return False

postgres_snap.set(
{
"exporter.user": MONITORING_USER,
"exporter.password": password,
"exporter.password": self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY),
}
)
if postgres_snap.services[MONITORING_SNAP_SERVICE]["active"] is False:
postgres_snap.start(services=[MONITORING_SNAP_SERVICE], enable=True)
else:
postgres_snap.restart(services=[MONITORING_SNAP_SERVICE])
return True

def _start_primary(self, event: StartEvent) -> None:
"""Bootstrap the cluster."""
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ def test_on_start(
_enable_disable_extensions.assert_called_once()
self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))

@patch("charm.PostgresqlOperatorCharm.get_secret")
@patch("charm.snap.SnapCache")
@patch("charm.Patroni.get_postgresql_version")
@patch_network_get(private_address="1.1.1.1")
Expand Down Expand Up @@ -359,7 +358,6 @@ def test_on_start_replica(
_configure_patroni_on_unit,
_get_postgresql_version,
_snap_cache,
_get_secret,
):
_get_postgresql_version.return_value = "14.0"

Expand All @@ -369,7 +367,6 @@ def test_on_start_replica(
# Mock the passwords.
_get_password.return_value = "fake-operator-password"
_replication_password.return_value = "fake-replication-password"
_get_secret.return_value = "fake-monitoring-passwordl"

# Test an uninitialized cluster.
self.charm._peers.data[self.charm.app].update({"cluster_initialised": ""})
Expand Down

0 comments on commit 0920039

Please sign in to comment.