<commit_before>import os
import os.path
import logging
import signal
import subprocess
import re
import time

import lockfile
from lockfile import pidlockfile

import taskqueue

OPERATION_SUCCESSFUL = 0
OPERATION_FAILED = 1
INSTANCE_ALREADY_RUNNING = 2
INSTANCE_NOT_RUNNING = 3
SET_USER_FAILED = 4

logger = logging.getLogger('taskqueue.daemon')

def get_pidfile():
    return pidlockfile.PIDLockFile(
        os.path.join(taskqueue.BASE_DIR, 'tqdaemon.pid'))

def status():
    "Check whether daemon is already running. return pid or False"
    plf = get_pidfile()
    pid = plf.read_pid()
    if pid and check_for_process(pid):
        return pid
    return False

def check_for_process(pid):
    if pid:
        try:
            os.kill(pid, 0) #no-op signal, only indicates whether it could be sent
            return True
        except OSError:
            get_pidfile().break_lock()
            return False

def stop():
    "Kill a running daemon instance"

    pid = status()
    if not pid:
        logger.warn("Daemon not running.")
        return INSTANCE_NOT_RUNNING

    # Try killing the daemon process
    try:
        for i in range(0, 30):
            os.kill(pid, signal.SIGTERM)
            time.sleep(0.5)
    except OSError, err:
        err = str(err)
        if err.find("No such process") > 0:
            get_pidfile().break_lock()
        else:
            logger.exception("Failed to stop daemon.")
            return OPERATION_FAILED
    return OPERATION_SUCCESSFUL

def restart():
    "Restart a daemon process"
    if status():
        kill_status = stop()
        if kill_status == OPERATION_FAILED:
            return kill_status
    return daemonize()

def kill(sig=signal.SIGKILL):
    uid = os.getuid()
    proc = subprocess.Popen(['ps', 'x', '-U', str(uid), '-o', 'pid,command'], stdout=subprocess.PIPE)
    lines = proc.stdout.readlines()[1:]
    logger.debug("Searching %d processes for this user.", len(lines))
    for l in lines:
        l = l.strip()
        m = re.match('(\\d+) (npact-.*)', l)
        if m:
            pid,name = m.groups()
            logger.warning("Killing proc %s %r", pid, name)
            os.kill(int(pid), sig)



def daemonize():
    "Start a daemonized taskqueue"
    import daemon

    ##Before daemonizing figure out which handlers we want to keep.
    ##We only want to keep handlers for this library that aren't going
    ##to stderr or stdout
    other_handlers = set(logging._handlerList)
    fds = []
    l = logger
    while l:
        for h in l.handlers:
            if hasattr(h, 'stream') and \
              hasattr(h.stream, 'fileno') and \
              h.stream.fileno() not in [1,2]:
                fds.append(h.stream.fileno())
                other_handlers.discard(h)
        l = l.propagate and l.parent<commit_msg>Handle exceptions trying to kill the tqdaemon processes.<commit_after>import os
import os.path
import logging
import signal
import subprocess
import re
import time

import lockfile
from lockfile import pidlockfile

import taskqueue

OPERATION_SUCCESSFUL = 0
OPERATION_FAILED = 1
INSTANCE_ALREADY_RUNNING = 2
INSTANCE_NOT_RUNNING = 3
SET_USER_FAILED = 4

logger = logging.getLogger('taskqueue.daemon')

def get_pidfile():
    return pidlockfile.PIDLockFile(
        os.path.join(taskqueue.BASE_DIR, 'tqdaemon.pid'))

def status():
    "Check whether daemon is already running. return pid or False"
    plf = get_pidfile()
    pid = plf.read_pid()
    if pid and check_for_process(pid):
        return pid
    return False

def check_for_process(pid):
    if pid:
        try:
            os.kill(pid, 0) #no-op signal, only indicates whether it could be sent
            return True
        except OSError:
            get_pidfile().break_lock()
            return False

def stop():
    "Kill a running daemon instance"

    pid = status()
    if not pid:
        logger.warn("Daemon not running.")
        return INSTANCE_NOT_RUNNING

    # Try killing the daemon process
    try:
        for i in range(0, 30):
            os.kill(pid, signal.SIGTERM)
            time.sleep(0.5)
    except OSError, err:
        err = str(err)
        if err.find("No such process") > 0:
            get_pidfile().break_lock()
        else:
            logger.exception("Failed to stop daemon.")
            return OPERATION_FAILED
    return OPERATION_SUCCESSFUL

def restart():
    "Restart a daemon process"
    if status():
        kill_status = stop()
        if kill_status == OPERATION_FAILED:
            return kill_status
    return daemonize()

def kill(sig=signal.SIGKILL):
    uid = os.getuid()
    proc = subprocess.Popen(['ps', 'x', '-U', str(uid), '-o', 'pid,command'], stdout=subprocess.PIPE)
    lines = proc.stdout.readlines()[1:]
    logger.debug("Searching %d processes for this user.", len(lines))
    for l in lines:
        l = l.strip()
        m = re.match('(\\d+) (npact-.*)', l)
        if m:
            pid,name = m.groups()
            logger.warning("Killing proc %s %r", pid, name)
            try:
                os.kill(int(pid), sig)
            except:
                logger.exception("Error killing %s %r", pid, name)



def daemonize():
    "Start a daemonized taskqueue"
    import daemon

    ##Before daemonizing figure out which handlers we want to keep.
    ##We only want to keep handlers for this library that aren't going
    ##to stderr or stdout
    other_handlers = set(logging._handlerList)
    fds = []
    l = logger
    while l:
        for h in l.handlers:
            if hasattr(h, 'stream') and \
              hasattr(h.stream, 'fileno') and \
              h.stream.fileno() not in [1,2]:
                fds.append(h.stream.fileno())
                other_handlers.discard(h)
        l = l.propagate and l.parent
{'fraction_non_alphanumeric': 0.10331079909863061, 'fraction_numerical': 0.005200208008320333, 'mean_word_length': 2.289623717217788, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4988565', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1791, 'n_words': 534}