Skip to content

Commit 6059699

Browse files
committed
Adds an option to exit on encounters with unexpected container states
Closes #57
1 parent d9c9a92 commit 6059699

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

deck_chores/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ def generate_config() -> None:
6969
)
7070
cfg.debug = trueish(getenv('DEBUG', 'no'))
7171
cfg.default_max = int(getenv('DEFAULT_MAX', 1))
72+
cfg.exit_on_unexpected_container_states = trueish(
73+
getenv('EXIT_ON_UNEXPECTED_CONTAINER_STATES', 'no')
74+
)
7275
cfg.job_executor_pool_size = int(getenv('JOB_POOL_SIZE', 10))
7376
cfg.job_name_regex = getenv("JOB_NAME_REGEX", "[a-z0-9-]+")
7477
cfg.label_ns = getenv('LABEL_NAMESPACE', 'deck-chores') + '.'

deck_chores/jobs.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import logging
2+
import os
3+
from signal import SIGUSR2
24
from typing import Dict, Iterator, Mapping, Tuple
35

46
from apscheduler import events
@@ -83,21 +85,27 @@ def on_missed(event: events.JobExecutionEvent) -> None:
8385
####
8486

8587

88+
def handle_exec_error(message: str):
89+
log.error(message)
90+
if cfg.exit_on_unexpected_container_states:
91+
os.kill(os.getpid(), SIGUSR2)
92+
93+
8694
def exec_job(**definition) -> Tuple[int, bytes]:
8795
job_id = definition['job_id']
8896
container_id = definition['container_id']
8997
log.info(f"{container_name(container_id)}: Executing '{definition['job_name']}'.")
9098

91-
# some sanity checks, to be removed eventually
99+
# some sanity checks
92100
assert scheduler.get_job(job_id) is not None
93101
if cfg.client.containers.list(filters={'id': container_id, 'status': 'paused'}):
94-
raise AssertionError('Container is paused.')
102+
handle_exec_error('Container is paused.')
95103

96104
if not cfg.client.containers.list(
97105
filters={'id': container_id, 'status': 'running'}
98106
):
99107
assert scheduler.get_job(job_id) is None
100-
raise AssertionError('Container is not running.')
108+
handle_exec_error('Container is not running.')
101109
# end of sanity checks
102110

103111
return cfg.client.containers.get(container_id).exec_run(

deck_chores/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import sys
55
from datetime import datetime, timedelta, timezone
6-
from signal import signal, SIGINT, SIGTERM, SIGUSR1
6+
from signal import signal, SIGINT, SIGTERM, SIGUSR1, SIGUSR2
77
from typing import Optional
88

99
from apscheduler.schedulers import SchedulerNotRunningError
@@ -62,9 +62,15 @@ def sigusr1_handler(signum, frame):
6262
log.info(job.kwargs)
6363

6464

65+
def sigusr2_handler(signum, frame):
66+
log.info("SIGUSR2 received, exiting with error.")
67+
raise SystemExit(1)
68+
69+
6570
signal(SIGINT, sigint_handler)
6671
signal(SIGTERM, sigterm_handler)
6772
signal(SIGUSR1, sigusr1_handler)
73+
signal(SIGUSR2, sigusr2_handler)
6874

6975

7076
####

docs/usage.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ deck-chore's behaviour is defined by these environment variables:
374374

375375
The URL of the Docker daemon to connect to.
376376

377+
.. envvar:: EXIT_ON_UNEXPECTED_CONTAINER_STATES
378+
379+
default: ``no``
380+
381+
Exit *deck-chores* when it finds itself confused with unexpected states of containers.
382+
If you expect a restart, configure your process manager like *systemd* or the *Docker daemon*
383+
accordingly.
384+
377385
.. envvar:: STDERR_LEVEL
378386

379387
default: ``NOTSET``

tests/test_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def every_file_exists(*args, **kwargs):
3232
'debug': False,
3333
'default_max': 1,
3434
'default_flags': ('image', 'service'),
35+
'exit_on_unexpected_container_states': False,
3536
'job_executor_pool_size': 10,
3637
'job_name_regex': '[a-z0-9-]+',
3738
'label_ns': 'deck-chores.',

0 commit comments

Comments
 (0)