Skip to content

Commit

Permalink
Add signal handler for graceful exit
Browse files Browse the repository at this point in the history
  • Loading branch information
berkayurun committed Sep 9, 2023
1 parent f5b3972 commit 6270125
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
35 changes: 34 additions & 1 deletion controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import logging
from multiprocessing import Manager, Process
from pathlib import Path
import signal
import subprocess
import sys
import tables
Expand All @@ -47,6 +48,24 @@

clogger = logging.getLogger(__name__)

stop_signal_received = False


def signal_handler(signum, frame):
global stop_signal_received
stop_signal_received = True


def register_signal_handlers(clogger, queue_output, p_logger):
signal.signal(
signal.SIGTERM,
signal_handler,
)
signal.signal(
signal.SIGINT,
signal_handler,
)


def build_ranges_dict(fault_dict):
"""
Expand Down Expand Up @@ -606,9 +625,23 @@ def controller(
continue
goldenrun_data[keyword] = pd.DataFrame(goldenrun_data[keyword])

pbar = tqdm(total=len(faultlist), desc="Simulating faults", disable=not len(faultlist))
# Handlers are used for a graceful exit, in case of a signal
register_signal_handlers(clogger, queue_output, p_logger)

pbar = tqdm(
total=len(faultlist), desc="Simulating faults", disable=not len(faultlist)
)
itter = 0
while 1:
if stop_signal_received:
clogger.info("Stopping the execution")
queue_output.put({"index": -4})

for p in p_list:
p["process"].kill()

break

if len(p_list) == 0 and itter == len(faultlist):
clogger.debug("Done inserting qemu jobs")
break
Expand Down
14 changes: 14 additions & 0 deletions hdf5logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import signal
import logging
import time

Expand All @@ -25,6 +26,14 @@
logger = logging.getLogger(__name__)


def register_signal_handlers():
"""
Ignore signals, they will be handled by the controller.py anyway
"""
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.signal(signal.SIGINT, signal.SIG_IGN)


# Tables for storing the elements from queue
class translation_block_exec_table(tables.IsDescription):
tb = tables.UInt64Col()
Expand Down Expand Up @@ -469,6 +478,8 @@ def hdf5collector(
log_config=False,
overwrite_faults=False,
):
register_signal_handlers()

prctl.set_name("logger")
prctl.set_proctitle("logger")
f = tables.open_file(hdf5path, mode, max_group_width=65536)
Expand Down Expand Up @@ -540,6 +551,9 @@ def hdf5collector(
process_backup(f, exp_group, exp, myfilter)
log_config = False
continue
elif exp["index"] == -4:
f.close()
return
else:
continue

Expand Down

0 comments on commit 6270125

Please sign in to comment.