Skip to content
Snippets Groups Projects
Commit 60bdd690 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel
Browse files

Worker: allow more configuration through config file

Also added our own subclass of ConfigParser to make some stuff easier.
parent 403c5067
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,12 @@ manager_url = http://localhost:8083/ ...@@ -3,6 +3,12 @@ manager_url = http://localhost:8083/
job_types = sleep blender-render job_types = sleep blender-render
task_update_queue_db = flamenco-worker.db task_update_queue_db = flamenco-worker.db
may_i_run_interval_seconds = 5
push_log_max_interval_seconds = 20
push_log_max_entries = 200
push_act_max_interval_seconds = 10
[loggers] [loggers]
keys = root,flamenco_worker keys = root,flamenco_worker
......
...@@ -35,11 +35,11 @@ def main(): ...@@ -35,11 +35,11 @@ def main():
from . import runner, worker, upstream, upstream_update_queue, may_i_run from . import runner, worker, upstream, upstream_update_queue, may_i_run
fmanager = upstream.FlamencoManager( fmanager = upstream.FlamencoManager(
manager_url=confparser.get(config.CONFIG_SECTION, 'manager_url'), manager_url=confparser.value('manager_url'),
) )
tuqueue = upstream_update_queue.TaskUpdateQueue( tuqueue = upstream_update_queue.TaskUpdateQueue(
db_fname=confparser.get(config.CONFIG_SECTION, 'task_update_queue_db'), db_fname=confparser.value('task_update_queue_db'),
manager=fmanager, manager=fmanager,
shutdown_future=shutdown_future, shutdown_future=shutdown_future,
) )
...@@ -50,18 +50,20 @@ def main(): ...@@ -50,18 +50,20 @@ def main():
manager=fmanager, manager=fmanager,
trunner=trunner, trunner=trunner,
tuqueue=tuqueue, tuqueue=tuqueue,
job_types=confparser.get(config.CONFIG_SECTION, 'job_types').split(), job_types=confparser.value('job_types').split(),
worker_id=confparser.get(config.CONFIG_SECTION, 'worker_id'), worker_id=confparser.value('worker_id'),
worker_secret=confparser.get(config.CONFIG_SECTION, 'worker_secret'), worker_secret=confparser.value('worker_secret'),
loop=loop, loop=loop,
shutdown_future=shutdown_future, shutdown_future=shutdown_future,
push_log_max_interval=confparser.interval_secs('push_log_max_interval_seconds'),
push_log_max_entries=confparser.value('push_log_max_entries', int),
push_act_max_interval=confparser.interval_secs('push_act_max_interval_seconds'),
) )
mir_interval = float(confparser.get(config.CONFIG_SECTION, 'may_i_run_interval_seconds'))
mir = may_i_run.MayIRun( mir = may_i_run.MayIRun(
manager=fmanager, manager=fmanager,
worker=fworker, worker=fworker,
poll_interval=datetime.timedelta(seconds=mir_interval), poll_interval=confparser.interval_secs('may_i_run_interval_seconds'),
loop=loop, loop=loop,
) )
......
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
import collections import collections
import configparser import configparser
import datetime
import os.path import os.path
import logging import logging
from . import worker
HOME_CONFIG_FILE = os.path.expanduser('~/.flamenco-worker.cfg') HOME_CONFIG_FILE = os.path.expanduser('~/.flamenco-worker.cfg')
GLOBAL_CONFIG_FILE = 'flamenco-worker.cfg' GLOBAL_CONFIG_FILE = 'flamenco-worker.cfg'
CONFIG_SECTION = 'flamenco-worker' CONFIG_SECTION = 'flamenco-worker'
...@@ -17,16 +20,36 @@ DEFAULT_CONFIG = { ...@@ -17,16 +20,36 @@ DEFAULT_CONFIG = {
('may_i_run_interval_seconds', '5'), ('may_i_run_interval_seconds', '5'),
('worker_id', ''), ('worker_id', ''),
('worker_secret', ''), ('worker_secret', ''),
# All intervals in seconds
('push_log_max_interval_seconds', str(worker.PUSH_LOG_MAX_INTERVAL.total_seconds())),
('push_log_max_entries', str(worker.PUSH_LOG_MAX_ENTRIES)),
('push_act_max_interval_seconds', str(worker.PUSH_ACT_MAX_INTERVAL.total_seconds())),
]) ])
} }
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class ConfigParser(configparser.ConfigParser):
"""ConfigParser that can easily get values from our default config section."""
_DEFAULT_INTERPOLATION = configparser.ExtendedInterpolation()
def value(self, key, valtype: type=str):
return valtype(self.get(CONFIG_SECTION, key))
def interval_secs(self, key) -> datetime.timedelta:
"""Returns the configuration value as timedelta."""
secs = self.value(key, float)
return datetime.timedelta(seconds=secs)
def merge_with_home_config(new_conf: dict): def merge_with_home_config(new_conf: dict):
"""Updates the home configuration file with the given config dict.""" """Updates the home configuration file with the given config dict."""
confparser = configparser.ConfigParser() confparser = ConfigParser()
confparser.read_dict({CONFIG_SECTION: {}}) confparser.read_dict({CONFIG_SECTION: {}})
confparser.read(HOME_CONFIG_FILE, encoding='utf8') confparser.read(HOME_CONFIG_FILE, encoding='utf8')
...@@ -45,15 +68,14 @@ def merge_with_home_config(new_conf: dict): ...@@ -45,15 +68,14 @@ def merge_with_home_config(new_conf: dict):
def load_config(config_file: str = None, def load_config(config_file: str = None,
show_effective_config: bool = False) -> configparser.ConfigParser: show_effective_config: bool = False) -> ConfigParser:
"""Loads one or more configuration files.""" """Loads one or more configuration files."""
# Logging and the default interpolation of configparser both use the # Logging and the default interpolation of configparser both use the
# same syntax for variables. To make it easier to work with, we use # same syntax for variables. To make it easier to work with, we use
# another interpolation for config files, so they now use ${loglevel} # another interpolation for config files, so they now use ${loglevel}
# whereas logging still uses %(levelname)s. # whereas logging still uses %(levelname)s.
confparser = configparser.ConfigParser( confparser = ConfigParser()
interpolation=configparser.ExtendedInterpolation())
confparser.read_dict(DEFAULT_CONFIG) confparser.read_dict(DEFAULT_CONFIG)
if config_file: if config_file:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment