class TransactionMain: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() self.transactionLirePika = PikaDAO(self.configuration) self.callbackImpl = CallbackNouvelleTransaction() def connecter(self): self.connexionMq = self.transactionLirePika.connecter() self.transactionLirePika.demarrer_lecture_nouvelles_transactions( self.callbackImpl.callbackAvecAck) def deconnecter(self): self.transactionLirePika.deconnecter() # Methode principale de traitement def run(self): print("Demarrage du traitement des transactions MQ -> MongoDB") print("MQ Host: %s, MQ Queue: %s" % (self.configuration.mq_host, self.configuration.queue_nouvelles_transactions)) self.connecter() print("Fin execution transactions MQ -> MongoDB")
class NotificationActionExempleTest: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() self.message_dao = PikaDAO(self.configuration) self.message_dao.connecter() self.generateur_transaction = GenerateurTransaction(self.configuration, self.message_dao) def deconnecter(self): self.message_dao.deconnecter() def test1(self): # temps_lecture_ajuste = temps_lecture + datetime.timedelta(hours=4) domaine = TachesConstantes.TRANSACTION_ACTION_TACHE transaction_message = dict({ TachesConstantes.LIBELLE_ID_NOTIFICATION: '5c152457e094095d8c8d314e', TachesConstantes.LIBELLE_ACTION: TachesConstantes.ACTION_VUE, TachesConstantes.LIBELLE_DATE_ATTENTE_ACTION: 180 }) self.generateur_transaction.soumettre_transaction(transaction_message, domaine) print("Sent notification domaine %s: %s" % (domaine, transaction_message))
class NotificationExempleTest: def __init__(self): self.configuration = TransactionConfiguration() self.configuration.loadEnvironment() self.message_dao = PikaDAO(self.configuration) self.message_dao.connecter() def deconnecter(self): self.message_dao.deconnecter() def test1(self): # temps_lecture_ajuste = temps_lecture + datetime.timedelta(hours=4) notification = dict({ '_mg-libelle': 'regle_simple', 'evenements': Constantes.EVENEMENT_NOTIFICATION, 'source': { '_collection': 'mgdomaines_appareils_SenseursPassifs', '_id': "5bef31be82cc2cb5ab0d57fe" }, 'regles': [{ "pasbonne_10": { "element": "temperature" } }], "date": int(datetime.datetime.utcnow().timestamp()), 'valeurs': { "element": "temperature", "valeur": 24.6 } }) self.message_dao.transmettre_notification( notification, TachesConstantes.AVERTISSEMENT) print("Sent notification: %s" % notification)
processus = "mgdomaines_appareils_SenseursPassifs:ProcessusTransactionSenseursPassifsLecture" demarreur.demarrer_processus(processus, message_test) return message_test # --- MAIN --- configuration = TransactionConfiguration() configuration.loadEnvironment() print("Connecter Pika") messageDao = PikaDAO(configuration) messageDao.connecter() print("Connection MongDB") documentDao = MongoDAO(configuration) documentDao.connecter() print("Envoyer message") demarreur = MGPProcessusDemarreur(messageDao, documentDao) # TEST enveloppe = message_test() # FIN TEST print("Sent: %s" % enveloppe) messageDao.deconnecter() documentDao.deconnecter()
# Script qui agit comme set-up pour RabbitMQ import time from millegrilles.dao.Configuration import TransactionConfiguration from millegrilles.dao.MessageDAO import PikaDAO configuration = TransactionConfiguration() configuration.loadEnvironment() messagedao = PikaDAO(configuration) messagedao.connecter() messagedao.configurer_rabbitmq() time.sleep(20) messagedao.deconnecter()
class ContexteRessourcesMilleGrilles: """ Classe helper qui permet d'initialiser et de passer les ressources (configuration, DAOs) """ def __init__(self, configuration: TransactionConfiguration = None, message_dao=None, additionals: list = None): """ Init classe. Fournir les ressources deja initialisee ou utiliser methode initialiser(). :param configuration: Optionnel, configuration MilleGrilles deja initialisee. :param message_dao: Optionnel, message_dao deja initialise. :param additionals: Fichiers de config additionels a combiner """ self._configuration = configuration self._message_dao = message_dao self._additionnals = additionals self._email_dao = None self._signateur_transactions = None self._verificateur_certificats = None self._verificateur_transactions = None self._generateur_transactions = None self.validation_workdir_tmp = None self.__logger = logging.getLogger(__name__ + '.' + self.__class__.__name__) def initialiser(self, init_message=True, connecter=True): """ Initialise/reinitialise le contexte et connecte les DAOs. :param init_message: Si True, initialise et connecte PikaDAO :param connecter: Si true, la connexion aux DAOs est ouverte immediatement """ if not self._configuration: self._configuration = TransactionConfiguration() self._configuration.loadEnvironment(additionals=self._additionnals) self._message_dao = None self.validation_workdir_tmp = tempfile.mkdtemp(prefix='millegrilles_pki_', dir=self._configuration.pki_workdir) if init_message: self._message_dao = PikaDAO(self._configuration) self._signateur_transactions = SignateurTransaction(self) self._signateur_transactions.initialiser() # Preparer les certificats, validateurs self._verificateur_transactions = VerificateurTransaction(self) self._verificateur_transactions.initialiser() self._verificateur_certificats = VerificateurCertificats(self) self._verificateur_certificats.initialiser() if connecter: self._message_dao.connecter() def fermer(self): try: shutil.rmtree(self.validation_workdir_tmp) except Exception as e: self.__logger.warning("Erreur suppression workdir pki tmp : %s", str(e)) try: self._message_dao.deconnecter() except: pass @property def configuration(self): return self._configuration @property def message_dao(self) -> PikaDAO: """ Retourne un message_dao. :return: Message dao. :raises: ValueError is le message dao n'a pas ete defini. """ # if self._message_dao is None: # raise ValueError("MessageDAO n'est pas initialise") return self._message_dao @message_dao.setter def message_dao(self, message_dao): self._message_dao = message_dao @property def generateur_transactions(self) -> GenerateurTransaction: if self._generateur_transactions is None: self._generateur_transactions = GenerateurTransaction(self) return self._generateur_transactions @property def signateur_transactions(self) -> SignateurTransaction: return self._signateur_transactions @property def verificateur_transaction(self) -> VerificateurTransaction: return self._verificateur_transactions @property def verificateur_certificats(self) -> VerificateurCertificats: return self._verificateur_certificats @property def idmg(self) -> str: return self._configuration.idmg