Search code examples
pythonsqlalchemymicroservicesnameko

Python, Nameko, nameko-sqlalchemy : Error : db_uris = self.container.config[DB_URIS_KEY] KeyError: 'DB_URIS'


Hi everyone,

First of all, I want to let you know that I am just beginning to learn about the python micro service library Nameko.

I am trying to create a simple micro service that add a user in Sqlite database using nameko-sqlalchemy extention. When I try to run the micro service using the Nameko command : nameko run user_service, I get the error :

Connected to amqp://guest:**@127.0.0.1:5672//
starting services: userService
Traceback (most recent call last):
  File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 461, in fire_timers
    timer()
  File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 59, in __call__
    cb(*args, **kw)
  File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/eventlet/greenthread.py", line 221, in main
    result = function(*args, **kwargs)
  File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/nameko/utils/concurrency/__init__.py", line 76, in call
    return getattr(item, name)(*args, **kwargs)
  File "/home/yek/Documents/my_development/micro_service_project/ms_env/lib/python3.6/site-packages/nameko_sqlalchemy/database_session.py", line 24, in setup
    db_uris = self.container.config[DB_URIS_KEY]
KeyError: 'DB_URIS'

I use the code bellow:

user_service.py

from nameko_sqlalchemy import DatabaseSession
from nameko.rpc import rpc
from nameko.containers import ServiceContainer

from model import DeclarativeBase
from model import User

class UserService(object):
    name = 'userService'

    db = DatabaseSession(DeclarativeBase)

    @rpc
    def write_to_db(self, user_id, first_name, last_name):
        user = User(user_id, first_name, last_name)

        with self.db.get_session() as session:
            session.add(user)
            session.commit()


CONFIG = {
            "AMQP_URI":"amqp://guest:guest@localhost",
            "DB_URIS":
                      {"userService:Base":"sqlite:///data.db"}
         }

container = ServiceContainer(UserService,config=CONFIG)

service_extention = list(container.extensions)

container.start()

container.stop()

with :

model.py

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime


DeclarativeBase = declarative_base()

class User(DeclarativeBase):
    __tablename__ = "users"

    user_id = Column(Integer,primary_key=True)
    first_name = Column(String)
    last_name = Column(String)

    def __init__(self, user_id, first_name, last_name):
        self.user_id = user_id
        self.first_name = first_name
        self.last_name = last_name 


if __name__ == "__main__":
    from sqlalchemy import create_engine

    engine = create_engine('sqlite:///data.db', echo = True)
    DeclarativeBase.metadata.create_all(engine)

Environment: nameko 2.12.0 / nameko-sqlalchemy 1.5.0 / Ubuntu 18.04.4 LTS / Python 3.6.9 / Virtualenv

Thank you in advance for the help.

Regards, Younes


Solution

  • After learning the official documentation, I have found out what is going wrong with my approach of running the code.

    The solutions are :

    1. To use directly the Nameko CLI as I did bellow but with adding config file. The new command look like that : nameko run user_service --config ./config.yaml ./config.yaml
    AMQP_URI: 'amqp://guest:guest@localhost'
    DB_URIS:
        'userService:Base': 'sqlite:///data.db'
    
    1. To run directly the user_service.py file after adding a Service Container or a Runner with the configuration passed as dict. The solution needs to run the python file for every use. I think that I miss some thing with this second way.

    I hope It will help.