Package: baguette-messaging
Tiny Framework to build micro services.
Currently only supportamqp,rpc over amqpandhttp streambased micro services.How it worksAMQPWe declare a publisher:the method decorated takes one parameterpublish(farine.amqp.publisher.Publisher)importfarine.amqpclassPublish(object):@farine.amqp.publish(exchange='consume',routing_key='routing_key')defpublish_dummy(self,publish):"""
        :param publish: Send the data through AMQP.
        :type publish: farine.amqp.publisher.Publisher
        """publish({'result':0})And then a consumer :the method decorated takes two parametersbody(dict) andmessage(kombu.message.Message).importfarine.amqpclassConsume(object):@farine.amqp.consume(exchange='publish',routing_key='routing_key')defconsume_dummy(self,body,message):"""
        :param body: The message's data.
        :type body: dict
        :param message: Message class.
        :type message: kombu.message.Message
        """message.ack()RPC over AMQPWe need declare two services :the server : Wait for a call(consumer), and answer(publisher).The method decorated just takesargsandkwargsimportfarine.rpcclassServer(object):@farine.rpc.method()defdummy(self,*args,**kwargs):returnTrueAnd the client : Send a call(publisher), and wait for an answer(consumer).the method decorated takes one argumentrpc(farine.rpc.client.Client).The result will be a dictionnary.importfarine.rpcclassClient(object):@farine.rpc.client('myotherservice')defdummy(self,rpc):"""
        :param rpc: The RPC client.
        :type rpc: farine.rpc.client.Client
        """result=rpc.dummy()RPC StreamWe can also do streaming RPC call.All you need to do is to add__stream__ = True*to your RPC call.Also, a generator is returned.Example:importfarine.rpcclassServer(object):@farine.rpc.method()defdummy(self,*args,**kwargs):yield'a'yield'b'importfarine.rpcclassClient(object):@farine.rpc.client('myotherservice')defdummy(self,rpc):"""
        :param rpc: The RPC client.
        :type rpc: farine.rpc.client.Client
        """forresultinrpc.dummy(__stream__=True):printresultHTTP StreamWe can declare a service that will listen to an HTTP SSE event :the method decorated takes one argumentdata(dict).In the configuration file, we just need to setup the endpoint to listen.importfarine.streamclassClient(object):@farine.stream.http()deflisten_event(self,data):"""
        :param data: The event sent.
        :type data: dict
        """returnTrueExecute methodIf all we need is to execute a method, we can usefarine.execute.method.It will restart it, if it ends (customizable).importfarine.executeclassClient(object):@farine.execute.method()defmy_own_stuff(self):"""
    Your own code.
    """Databasebaguette-messagingis using peewee to manage databases connections (only postgresql is supported for the moment)In order for it to detect that you are using a database, you need to create amodels.pymodule.Then, each time we enter into a method (amqp,rpc,stream) a database connection will be created and closed.You can use the database connection usingself.db, to manage transactions for example.Examplemodels.py:fromfarine.connectors.sqlimport*classUser(Model):name=CharField()service.py:importfarine.amqpfrommodelsimportUserclassClient(object):@farine.amqp.consume(exchange='exchange',routing_key='routing_key')defselect(self,body,message):returnUser.select().where(User.id==1)OverviewYou can mix in a service, everything:it can be a consumer of an HTTP stream, and send back the result in RPC, etc.Exampleimportfarine.rpcimportfarine.streamclassClient(object):@farine.stream.http()defget(self,data):returnself.send(data)@farine.rpc.client('myotherservice')defsend(self,rpc,data):returnrpc.process(data)ConfigurationBy default the configuration file is located in/etc/farine.ini.
You can override this path using the environment variableFARINE_INI.It must contains one section by service (using thelowercase class name).aDEFAULTsection can also be present.Example[DEFAULT]
amqp_uri = amqp://baguette:baguette@127.0.0.1:5672/baguette

[consume]
enabled = trueDatabaseIf you use a database connection, you have to add in the[DEFAULT]section the db parameters:[DEFAULT]
db_connector = postgres (required)
db_name = name (required)
db_user = user (required)
db_host = host (required)
db_password = password (required)
db_port = port (optional)
db_max_conn = max_connections (optional)
db_stale_timeout = stale timeout (optional)
db_timeout = timeout (optional)LaunchTo launch a service, just run:farine--start=mymoduleIt will try to importmymodule.serviceand launch it.