Skip to content

Commit

Permalink
Dynamically load source drivers by name
Browse files Browse the repository at this point in the history
Health job now loads driver modules dynamically, by it's type. If
source's driver type is "foo" it expects 'health.drivers.foo.driver' to
be present and to contain main function, that will be passed driver
configuration and max timestamp.
  • Loading branch information
teferi committed Nov 23, 2016
1 parent 641b67d commit 62b041c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion health/drivers/tcp/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def record_from_bucket(bucket, timestamp, service):
return record


def main(es, latest_aggregated_ts=None):
def main(config, latest_aggregated_ts=None):
es = config["elastic_src"]
ts_min, ts_max = utils.get_min_max_timestamps(es, "Timestamp")

if latest_aggregated_ts:
Expand Down
20 changes: 16 additions & 4 deletions health/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import importlib
import json
import logging
import sys
Expand All @@ -23,7 +24,6 @@
import requests
import schedule

from health.drivers.tcp import driver as tcp_driver
from health.drivers import utils
from health.mapping import es

Expand Down Expand Up @@ -83,6 +83,16 @@
CONF = None


def _get_driver(driver_type):
driver = None
try:
driver = importlib.import_module("." + driver_type + ".driver",
"health.drivers")
except ImportError:
logging.error("Could not load driver for '{}'".format(driver_type))
return driver


def job():
started_at = time.time()
logging.info("Starting Syncing Job")
Expand All @@ -92,9 +102,11 @@ def job():
min_ts, max_ts = utils.get_min_max_timestamps(backend_url, "timestamp")

for src in CONF["sources"]:
# TODO(boris-42): Make this actually pluggable
data_generator = tcp_driver.main(src["driver"]["elastic_src"],
latest_aggregated_ts=max_ts)
driver = _get_driver(src["driver"]["type"])
if not driver:
continue
data_generator = driver.main(src["driver"],
latest_aggregated_ts=max_ts)

logging.info("Start syncing %s region" % src["region"])

Expand Down

0 comments on commit 62b041c

Please sign in to comment.