scripts/gestion/trigger/transcriptor.py
Pierre-Elliott Bécue 9cac0c2531 [trigger] On commence un début de gestionnaire d'événements.
* À terme, il sera ajouté aux bindings, dans l'objectif de gérer les
 envois de modifications sans que ceux-ci n'aient à implémenter la
 moindre autre chose qu'un producer standard qui balance des diff
 d'objets.
2014-06-11 23:33:44 +02:00

80 lines
2.6 KiB
Python

#!/bin/bash /usr/scripts/python.sh
# -*- coding: utf-8 -*-
#
# Author : Pierre-Elliott Bécue <becue@crans.org>
# License : GPLv3
# Date : 29/04/2014
import argparse
import cranslib.clogger as clogger
import cmb
import cPickle
import socket
import gestion.config.trigger as trigger_config
import gestion.affichage as affichage
import sys
logger = clogger.CLogger("transcriptor", "info")
def process_event(body):
"""
Makes the transcription between the ldap modlist generated
by lc_ldap and sent to the event queue and the message one
have to send to the services.
Example :
{}
"""
class EvenementListener(cmb.AsynchronousConsumer):
"""
Gestionnaire d'événement
"""
def on_message(self, channel, basic_deliver, properties, body):
"""Invoked by pika when a message is delivered from RabbitMQ. The
channel is passed for your convenience. The basic_deliver object that
is passed in carries the exchange, routing key, delivery tag and
a redelivered flag for the message. The properties passed in is an
instance of BasicProperties with the message properties and the body
is the message that was sent.
:param pika.channel.Channel channel: The channel object
:param pika.Spec.Basic.Deliver: basic_deliver method
:param pika.Spec.BasicProperties: properties
:param str|unicode body: The message body
"""
origin = properties.app_id
message_id = properties.message_id
logger.info('Received message # %s from %s: %s',
basic_deliver.delivery_tag, properties.app_id, body)
body = cPickle.loads(body)
process_event(body)
self.acknowledge_message(basic_deliver.delivery_tag)
def run(self):
"""Run the example consumer by connecting to RabbitMQ and then
starting the IOLoop to block and allow the SelectConnection to operate.
"""
logger.info("""Crans Message Broker
+--------------------------------------------+
| Welcome on Transcriptor |
+--------------------------------------------+""")
self._connection = self.connect()
self.add_queue("trigger-event", "trigger.event")
self._connection.ioloop.start()
def daemonize():
listener = EvenementListener(trigger_config.master, "trigger", "topic")
try:
listener.run()
except KeyboardInterrupt:
logger.warning("Caught SIGINT, will now go for shutdown.")
listener.stop()
if __name__ == '__main__':
# Daemonize the trigger app, in order to listen and execute commands from civet.
daemonize()