예제 #1
0
def main():
    parser = get_parser()
    opts, args = parser.parse_args()
    load_config_or_exit(opts.configfile)
    if opts.check and opts.background:
        parser.error(
            '--check --background makes no sense, how will you know the result?'
        )
    if not opts.background:
        log_to_stream(sys.stderr,
                      level=logging.DEBUG if opts.debug else logging.WARNING)
        return doit(opts)
    else:
        pidlockfile = PIDLockFile(PIDFILE)
        existing_pid = pidlockfile.read_pid()
        if existing_pid:
            if process_is_alive(existing_pid):
                sys.stderr.write(
                    'Another beaker-init process is running (pid %s)\n' %
                    existing_pid)
                return 1
            else:
                sys.stderr.write('Pid file %s exists but pid %s is dead, '
                                 'removing the pid file\n' %
                                 (PIDFILE, existing_pid))
                pidlockfile.break_lock()
        with daemon.DaemonContext(pidfile=pidlockfile, detach_process=True):
            log_to_syslog('beaker-init')
            return doit(opts)
예제 #2
0
def start(daemon=True, **kwargs):
    lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid"))
    if lock.is_locked():
        try:
            Process(lock.read_pid())
            print("Rammon is already running")
            sys.exit(1)
        except NoSuchProcess:
            print(
                "Rammon was stopped, however it was stopped uncleanly leaving behind a pidfile.\n"
                "Breaking pidfile lock...")
            lock.break_lock()
    if daemon:
        logger.info("Starting rammon as a daemon...")
    else:
        logger.info("Starting rammon in the foreground...")

    if daemon:
        if try_systemd_start():
            logger.info("Starting with systemd...")
            sys.exit(0)
        else:
            mon = RamMonitor()
            set_handlers(mon)
            logger.info("Starting without systemd...")
            with DaemonContext(umask=0o077,
                               pidfile=lock,
                               detach_process=daemon):
                mon.start()
    else:
        with lock:
            mon = RamMonitor()
            set_handlers(mon)
            mon.start()
예제 #3
0
def status(**kwargs):
    lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid"))
    if lock.is_locked():
        try:
            Process(lock.read_pid())
            print("Rammon is running" +
                  (", and auto-start is enabled" if is_enabled() else ""))
        except NoSuchProcess:
            print(
                "Rammon is stopped, however it was stopped uncleanly leaving behind a pidfile.\n"
                "Breaking pidfile lock...")
            lock.break_lock()
    else:
        print("Rammon is stopped" +
              (", but auto-start is enabled" if is_enabled() else ""))
예제 #4
0
def stop(**kwargs):
    lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid"))
    if try_systemd_stop():
        logger.info("Stopping rammon daemon with systemd...")
    if lock.is_locked():
        try:
            proc = Process(lock.read_pid())
            proc.terminate()
            try:
                proc.wait(1)
            except TimeoutExpired:
                print("Rammon did not stop gracefully, killing it...")
                proc.kill()
                lock.break_lock()
            logger.info("Rammon stopped successfully")
        except NoSuchProcess:
            logger.warning(
                "Rammon was already stopped, but had not stopped cleanly.\n"
                "Breaking pidfile lock...")
            lock.break_lock()
    else:
        logger.error("Rammon is already stopped")
        sys.exit(1)
예제 #5
0
def main():
    parser = get_parser()
    opts, args = parser.parse_args()
    load_config_or_exit(opts.configfile)
    if opts.check and opts.background:
        parser.error('--check --background makes no sense, how will you know the result?')
    if not opts.background:
        log_to_stream(sys.stderr, level=logging.DEBUG if opts.debug else logging.WARNING)
        return doit(opts)
    else:
        pidlockfile = PIDLockFile(PIDFILE)
        existing_pid = pidlockfile.read_pid()
        if existing_pid:
            if process_is_alive(existing_pid):
                sys.stderr.write('Another beaker-init process is running (pid %s)\n'
                        % existing_pid)
                return 1
            else:
                sys.stderr.write('Pid file %s exists but pid %s is dead, '
                        'removing the pid file\n' % (PIDFILE, existing_pid))
                pidlockfile.break_lock()
        with daemon.DaemonContext(pidfile=pidlockfile, detach_process=True):
            log_to_syslog('beaker-init')
            return doit(opts)
예제 #6
0
파일: pet.py 프로젝트: Gateswong/GatesPet
from db import MongoDataAdapter
from time import sleep
from daemon.pidfile import PIDLockFile
import psutil

def create_task_manager(db, max_tasks, scan_interval):
    # TaskManager instance.
    tm = TaskManager(mongo, max_tasks=max_tasks, scan_interval=scan_interval)
    return tm

lock = PIDLockFile('pet.pid')

if lock.is_locked() and not lock.i_am_locking():
    if not psutil.pid_exists(lock.read_pid()):
        print 'The pidlock is hold by a void process. Breaking it.'
        lock.break_lock()
    else:
        print 'Another process is holding pidlock. Exit.'
        exit()

print 'Acquiring pidlock.'
lock.acquire(timeout=5)

# TODO Load config.
mongo = MongoDataAdapter.create_adapter('GatesPet')
max_tasks = 50
scan_interval = 10
process_interval = 120

tm = create_task_manager(mongo, max_tasks, scan_interval)