<commit_before>    #
# ========================================================
# Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
# ========================================================


from optparse import make_option

import threading
import sys
import traceback
import signal
import os
from daemon.pidlockfile import PIDLockFile
from daemon import DaemonContext
from django.core.management.base import BaseCommand
from chroma_core.services.log import log_set_filename, log_register, log_enable_stdout
from chroma_core.services.rpc import RpcClientFactory


log = log_register(__name__.split('.')[-1])


class ServiceMain(threading.Thread):
    """Given a ChromaService instance, execute its start() method, logging any
    exception thrown"""
    def __init__(self, service, *args, **kwargs):
        super(ServiceMain, self).__init__(*args, **kwargs)
        self.service = service

    def run(self):
        service_name = self.service.__class__.__module__.split('.')[-1]
        self.service.log = log_register(service_name)
        try:
            log.info("Running %s" % service_name)
            self.service.start()
        except Exception:
            exc_info = sys.exc_info()
            backtrace = '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))
            log.warning("Exception from run_main_loop.  backtrace: %s" % backtrace)


class Command(BaseCommand):
    help = """Run a single ChromaService in a new plugin."""
    option_list = BaseCommand.option_list + (
        make_option('--gevent', action = 'store_true', dest = 'gevent', default = False),
        make_option('--lightweight-rpc', action = 'store_true', dest = 'lightweight_rpc', default = False),
        make_option('--verbose', action = 'store_true', dest = 'verbose', default = False),
        make_option('--name', dest = 'name', default = 'chroma_service'),
        make_option('--daemon', dest = 'daemon', action = 'store_true', default = None),
        make_option('--pid-file', dest = 'pid-file', default = None),
    )

    def execute(self, *args, **options):
        if options['daemon']:
            pid_file = options['pid-file']
            if os.path.exists(pid_file + ".lock") or os.path.exists(pid_file):
                try:
                    pid = int(open(pid_file).read())
                    os.kill(pid, 0)
                except (ValueError, OSError):
                    # Not running, delete stale PID file
                    sys.stderr.write("Removing stale PID file\n")
                    try:
                        os.remove(pid_file)
                        os.remove(pid_file + ".lock")
                    except OSError, e:
                        import errno
                        if e.errno != errno.ENOENT:
                            raise e
                else:
                    # Running, we should refuse to run
                    raise RuntimeError("Daemon is already running (PID %s)" % pid)

            with DaemonContext(pidfile = PIDLockFile(pid_file)):
                self._execute_inner(*args, **options)
        else:
            self._execute_inner(*args, **options)

    def _execute_inner(self, *args, **options):
        if options['verbose']:
            log_enable_stdout()

        if options['gevent']:
            from gevent.monkey import patch_all
            patch_all()

        from chroma_core.lib.service_config import ServiceConfig
        if not ServiceConfig().configured():
            sys.stderr.write("Chroma is not configured, please run chroma-config setup first\n")<commit_msg>HYD-1589: Remove stale .pid.lock file and continue even if .pid file not present.<commit_after>    #
# ========================================================
# Copyright (c) 2012 Whamcloud, Inc.  All rights reserved.
# ========================================================


from optparse import make_option

import threading
import sys
import traceback
import signal
import os
from daemon.pidlockfile import PIDLockFile
from daemon import DaemonContext
from django.core.management.base import BaseCommand
from chroma_core.services.log import log_set_filename, log_register, log_enable_stdout
from chroma_core.services.rpc import RpcClientFactory


log = log_register(__name__.split('.')[-1])


class ServiceMain(threading.Thread):
    """Given a ChromaService instance, execute its start() method, logging any
    exception thrown"""
    def __init__(self, service, *args, **kwargs):
        super(ServiceMain, self).__init__(*args, **kwargs)
        self.service = service

    def run(self):
        service_name = self.service.__class__.__module__.split('.')[-1]
        self.service.log = log_register(service_name)
        try:
            log.info("Running %s" % service_name)
            self.service.start()
        except Exception:
            exc_info = sys.exc_info()
            backtrace = '\n'.join(traceback.format_exception(*(exc_info or sys.exc_info())))
            log.warning("Exception from run_main_loop.  backtrace: %s" % backtrace)


class Command(BaseCommand):
    help = """Run a single ChromaService in a new plugin."""
    option_list = BaseCommand.option_list + (
        make_option('--gevent', action = 'store_true', dest = 'gevent', default = False),
        make_option('--lightweight-rpc', action = 'store_true', dest = 'lightweight_rpc', default = False),
        make_option('--verbose', action = 'store_true', dest = 'verbose', default = False),
        make_option('--name', dest = 'name', default = 'chroma_service'),
        make_option('--daemon', dest = 'daemon', action = 'store_true', default = None),
        make_option('--pid-file', dest = 'pid-file', default = None),
    )

    def execute(self, *args, **options):
        if options['daemon']:
            pid_file = options['pid-file']
            if os.path.exists(pid_file + ".lock") or os.path.exists(pid_file):
                try:
                    pid = int(open(pid_file).read())
                    os.kill(pid, 0)
                except (ValueError, OSError, IOError):
                    # Not running, delete stale PID file
                    sys.stderr.write("Removing stale PID file\n")
                    import errno
                    try:
                        os.remove(pid_file)
                    except OSError, e:
                        if e.errno != errno.ENOENT:
                            raise e
                    try:
                        os.remove(pid_file + ".lock")
                    except OSError, e:
                        if e.errno != errno.ENOENT:
                            raise e
                else:
                    # Running, we should refuse to run
                    raise RuntimeError("Daemon is already running (PID %s)" % pid)

            with DaemonContext(pidfile = PIDLockFile(pid_file)):
                self._execute_inner(*args, **options)
        else:
            self._execute_inner(*args, **options)

    def _execute_inner(self, *args, **options):
        if options['verbose']:
            log_enable_stdout()

        if options['gevent']:
            from gevent.monkey import patch_all
            patch_all()

        from chroma_core.lib.service_config import ServiceConfig
        if not ServiceConfig().configured():
            sys.stderr.write("Chroma is not configured, please run chroma-config setup first\n")
{'fraction_non_alphanumeric': 0.1390728476821192, 'fraction_numerical': 0.0024327611839437763, 'mean_word_length': 2.278688524590164, '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': '3227260', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1946, 'n_words': 511}