Writing Extensions

Structure

Extensions should subclass nameko.extensions.Extension. This base class provides the basic structure for an extension, in particular the following methods which can be overridden to add functionality:

Extension.setup()

Called on bound Extensions before the container starts.

Extensions should do any required initialisation here.

Extension.start()

Called on bound Extensions when the container has successfully started.

This is only called after all other Extensions have successfully returned from Extension.setup(). If the Extension reacts to external events, it should now start acting upon them.

Extension.stop()

Called when the service container begins to shut down.

Extensions should do any graceful shutdown here.

Dependency Providers

It’s likely that even a modest Nameko application will need to define its own dependencies – maybe to interface with a database for which there is no community extension or to communicate with a specific web service.

Dependency providers should subclass nameko.extensions.DependencyProvider and implement a get_dependency() method that returns an object to be injected into service workers.

Dependency providers may also hook into the worker lifecycle. The following three methods are called on all dependency providers for every worker:

DependencyProvider.worker_setup(worker_ctx)

Called before a service worker executes a task.

Dependencies should do any pre-processing here, raising exceptions in the event of failure.

Example: ...

Parameters:
worker_ctx : WorkerContext

See nameko.containers.ServiceContainer.spawn_worker

DependencyProvider.worker_result(worker_ctx, result=None, exc_info=None)

Called with the result of a service worker execution.

Dependencies that need to process the result should do it here. This method is called for all Dependency instances on completion of any worker.

Example: a database session dependency may flush the transaction

Parameters:
worker_ctx : WorkerContext

See nameko.containers.ServiceContainer.spawn_worker

DependencyProvider.worker_teardown(worker_ctx)

Called after a service worker has executed a task.

Dependencies should do any post-processing here, raising exceptions in the event of failure.

Example: a database session dependency may commit the session

Parameters:
worker_ctx : WorkerContext

See nameko.containers.ServiceContainer.spawn_worker