Skip to content

Commit

Permalink
Merge pull request #255 from kollhof/nameko-run-config
Browse files Browse the repository at this point in the history
nameko run config
  • Loading branch information
mattbennett committed May 15, 2015
2 parents 39f6f59 + d0205ce commit 994b83f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 18 deletions.
25 changes: 25 additions & 0 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ Running a Service
Discover and run a service class. This will start the service in the foreground and run it until the process terminates.

It is possible to override the default settings using a ``--config`` switch

.. code-block:: shell
$ nameko run --config ./foobar.yaml <module>[:<ServiceClass>]
and providing a simple YAML configuration file:

.. code-block:: yaml
# foobar.yaml
AMQP_URI: 'amqp://guest:guest@localhost'
WEB_SERVER_ADDRESS: '0.0.0.0:8000'
rpc_exchange: 'nameko-rpc'
max_workers: 10
parent_calls_tracked: 10
Interacting with running services
---------------------------------
Expand All @@ -38,3 +62,4 @@ Dispatching an event as "source_service":
$ nameko shell
>>> n.dispatch_event("source_service", "event_type", "event_payload")
20 changes: 17 additions & 3 deletions nameko/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import re
import signal
import sys
import yaml

from eventlet import backdoor

Expand Down Expand Up @@ -168,7 +169,14 @@ def main(args):
import_service(path)
)

config = {AMQP_URI_CONFIG_KEY: args.broker}
config = {}

if args.config:
with open(args.config) as fle:
config = yaml.load(fle)
else:
config[AMQP_URI_CONFIG_KEY] = args.broker

run(services, config, backdoor_port=args.backdoor_port)


Expand All @@ -177,13 +185,19 @@ def init_parser(parser):
'services', nargs='+',
metavar='module[:service class]',
help='python path to one or more service classes to run')

parser.add_argument(
'--config', default='',
help='The YAML configuration file')

parser.add_argument(
'--broker', default='amqp://guest:guest@localhost',
help='RabbitMQ broker url')

parser.add_argument(
'--backdoor-port', type=int,
help='Specify a port number to host a backdoor, which can be connected'
' to for an interactive interpreter within the running service'
' process using `nameko backdoor`.'
)
' process using `nameko backdoor`.')

return parser
20 changes: 11 additions & 9 deletions nameko/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
AMQP_URI_CONFIG_KEY = 'AMQP_URI'
WEB_SERVER_CONFIG_KEY = 'WEB_SERVER_ADDRESS'
RPC_EXCHANGE_CONFIG_KEY = 'rpc_exchange'

MAX_WORKERS_CONFIG_KEY = 'max_workers'
PARENT_CALLS_CONFIG_KEY = 'parent_calls_tracked'

DEFAULT_MAX_WORKERS = 10
DEFAULT_PARENT_CALLS_TRACKED = 10

DEFAULT_RETRY_POLICY = {'max_retries': 3}


CALL_ID_STACK_CONTEXT_KEY = 'call_id_stack'
AUTH_TOKEN_CONTEXT_KEY = 'auth_token'
Expand All @@ -14,12 +25,3 @@
AUTH_TOKEN_CONTEXT_KEY,
CALL_ID_STACK_CONTEXT_KEY,
)


MAX_WORKERS_CONFIG_KEY = 'max_workers'
PARENT_CALLS_CONFIG_KEY = 'parent_calls_tracked'

DEFAULT_MAX_WORKERS = 10
DEFAULT_PARENT_CALLS_TRACKED = 10

DEFAULT_RETRY_POLICY = {'max_retries': 3}
4 changes: 2 additions & 2 deletions nameko/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from kombu import Connection, Exchange, Queue
from kombu.pools import producers

from nameko.constants import AMQP_URI_CONFIG_KEY, DEFAULT_RETRY_POLICY
from nameko.constants import (
AMQP_URI_CONFIG_KEY, DEFAULT_RETRY_POLICY, RPC_EXCHANGE_CONFIG_KEY)
from nameko.exceptions import (
ContainerBeingKilled, deserialize, MalformedRequest, MethodNotFound,
RpcConnectionError, serialize, UnknownService, UnserializableValueError)
Expand All @@ -22,7 +23,6 @@
_log = getLogger(__name__)


RPC_EXCHANGE_CONFIG_KEY = 'rpc_exchange'
RPC_QUEUE_TEMPLATE = 'rpc-{}'
RPC_REPLY_QUEUE_TEMPLATE = 'rpc.reply-{}-{}'

Expand Down
3 changes: 2 additions & 1 deletion nameko/standalone/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from kombu.common import maybe_declare

from nameko.amqp import verify_amqp_uri
from nameko.constants import AMQP_URI_CONFIG_KEY
from nameko.containers import WorkerContext
from nameko.extensions import Entrypoint
from nameko.exceptions import RpcConnectionError, RpcTimeout
Expand Down Expand Up @@ -75,7 +76,7 @@ def _setup_queue(self):

def register_provider(self, provider):
self.provider = provider
amqp_uri = provider.container.config['AMQP_URI']
amqp_uri = provider.container.config[AMQP_URI_CONFIG_KEY]
verify_amqp_uri(amqp_uri)
self.connection = Connection(amqp_uri)
self.queue = provider.queue
Expand Down
3 changes: 1 addition & 2 deletions nameko/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
from werkzeug.routing import Map
from werkzeug.wrappers import Request

from nameko.constants import WEB_SERVER_CONFIG_KEY
from nameko.exceptions import ConfigurationError
from nameko.extensions import ProviderCollector, SharedExtension


WEB_SERVER_CONFIG_KEY = 'WEB_SERVER_ADDRESS'

BindAddress = namedtuple("BindAddress", ['address', 'port'])


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"kombu>=3.0.1",
"mock>=1.0.1",
"path.py>=6.2",
"pyyaml>=3.10",
"requests>=1.2.0",
"six>=1.9.0",
"werkzeug>=0.9",
Expand Down
6 changes: 6 additions & 0 deletions test/cli/run-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# nameko config file for testing
# $> nameko run --config run-config.yaml foobar

WEB_SERVER_ADDRESS: '0.0.0.0:8001'

AMQP_URI: 'amqp://foo:bar@example.org'
25 changes: 24 additions & 1 deletion test/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import signal
import socket

from os.path import join, dirname, abspath
import eventlet
from mock import patch
import pytest
Expand All @@ -16,6 +16,9 @@
from test.sample import Service


RUN_CONFIG_FILE = abspath(join(dirname(__file__), 'run-config.yaml'))


def test_run(rabbit_config):
parser = setup_parser()
broker = rabbit_config['AMQP_URI']
Expand All @@ -40,6 +43,26 @@ def test_run(rabbit_config):
gt.wait()


def test_main_with_config(rabbit_config):
parser = setup_parser()
args = parser.parse_args([
'run',
'--config',
RUN_CONFIG_FILE,
'test.sample',
])

with patch('nameko.cli.run.run') as run:
main(args)
assert run.call_count == 1
(_, config) = run.call_args[0]

assert config == {
'WEB_SERVER_ADDRESS': '0.0.0.0:8001',
'AMQP_URI': 'amqp://foo:bar@example.org'
}


def test_import_ok():
assert import_service('test.sample') == [Service]
assert import_service('test.sample:Service') == [Service]
Expand Down

0 comments on commit 994b83f

Please sign in to comment.