Skip to content

Commit

Permalink
commands: time out waiting for conditions after 5min
Browse files Browse the repository at this point in the history
Put a time bounds on the code that waits for the conditions using
a context manager that wraps alarm.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
  • Loading branch information
phlogistonjohn authored and mergify[bot] committed Aug 8, 2024
1 parent 07f3f87 commit 406afac
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions sambacc/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
#

import contextlib
import logging
import signal
import time
import typing

from sambacc import samba_cmds
import sambacc.paths as paths
Expand Down Expand Up @@ -103,16 +106,32 @@ def _run_container_args(parser):
)


_COND_TIMEOUT = 5 * 60


@contextlib.contextmanager
def _timeout(timeout: int) -> typing.Iterator[None]:
def _handler(sig: int, frame: typing.Any) -> None:
raise RuntimeError("timed out waiting for conditions")

signal.signal(signal.SIGALRM, _handler)
signal.alarm(timeout)
yield
signal.alarm(0)
signal.signal(signal.SIGALRM, signal.SIG_DFL)


@commands.command(name="run", arg_func=_run_container_args)
def run_container(ctx: Context) -> None:
"""Run a specified server process."""
if ctx.cli.no_init and ctx.cli.setup:
raise Fail("can not specify both --no-init and --setup")

if ctx.cli.wait_for:
conditions = [_wait_for_conditions[n]() for n in ctx.cli.wait_for]
while not all(c.met(ctx) for c in conditions):
time.sleep(1)
with _timeout(_COND_TIMEOUT):
conditions = [_wait_for_conditions[n]() for n in ctx.cli.wait_for]
while not all(c.met(ctx) for c in conditions):
time.sleep(1)

# running servers expect to make use of ctdb whenever it is configured
ctx.expects_ctdb = True
Expand Down

0 comments on commit 406afac

Please sign in to comment.