If you havent read it, you should read about plugins first
::: tip You must use dynaconf merge when changing these parameters.. Example
myenv:
OPTIONAL_COMPONENTS:
dynaconf_merge: true
WALRUS:
LOAD: "auto"
DRIVER: "redis-walrus"
OPTS:
URL: "redis://redis":::
Take a look at the default configuration here or see the file below (it is under the OPTIONAL_COMPONENTS key) for some examples.
::: details default-settings.yaml <<< @/opa-stack/api/data/opa/default-settings.yaml :::
When you want to use an optional-component you the best way is to use opa.get_instance(name) like this
from opa import get_instance, get_router
from opa.plugins.driver_redis import Walrus
router = get_router()
@router.get("/")
def counter_sync():
walrus = get_intance('walrus')
counter = walrus.incr('counter')
return f'Counter is {counter}'::: warning
opa.get_instance() takes the lowercase component key-name as input
:::
The other ways to get the instance (might be usefull in some cases)
- Use
component = opa.get_component(name)- The instance will be under
component.instance, but there are other component-info you might want undercomponent
- The instance will be under
- Use
pm = opa.get_plugin_manager(), thenpm.optional_components[name]to get the component instance
| Backend | Library | Drivername | Async |
|---|---|---|---|
| Redis | Aioredis | redis-aioredis | yes |
| Redis | Walrus | redis-walrus | no |
| Mongodb | Motor | mongodb-async-motor | yes |
| Celery | Celery | celery | no |
- Very powerful redis library with support for a lot of additional features
If you need basic background tasks, look into FastAPI/Starlette BackgroundTasks instead.
There is builtin support for running celery tasks, see the sample-project for details, view info about the sample project here
The opa-stack's api-container can run in worker mode, ie, it will run a celery worker with the same codebase as the rest of the api. This is by far the simplest setup, but feel free to run your own workers if you need.
- Set environment-variable
MODE=workerto start it as a worker - Set additional celery params (
celery worker -A opa.main ${CELERY_PARAMS})
Some important notes about using celery in opa-stack is:
- The tasks must be defined in
tasks.py, ie, you will need your plugin to be a python package (folder with__init__.py). - You can access optional-components as usual in the tasks, but only non-async drivers are working. Ie, you can't use drivers like
motororaioredisin your tasks.- It is important that you DONT import any of your tasks on top of the file where your route is defined. Ie, do the
from packagename.tasks import sometaskinside your@router.get('/')...function!
- It is important that you DONT import any of your tasks on top of the file where your route is defined. Ie, do the
- The celery worker is using most of the opa-stack code. So configuration, driver-loading, hooks and so on will also work in the celery worker.