[common.py,daemon.py,initscript] Ecriture du daemon crans_ticket

This commit is contained in:
root 2014-09-10 23:29:32 +02:00
parent fb298f5d8d
commit 3a5e722133
3 changed files with 107 additions and 10 deletions

View file

@ -1,18 +1,56 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys,os,pwd,grp
import common
import pika
import json
import dump
import config
conn = pika.BlockingConnection(config.PARAMS)
ch = conn.channel()
ch.queue_declare(queue='CransTicket')
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
dump.print_liste(json.loads(body))
def run():
conn = pika.BlockingConnection(config.PARAMS)
ch = conn.channel()
ch.queue_declare(queue='CransTicket')
def callback(ch, method, properties, body):
print (" [x] Received %r" % (body,))
dump.print_liste(json.loads(body))
ch.basic_consume(callback, queue='CransTicket', no_ack=True)
ch.start_consuming()
conn.close()
ch.basic_consume(callback, queue='CransTicket', no_ack=True)
ch.start_consuming()
conn.close()
# fork en arrière plan + pidfile
if __name__ == "__main__":
# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
print("fork #1 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
sys.exit(1)
# decouple from parent environment
os.chdir("/") #don't prevent unmounting....
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent, print eventual PID before
#print "Daemon PID %d" % pid
open(common.PIDFILE, 'w').write("%d" % pid)
sys.exit(0)
except OSError, e:
print("fork #2 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr)
sys.exit(1)
# start the daemon main loop
run()