Skip to content

Commit d44e67b

Browse files
wmuldergovbcgov-brwang
authored andcommitted
DBC22-6666: Fix leader election script bug
1 parent 34d98ce commit d44e67b

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

compose/backend/leader-election.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def _is_expired(lease) -> bool:
106106
if renew_time is None:
107107
return True
108108
if isinstance(renew_time, str):
109-
renew_dt = datetime.strptime(renew_time, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
109+
ts = renew_time.replace('Z', '+00:00')
110+
renew_dt = datetime.fromisoformat(ts)
110111
else:
111112
renew_dt = renew_time # already a datetime
112113
age = (datetime.now(timezone.utc) - renew_dt).total_seconds()
@@ -197,9 +198,15 @@ def main():
197198
_stop_huey()
198199
leading = False
199200
else:
200-
# Check Huey is still up; if it died, exit so the pod restarts
201-
if not _huey_alive():
202-
logger.error("Huey exited unexpectedly — terminating pod so it restarts")
201+
if huey_process is None:
202+
# We hold the lease (e.g. after a container restart)
203+
# but haven't started the process yet.
204+
logger.info("Already hold lease after restart — starting Huey")
205+
leading = True
206+
_start_huey()
207+
elif not _huey_alive():
208+
# Huey was running, but now it's not.
209+
logger.error("Huey exited unexpectedly — terminating container so it restarts")
203210
os._exit(1)
204211

205212
elif _is_expired(lease):

0 commit comments

Comments
 (0)