Created
March 15, 2016 19:11
-
-
Save Verurteilt/039e4f8015779334bd37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import atexit, os, sys, time | |
from signal import SIGTERM | |
from django.utils.daemonize import become_daemon | |
from color import Color | |
MSG_PID_EXISTE = 'Pidfile %s existe, demonio ejecutandose' | |
MSG_PID_NO_EXISTE = 'Pidfile %s no existe, el demonio no esta ejecutandose' | |
MSJ_STATUS = lambda service_name, status, msg_pid: 'Service \'{service_name}\' is {status}{msg_pid}'.format(service_name = service_name, status=status, msg_pid=msg_pid) | |
CODIGO_NO_SUCH_PROCESS = 3 | |
class Daemon(object): | |
def __init__(self, pidfile, **kwargs): | |
self.pidfile = pidfile | |
self.become_daemon_kwargs = kwargs | |
def daemonize(self): | |
become_daemon(**self.become_daemon_kwargs) | |
atexit.register(self.borrar_pid) | |
pid = str(os.getpid()) | |
file(self.pidfile, 'w+').write("%s\n" % pid) | |
def borrar_pid(self): | |
os.remove(self.pidfile) | |
def status(self): | |
try: | |
pf = file(self.pidfile, 'r') | |
pid = int(pf.read().strip()) | |
pf.close() | |
except IOError: | |
pid = None | |
print Color.colorear(MSJ_STATUS(self.__str__(), | |
pid and 'running' or 'stopped', | |
pid and ' with pid: {pid}.'.format(pid=pid) or '.'), color=pid and Color.OKGREEN or Color.WARNING) | |
sys.exit() | |
def start(self): | |
try: | |
pf = file(self.pidfile, 'r') | |
pid = int(pf.read().strip()) | |
pf.close() | |
except IOError: | |
pid = None | |
if pid: | |
print Color.colorear(MSG_PID_EXISTE % self.pidfile) | |
sys.exit(1) | |
# start the daemon | |
self.daemonize() | |
self.run() | |
def stop(self): | |
try: | |
pf = file(self.pidfile, 'r') | |
pid = int(pf.read().strip()) | |
pf.close() | |
except IOError: | |
pid = None | |
if not pid: | |
print Color.colorear(MSG_PID_NO_EXISTE % self.pidfile, color=Color.WARNING) | |
return # no es error en 'restart' | |
# Matar el demonio | |
try: | |
while 1: | |
os.kill(pid, SIGTERM) | |
time.sleep(0.1) | |
except OSError, err: | |
codigo_error, msg_error = err | |
if codigo_error == CODIGO_NO_SUCH_PROCESS: | |
if os.path.exists(self.pidfile): | |
os.remove(self.pidfile) | |
else: | |
print Color.colorear(msg_error) | |
sys.exit(1) | |
def restart(self): | |
self.stop() | |
self.start() | |
def run(self): | |
"""Sobreescribir este método""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment