Skip to content

Commit

Permalink
Less defers in charm
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Jul 27, 2023
1 parent e348f24 commit d86803c
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions charms/jimm-k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
)
from ops.charm import ActionEvent, CharmBase, RelationJoinedEvent
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, WaitingStatus
from ops.model import ActiveStatus, BlockedStatus, WaitingStatus, ErrorStatus

from state import State, requires_state, requires_state_setter

Expand All @@ -73,6 +73,9 @@
# This likely will just be JIMM's port.
PROMETHEUS_PORT = 8080

class DeferException(Exception):
"""Used to indicate to the calling function that an event should be deferred."""
pass

class JimmOperatorCharm(CharmBase):
"""JIMM Operator Charm."""
Expand Down Expand Up @@ -302,15 +305,21 @@ def _update_workload(self, event):
},
}
container.add_layer("jimm", pebble_layer, combine=True)
if self._ready():
if container.get_service(JIMM_SERVICE_NAME).is_running():
logger.info("replanning service")
container.replan()
try:
if self._ready():
if container.get_service(JIMM_SERVICE_NAME).is_running():
logger.info("replanning service")
container.replan()
else:
logger.info("starting service")
container.start(JIMM_SERVICE_NAME)
self.unit.status = ActiveStatus("running")
if self.unit.is_leader():
self.app.status = ActiveStatus()
else:
logger.info("starting service")
container.start(JIMM_SERVICE_NAME)
self.unit.status = ActiveStatus("running")
else:
logger.info("workload not ready - returning")
return
except DeferException:
logger.info("workload container not ready - deferring")
event.defer()
return
Expand All @@ -337,11 +346,23 @@ def _on_stop(self, _):
container.stop(JIMM_SERVICE_NAME)
except Exception as e:
logger.info("failed to stop the jimm service: {}".format(e))
self._ready()
try:
self._ready()
except DeferException:
logger.info("workload not ready")
return

def _on_update_status(self, _):
"""Update the status of the charm."""
self._ready()
if self.unit.status.name == ErrorStatus.name:
# Skip ready check if unit in error to allow for error resolution.
logger.info("unit in error status, skipping ready check")
return
try:
self._ready()
except DeferException:
logger.info("workload not ready")
return

@requires_state_setter
def _on_dashboard_relation_joined(self, event: RelationJoinedEvent):
Expand Down Expand Up @@ -410,9 +431,7 @@ def _ready(self):
self.unit.status = WaitingStatus("stopped")
return True
else:
logger.error("cannot connect to workload container")
self.unit.status = WaitingStatus("waiting for jimm workload")
return False
raise DeferException

def _get_network_address(self, event):
return str(self.model.get_binding(event.relation).network.egress_subnets[0].network_address)
Expand Down Expand Up @@ -562,11 +581,6 @@ def _on_openfga_store_created(self, event: OpenFGAStoreCreateEvent):

@requires_state
def _get_dns_name(self, event):
if not self._state.is_ready():
event.defer()
logger.warning("State is not ready")
return None

default_dns_name = "{}.{}-endpoints.{}.svc.cluster.local".format(
self.unit.name.replace("/", "-"),
self.app.name,
Expand Down

0 comments on commit d86803c

Please sign in to comment.