<commit_before>import os
import sys
import web

curdir = os.path.dirname(__file__)
sys.path.insert(0, curdir)

from nailgun.settings import settings
from nailgun.api.handlers import check_client_content_type
from nailgun.api.handlers import forbid_client_caching
from nailgun.db import load_db_driver
from nailgun.urls import urls
from nailgun.logger import logger, FileLoggerMiddleware


class FlushingLogger(object):
    def __init__(self):
        self.opened = False
        self.fd = None

    @property
    def _fd(self):
        if not self.opened:
            self.fd = open(settings.ACCESS_LOG, "a+")
            self.opened = True
        return self.fd

    def __enter__(self):
        return self._fd

    def __exit__(self, type, value, traceback):
        self._fd.close()
        self.opened = False

    def write(self, data):
        self._fd.write(data)
        self._fd.flush()

    def close(self):
        self._fd.close()
        self.opened = False


def build_app():
    app = web.application(urls, locals())
    app.add_processor(load_db_driver)
    app.add_processor(forbid_client_caching)
    return app


def appstart():
    from nailgun.rpc import processed
    from nailgun.keepalive import keep_alive
    app = build_app()

    if not settings.FAKE_TASKS:
        rpc_process = processed.RPCProcess()
        logger.info("Running RPC process...")
        rpc_process.start()
    logger.info("Running KeepAlive watcher...")
    keep_alive.start()
    logger.info("Running WSGI app...")
    # seizes control
    if not int(settings.DEVELOPMENT):
        wsgifunc = app.wsgifunc(FileLoggerMiddleware)
    else:
        wsgifunc = app.wsgifunc()
    web.httpserver.runsimple(
        wsgifunc,
        (
            settings.LISTEN_ADDRESS,
            int(settings.LISTEN_PORT)
        )
    )
    logger.info("Stopping WSGI app...")
    if not settings.FAKE_TASKS:
        logger.info("Stopping RPC process...")
        rpc_process.terminate()
    logger.info("Stopping KeepAlive watcher...")
    keep_alive.join()
    logger.info("Done")<commit_msg>keepalive only for non-fake tasks<commit_after>import os
import sys
import web

curdir = os.path.dirname(__file__)
sys.path.insert(0, curdir)

from nailgun.settings import settings
from nailgun.api.handlers import check_client_content_type
from nailgun.api.handlers import forbid_client_caching
from nailgun.db import load_db_driver
from nailgun.urls import urls
from nailgun.logger import logger, FileLoggerMiddleware


class FlushingLogger(object):
    def __init__(self):
        self.opened = False
        self.fd = None

    @property
    def _fd(self):
        if not self.opened:
            self.fd = open(settings.ACCESS_LOG, "a+")
            self.opened = True
        return self.fd

    def __enter__(self):
        return self._fd

    def __exit__(self, type, value, traceback):
        self._fd.close()
        self.opened = False

    def write(self, data):
        self._fd.write(data)
        self._fd.flush()

    def close(self):
        self._fd.close()
        self.opened = False


def build_app():
    app = web.application(urls, locals())
    app.add_processor(load_db_driver)
    app.add_processor(forbid_client_caching)
    return app


def appstart():
    from nailgun.rpc import processed
    from nailgun.keepalive import keep_alive
    app = build_app()

    if not settings.FAKE_TASKS:
        logger.info("Running KeepAlive watcher...")
        keep_alive.start()
        rpc_process = processed.RPCProcess()
        logger.info("Running RPC process...")
        rpc_process.start()
    logger.info("Running WSGI app...")
    # seizes control
    if not int(settings.DEVELOPMENT):
        wsgifunc = app.wsgifunc(FileLoggerMiddleware)
    else:
        wsgifunc = app.wsgifunc()
    web.httpserver.runsimple(
        wsgifunc,
        (
            settings.LISTEN_ADDRESS,
            int(settings.LISTEN_PORT)
        )
    )
    logger.info("Stopping WSGI app...")
    if not settings.FAKE_TASKS:
        logger.info("Stopping KeepAlive watcher...")
        keep_alive.join()
        logger.info("Stopping RPC process...")
        rpc_process.terminate()
    logger.info("Done")
{'fraction_non_alphanumeric': 0.10033365109628217, 'fraction_numerical': 0.00047664442326024784, 'mean_word_length': 2.5810580204778155, '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': '5684507', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1298, 'n_words': 291}