<commit_before>from flow.configuration import defaults
from flow.configuration.commands import determine_command
from flow.configuration.inject.initialize import initialize_injector
from flow.configuration.metrics import initialize_metrics
from flow.configuration.parser import parse_arguments
from flow.configuration.settings.load import load_settings

import flow.exit_codes
import flow.util.stats
import logging.config
import pika
import sys
import traceback


LOG = logging.getLogger(__name__)


def main():
    try:
        exit_code = naked_main()

    except SystemExit as e:
        exit_code = e.code
    except:
        sys.stderr.write('Unhandled exception:\n')
        traceback.print_exc()
        exit_code = flow.exit_codes.UNKNOWN_ERROR

    return exit_code

<commit_msg>add signal handlers to main<commit_after>from flow.configuration import defaults
from flow.configuration.commands import determine_command
from flow.configuration.inject.initialize import initialize_injector
from flow.configuration.metrics import initialize_metrics
from flow.configuration.parser import parse_arguments
from flow.configuration.settings.load import load_settings

import flow.exit_codes
import flow.util.stats
import logging.config
import pika
import signal
import sys
import traceback


LOG = logging.getLogger(__name__)


def main():
    try:
        setup_exit_handler(signal.SIGINT, [signal.SIGINT, signal.SIGTERM])
        setup_exit_handler(signal.SIGTERM, [signal.SIGTERM])

        exit_code = naked_main()

    except SystemExit as e:
        exit_code = e.code
    except:
        sys.stderr.write('Unhandled exception:\n')
        traceback.print_exc()
        exit_code = flow.exit_codes.UNKNOWN_ERROR

    return exit_code


def setup_exit_handler(signum, child_signals):
    def _handler(signum, frame):
        LOG.critical('Received signal %d: %s', signum, frame)
        exit_process(exit_codes.UNKNOWN_ERROR, child_signals=child_signals)
    signal.signal(signum, _handler)


{'fraction_non_alphanumeric': 0.07060590886329494, 'fraction_numerical': 0.0, 'mean_word_length': 4.271767810026385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4756219', 'n_tokens_mistral': 555, 'n_tokens_neox': 544, 'n_words': 141}