diff --git a/README.md b/README.md index 4542982..87b6e3d 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,14 @@ The following scripts run in the foreground and should be deamonized celery -A ogn.collect worker -l info ``` +To load a custom configuration, create a file `myconfig.py` (see [config/default.py](config/default.py)) +and set the environment variable `OGN_CONFIG_MODULE` accordingly. + +``` +export OGN_CONFIG_MODULE="myconfig.py" +./manage.py gateway.run +``` + ### manage.py - CLI options ``` usage: manage.py [.] [] diff --git a/config/default.py b/config/default.py new file mode 100644 index 0000000..d9ce11a --- /dev/null +++ b/config/default.py @@ -0,0 +1,4 @@ +SQLALCHEMY_DATABASE_URI = 'sqlite:///beacons.db' + +BROKER_URL = 'redis://localhost:6379/0' +CELERY_RESULT_BACKEND = 'redis://localhost:6379/0' diff --git a/ogn/collect/celery.py b/ogn/collect/celery.py index 3fbb2b9..816e90b 100644 --- a/ogn/collect/celery.py +++ b/ogn/collect/celery.py @@ -1,21 +1,19 @@ +import os +import importlib from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from celery import Celery from celery.signals import worker_init, worker_shutdown -app = Celery('ogn.collect', - broker='redis://localhost:6379/0', - backend='redis://localhost:6379/0', - include=["ogn.collect.database", "ogn.collect.logbook"]) - -DB_URI = 'sqlite:///beacons.db' +os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default') +config = importlib.import_module(os.environ['OGN_CONFIG_MODULE']) @worker_init.connect def connect_db(signal, sender): # Load settings like DB_URI... - engine = create_engine(DB_URI, echo=False) + engine = create_engine(config.SQLALCHEMY_DATABASE_URI, echo=False) Session = sessionmaker(bind=engine) sender.app.session = Session() @@ -26,5 +24,8 @@ def close_db(signal, sender): sender.app.session.close() -if __name__ == '__main__': - app.start() +app = Celery('ogn.collect', + include=["ogn.collect.database", + "ogn.collect.logbook"]) + +app.config_from_envvar("OGN_CONFIG_MODULE") diff --git a/ogn/commands/dbutils.py b/ogn/commands/dbutils.py index 07ca652..be2d294 100644 --- a/ogn/commands/dbutils.py +++ b/ogn/commands/dbutils.py @@ -1,6 +1,13 @@ +import os +import importlib from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -engine = create_engine('sqlite:///beacons.db', echo=False) + + +os.environ.setdefault('OGN_CONFIG_MODULE', 'config.default') + +config = importlib.import_module(os.environ['OGN_CONFIG_MODULE']) +engine = create_engine(config.SQLALCHEMY_DATABASE_URI, echo=False) Session = sessionmaker(bind=engine) session = Session()