<commit_before>import threading
import logging

from fnmatch import fnmatch
from urlparse import urlparse

from eeagent.util import make_id, unmake_id
from eeagent.execute import PidWrapper
from eeagent.eeagent_exceptions import EEAgentParameterException, EEAgentUnauthorizedException
from pidantic.pidantic_exceptions import PIDanticStateException

RESTARTABLE_STATES = []


def eeagent_lock(func):
    def call(self, *args, **kwargs):
        with self._lock:
            return func(self, *args, **kwargs)
    return call


class EEAgentCore(object):

    ee_name = None

    def __init__(self, CFG, process_managers_map, log):
        self.CFG = CFG
        self._process_managers_map = process_managers_map
        self.ee_name = CFG.eeagent.name

        self._log = log
        self._lock = threading.RLock()

    def _find_proc(self, u_pid, round, ignore_round=False):
        id = make_id(u_pid, round)
        process = self._process_managers_map.lookup_id(id, ignore_round=ignore_round)
        return process

    @eeagent_lock
    def launch_process(self, u_pid, round, run_type, parameters):
        if run_type != self.CFG.eeagent.launch_type.name:
            raise EEAgentParameterException("Unknown run type %s" % (run_type))

        # check to see if download_code enabled
        if (parameters.get('module_uri') and
           (not self.CFG.eeagent.get('code_download') or
           not self.CFG.eeagent.code_download.get('enabled', False))):
            msg = "Code download not enabled in this eeagent"
            raise EEAgentUnauthorizedException(msg)

        # check if url in whitelist
        if parameters.get('module_uri'):
            uri = parameters.get('module_uri')
            if not self._check_whitelist(uri):
                msg = "%s not in code_download whitelist: '%s'" % (uri,
                        ", ".join(self._get_whitelist()))
                raise EEAgentUnauthorizedException(msg)

        # Check to see whether this process already exists
        process = self._find_proc(u_pid, round, ignore_round=True)
        if process is not None and not process.restartable:
            existing_upid, existing_round = unmake_id(process._name)
            state = str(process.get_state())

            if int(round) > int(existing_round):
                msg = ("VERY BAD THING: A launch request for '%s' has been "
                   "recieved, but it is in state %s. The existing process has "
                   "round %s, but the PD is asking to start process with round "
                   "%s. Restarting the process anyway, but something is wrong."
                   % (u_pid, state, existing_round, round))
                self._log.error(msg)
            else:
                msg = ("BAD THING: A launch request for '%s' has been "
                   "recieved, but it is in state %s. The existing process has "
                   "round %s, and the PD is asking to start process with round "
                   "%s. Maybe a message never arrived? Restarting now."
                   % (u_pid, state, existing_round, round))
                self._log.warning(msg)

            try:
                process.terminate()
            except PIDanticStateException, pse:
                self._log.warning("Attempt to terminate a process in the state %s" % (str(process.get_state())))<commit_msg>Do not restart processes when a duplicate launch request is recieved<commit_after>import threading
import logging

from fnmatch import fnmatch
from urlparse import urlparse

from eeagent.util import make_id, unmake_id
from eeagent.execute import PidWrapper
from eeagent.eeagent_exceptions import EEAgentParameterException, EEAgentUnauthorizedException
from pidantic.pidantic_exceptions import PIDanticStateException

RESTARTABLE_STATES = []


def eeagent_lock(func):
    def call(self, *args, **kwargs):
        with self._lock:
            return func(self, *args, **kwargs)
    return call


class EEAgentCore(object):

    ee_name = None

    def __init__(self, CFG, process_managers_map, log):
        self.CFG = CFG
        self._process_managers_map = process_managers_map
        self.ee_name = CFG.eeagent.name

        self._log = log
        self._lock = threading.RLock()

    def _find_proc(self, u_pid, round, ignore_round=False):
        id = make_id(u_pid, round)
        process = self._process_managers_map.lookup_id(id, ignore_round=ignore_round)
        return process

    @eeagent_lock
    def launch_process(self, u_pid, round, run_type, parameters):
        if run_type != self.CFG.eeagent.launch_type.name:
            raise EEAgentParameterException("Unknown run type %s" % (run_type))

        # check to see if download_code enabled
        if (parameters.get('module_uri') and
           (not self.CFG.eeagent.get('code_download') or
           not self.CFG.eeagent.code_download.get('enabled', False))):
            msg = "Code download not enabled in this eeagent"
            raise EEAgentUnauthorizedException(msg)

        # check if url in whitelist
        if parameters.get('module_uri'):
            uri = parameters.get('module_uri')
            if not self._check_whitelist(uri):
                msg = "%s not in code_download whitelist: '%s'" % (uri,
                        ", ".join(self._get_whitelist()))
                raise EEAgentUnauthorizedException(msg)

        # Check to see whether this process already exists
        process = self._find_proc(u_pid, round, ignore_round=True)
        if process is not None and not process.restartable:
            existing_upid, existing_round = unmake_id(process._name)
            state = str(process.get_state())

            if int(round) > int(existing_round):
                msg = ("VERY BAD THING: A launch request for '%s' has been "
                   "recieved, but it is in state %s. The existing process has "
                   "round %s, but the PD is asking to start process with round "
                   "%s. Restarting the process anyway, but something is wrong."
                   % (u_pid, state, existing_round, round))
                self._log.error(msg)
            else:
                msg = ("BAD THING: A launch request for '%s' has been "
                   "recieved, but it is in state %s. The existing process has "
                   "round %s, and the PD is asking to start process with round "
                   "%s. Maybe a message never arrived? Leaving process as-is."
                   % (u_pid, state, existing_round, round))
                self._log.warning(msg)
                return

            try:
                process.terminate()
            except PIDanticStateException, pse:
                self._log.warning("Attempt to terminate a process in the state %s" % (str(process.get_state())))
{'fraction_non_alphanumeric': 0.08469583149211961, 'fraction_numerical': 0.0, 'mean_word_length': 2.2993197278911564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '755144', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1853, 'n_words': 569}