Brathuhn is now a daemon

master
Stefan Krulj 14 years ago
parent 9d8f7c2ba1
commit da54a52bca
  1. 28
      log_rotate.py
  2. 15
      main.py
  3. 25
      sys/brathuhn.py
  4. 73
      sys/demonize.py

@ -1,28 +0,0 @@
#!/usr/bin/python
import os,sys
import time,datetime
LOG_FILE = "./brathuhn.log"
ROT_SIZE = 2**20
ROT_CRITICAL = 2**20+50*2**10
f = open(LOG_FILE,'wb')
cnt = 0
c = sys.stdin.read(1)
while c!='':
f.write(c)
cnt+=1
if c=='\n' and cnt>=ROT_SIZE or cnt>=ROT_CRITICAL:
f.close()
os.rename(LOG_FILE, LOG_FILE+date.strftime("_%Y-%m-%d_%H-%M-%S") )
f = open(LOG_FILE,'wb')
date=datetime.datetime.fromtimestamp(time.time())
print "rotate",cnt,date.strftime("%Y-%m-%d %H:%M:%S")
cnt = 0
c = sys.stdin.read(1)

@ -2,16 +2,19 @@
# -*- coding: utf-8 -*-
import sys, os
cwd = os.getcwd()
sys.path.append(os.path.join(cwd,"sys"))
sys.path.append(os.path.join(cwd,"modules","active"))
import brathuhn
import demonize, brathuhn
#Demonize
if not demonize.demonize('./pid',False,'log.txt'):
sys.stderr.write("Daemon already running")
sys.exit(1)
#Inport modules
modules = []
files = os.listdir( os.path.join(cwd,"modules","active") )
for f in files:
if f.endswith(".py"):
mod = f[:-3]
@ -19,10 +22,14 @@ for f in files:
mod = f[:-4]
if not mod in modules:
modules.append(mod)
for mod in modules:
__import__(mod)
print "PID:", os.getpid()
#Start Brathuhn
brathuhn.connect_and_join()
brathuhn.loop()

@ -1,5 +1,5 @@
import xmpp, os, sys, time, traceback, stream_ft
import threading
import threading, errno, select
from config import *
@ -281,16 +281,21 @@ def loop():
except KeyboardInterrupt:
print "keyboard interrupt, quitting..."
sys.exit(0)
except select.error as e:
if not (e.args and e.args[0] == errno.EINTR):
print_error()
except:
print "-------ERROR OCCURED-------"
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exc(file=sys.stdout)
if not cl.connected:
s=MIN_CONNECT_INTERVAL-(time.time()-last_connect)
if s>0:
time.sleep(s)
connect_and_join()
print "---------------------------"
print_error()
def print_error():
print "-------ERROR OCCURED-------"
traceback.print_exc(file=sys.stdout)
if not cl.connected:
s=MIN_CONNECT_INTERVAL-(time.time()-last_connect)
if s>0:
time.sleep(s)
connect_and_join()
print "---------------------------"
def callAt(func,_time,*args):
"""Call a function at given time; The call is not very exact but it is

@ -0,0 +1,73 @@
import os, sys, signal
log_file = None
log_path = None
def demonize(pid_file, force=False, logfile=None):
global log_file
global log_path
# check if pid file exists and return if not a forced start
if os.path.exists(pid_file) and not force:
return False
## Demonize
if os.fork():
exit(0)
os.umask(0)
os.setsid()
if os.fork():
exit(0)
pid = open(pid_file,'w')
pid.write(str(os.getpid()))
pid.close()
# The fileno is accessable after the first write
sys.stdout.write('\0')
sys.stderr.write('\0')
sys.stdout.flush()
sys.stderr.flush()
ignore = open('/dev/null', 'r+')
os.dup2(ignore.fileno(), sys.stdin.fileno())
if type(logfile)==file:
log_file = logfile
elif logfile!=None:
log_file = open(logfile,'w')
log_path = logfile
else:
log_file = ignore
os.dup2(log_file.fileno(), sys.stdout.fileno())
os.dup2(log_file.fileno(), sys.stderr.fileno())
signal.signal(signal.SIGHUP, __sys_call(__sighup_handler) )
return True
# decorator to make system call methods safe from EINTR
def __sys_call(func):
def __func(*args, **kwargs):
try:
rv = func(*args, **kwargs)
except exception as e:
if e.args and e.args[0] == EINTR:
pass
return rv
return __func
def __sighup_handler(sig,dontcare):
global log_path
global log_file
import traceback
try:
if log_path != None:
log_file.seek(0)
log_file.close()
log_file = open(log_path,'w')
os.dup2(log_file.fileno(), sys.stdout.fileno())
os.dup2(log_file.fileno(), sys.stderr.fileno())
except:
traceback.print_exc(file=sys.stdout)
sys.exit(1)
Loading…
Cancel
Save