Skip to content

Commit

Permalink
Lightbus now loads config on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcharnock committed Apr 18, 2018
1 parent 5eb1010 commit dfc35e9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 44 deletions.
9 changes: 8 additions & 1 deletion lightbus/bus.py
Expand Up @@ -57,6 +57,13 @@ def setup(self, plugins: dict=None):
directly. This you be handled for you if you are
calling `run_forever()`.
"""
logger.info(
LBullets("Lightbus getting ready to start", items={
'service_name (set with -s)': self.config.service_name,
'process_name (with with -p)': self.config.process_name,
})
)

if plugins is None:
logger.debug("Auto-loading any installed Lightbus plugins...")
plugins = autoload_plugins(self.config)
Expand Down Expand Up @@ -105,7 +112,7 @@ def run_forever(self, *, consume_rpcs=True, plugins=None):
event_transport = self.transport_registry.get_event_transport('default', default=None)

logger.info(LBullets(
"Lightbus getting ready to run. Default transports are:",
"Lightbus getting ready to run. Default transports are",
items={
"RPC transport": L(
'{}.{}',
Expand Down
20 changes: 16 additions & 4 deletions lightbus/commands/__init__.py
Expand Up @@ -24,9 +24,13 @@ def lightbus_entry_point(): # pragma: no cover

def parse_args(args=None):
parser = argparse.ArgumentParser(description='Lightbus management command.')
parser.add_argument('service_name')
parser.add_argument('--service-name', '-s',
help='Name of service in which this process resides. You should '
'likely set this in production. Will default to a random string.')
parser.add_argument('--process-name', '-p',
help='A unique name of this process within the service. Will '
'default to a random string.')
parser.add_argument('--config', help='Config file to load, JSON or YAML', metavar='FILE')
# TODO: Log level flag (plus honor --config flag)
parser.add_argument('--log-level', help='Set the log level. Overrides any value set in config. '
'One of debug, info, warning, critical, exception.', metavar='LOG_LEVEL')

Expand All @@ -53,6 +57,14 @@ def parse_args(args=None):

def load_config(args) -> Config:
if args.config:
return Config.load_file(file_path=args.config)
config = Config.load_file(file_path=args.config)
else:
return Config.load_dict({})
config = Config.load_dict({})

if args.service_name:
config._config.service_name = args.service_name

if args.process_name:
config._config.process_name = args.process_name

return config
2 changes: 1 addition & 1 deletion lightbus/commands/dump_config_schema.py
Expand Up @@ -18,7 +18,7 @@ def setup(self, parser, subparsers):
"bus' API schema, for that see the more commonly used 'dumpschema' "
"command",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_shell.add_argument('--schema', '-s',
parser_shell.add_argument('--schema', '-m',
help='File to write config schema to. '
'If omitted schema will be written to standard out.',
metavar='FILE',
Expand Down
2 changes: 1 addition & 1 deletion lightbus/commands/dump_schema.py
Expand Up @@ -16,7 +16,7 @@ def setup(self, parser, subparsers):
parser_shell = subparsers.add_parser('dumpschema',
help='Dumps all currently present bus schemas to a file or directory',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser_shell.add_argument('--schema', '-s',
parser_shell.add_argument('--schema', '-m',
help='File or directory to write schema to. If a directory is '
'specified one schema file will be created for each API. '
'If omitted schema will be written to standard out.',
Expand Down
38 changes: 2 additions & 36 deletions lightbus/commands/run.py
Expand Up @@ -19,54 +19,20 @@ def setup(self, parser, subparsers):
help='Only listen for and handle events, do not respond to RPC calls',
action='store_true')
parser_run_action_group.add_argument(
'--schema', '-s',
'--schema', '-m',
help='Manually load the schema from the given file or directory. '
'This will normally be provided by the schema transport, '
'but manual loading may be useful during development or testing.',
metavar='FILE_OR_DIRECTORY',
)
parser_run.set_defaults(func=self.handle)

parser_run_transport_group = parser_run.add_argument_group(title='Transport options')
parser_run_transport_group.add_argument(
'--rpc-transport', '-p', help='Default RPC transport class to use', default='lightbus.RedisRpcTransport'
)
parser_run_transport_group.add_argument(
'--result-transport', '-t', help='Default result transport class to use', default='lightbus.RedisResultTransport'
)
parser_run_transport_group.add_argument(
'--event-transport', '-e', help='Default event transport class to use', default='lightbus.RedisEventTransport'
)
parser_run_transport_group.add_argument(
'--schema-transport', '-a', help='Default schema transport class to use', default='lightbus.RedisSchemaTransport'
)

parser_run_connection_group = parser_run.add_argument_group(title='Connection options')
parser_run_connection_group.add_argument(
'--redis-url', '-r', help='URL to Redis server when using Redis-based transports', default='redis://localhost:6379/0'
)

def handle(self, args, config, dry_run=False):
self.setup_logging(args.log_level, config)

try:
rpc_transport = import_from_string(args.rpc_transport)
result_transport = import_from_string(args.result_transport)
event_transport = import_from_string(args.event_transport)
schema_transport = import_from_string(args.schema_transport)
except ImportError as e:
logger.critical("Error when trying to import transports: {}. Perhaps check your config for typos.".format(e))
return

bus_module = self.import_bus(args)

bus = create(
config=config,
rpc_transport=rpc_transport(url=args.redis_url),
result_transport=result_transport(url=args.redis_url),
event_transport=event_transport(url=args.redis_url),
schema_transport=schema_transport(url=args.redis_url),
)
bus = create(config=config)

if args.schema:
if args.schema == '-':
Expand Down
11 changes: 10 additions & 1 deletion lightbus/config/config.py
Expand Up @@ -6,6 +6,7 @@
import jsonschema
import yaml as yamllib

from lightbus.exceptions import UnexpectedConfigurationFormat
from lightbus.schema.hints_to_schema import python_type_to_json_schemas, SCHEMA_URI

if False:
Expand Down Expand Up @@ -69,7 +70,15 @@ def load_json(cls, json: str):
@classmethod
def load_yaml(cls, yaml: str):
"""Instantiate the config from a YAML string"""
return cls.load_dict(config=yamllib.load(yaml))
config = yamllib.load(yaml)
if not isinstance(config, dict):
raise UnexpectedConfigurationFormat(
f"The config file was loaded but it appears to be in an unexpected format. "
f"The root of the configuration should be a key/value mapping, but the "
f"type '{type(config).__name__}' was found instead. Check your config "
f"file is correctly formatted."
)
return cls.load_dict(config=config)

@classmethod
def load_dict(cls, config: dict, set_defaults=True):
Expand Down
4 changes: 4 additions & 0 deletions lightbus/exceptions.py
Expand Up @@ -131,3 +131,7 @@ class ApisMustUseSameTransport(LightbusException):

class OnlyAvailableOnRootNode(LightbusException):
pass


class UnexpectedConfigurationFormat(LightbusException):
pass

0 comments on commit dfc35e9

Please sign in to comment.