@@ -1042,7 +1042,7 @@ def _peers(self) -> Relation:
1042
1042
"""
1043
1043
return self .model .get_relation (PEER )
1044
1044
1045
- def push_tls_files_to_workload (self , container : Container = None ) -> None :
1045
+ def push_tls_files_to_workload (self , container : Container = None ) -> bool :
1046
1046
"""Uploads TLS files to the workload container."""
1047
1047
if container is None :
1048
1048
container = self .unit .get_container ("postgresql" )
@@ -1086,7 +1086,7 @@ def push_tls_files_to_workload(self, container: Container = None) -> None:
1086
1086
group = WORKLOAD_OS_GROUP ,
1087
1087
)
1088
1088
1089
- self .update_config ()
1089
+ return self .update_config ()
1090
1090
1091
1091
def _restart (self , event : RunWithLock ) -> None :
1092
1092
"""Restart PostgreSQL."""
@@ -1109,7 +1109,20 @@ def _restart(self, event: RunWithLock) -> None:
1109
1109
# Start or stop the pgBackRest TLS server service when TLS certificate change.
1110
1110
self .backup .start_stop_pgbackrest_service ()
1111
1111
1112
- def update_config (self ) -> None :
1112
+ @property
1113
+ def _is_workload_running (self ) -> bool :
1114
+ """Returns whether the workload is running (in an active state)."""
1115
+ container = self .unit .get_container ("postgresql" )
1116
+ if not container .can_connect ():
1117
+ return False
1118
+
1119
+ services = container .pebble .get_services (names = [self ._postgresql_service ])
1120
+ if len (services ) == 0 :
1121
+ return False
1122
+
1123
+ return services [0 ].current == ServiceStatus .ACTIVE
1124
+
1125
+ def update_config (self ) -> bool :
1113
1126
"""Updates Patroni config file based on the existence of the TLS files."""
1114
1127
# Update and reload configuration based on TLS files availability.
1115
1128
self ._patroni .render_patroni_yml_file (
@@ -1119,14 +1132,18 @@ def update_config(self) -> None:
1119
1132
stanza = self .app_peer_data .get ("stanza" ),
1120
1133
restore_stanza = self .app_peer_data .get ("restore-stanza" ),
1121
1134
)
1122
- if not self ._patroni . member_started :
1135
+ if not self ._is_workload_running :
1123
1136
# If Patroni/PostgreSQL has not started yet and TLS relations was initialised,
1124
1137
# then mark TLS as enabled. This commonly happens when the charm is deployed
1125
1138
# in a bundle together with the TLS certificates operator. This flag is used to
1126
1139
# know when to call the Patroni API using HTTP or HTTPS.
1127
1140
self .unit_peer_data .update ({"tls" : "enabled" if self .is_tls_enabled else "" })
1141
+ logger .debug ("Early exit update_config: Workload not started yet" )
1142
+ return True
1143
+
1144
+ if not self ._patroni .member_started :
1128
1145
logger .debug ("Early exit update_config: Patroni not started yet" )
1129
- return
1146
+ return False
1130
1147
1131
1148
restart_postgresql = self .is_tls_enabled != self .postgresql .is_tls_enabled ()
1132
1149
self ._patroni .reload_patroni_configuration ()
@@ -1140,6 +1157,8 @@ def update_config(self) -> None:
1140
1157
)
1141
1158
self .on [self .restart_manager .name ].acquire_lock .emit ()
1142
1159
1160
+ return True
1161
+
1143
1162
def _update_pebble_layers (self ) -> None :
1144
1163
"""Update the pebble layers to keep the health check URL up-to-date."""
1145
1164
container = self .unit .get_container ("postgresql" )
0 commit comments