Skip to content
Snippets Groups Projects

Load configuration from API or local file

Merged Bastien Abadie requested to merge load-configuration into master
6 files
+ 340
232
Compare changes
  • Side-by-side
  • Inline
Files
6
+ 54
2
@@ -7,6 +7,7 @@ import sys
import uuid
from enum import Enum
import yaml
from apistar.exceptions import ErrorResponse
from arkindex import ArkindexClient, options_from_env
@@ -33,16 +34,27 @@ class BaseWorker(object):
self.worker_version_id = os.environ.get("WORKER_VERSION_ID")
if not self.worker_version_id:
raise Exception(
"Missing WORKER_VERSION_ID environment variable to start the Worker"
logger.warning(
"Missing WORKER_VERSION_ID environment variable, worker is in read-only mode"
)
logger.info(f"Worker will use {self.work_dir} as working directory")
@property
def is_read_only(self):
"""Worker cannot publish anything without a worker version ID"""
return self.worker_version_id is None
def configure(self):
"""
Configure worker using cli args and environment variables
"""
self.parser.add_argument(
"-c",
"--config",
help="Alternative configuration file when running without a Worker Version ID",
type=open,
)
self.parser.add_argument(
"-v",
"--verbose",
@@ -66,6 +78,25 @@ class BaseWorker(object):
self.api_client = ArkindexClient(**options_from_env())
logger.debug(f"Setup Arkindex API client on {self.api_client.document.url}")
if self.worker_version_id:
# Retrieve initial configuration from API
worker_version = self.api_client.request(
"RetrieveWorkerVersion", id=self.worker_version_id
)
logger.info(
f"Loaded worker {worker_version['worker']['name']} revision {worker_version['revision']} from API"
)
self.config = worker_version["configuration"]
elif self.args.config:
# Load config from YAML file
self.config = yaml.safe_load(self.args.config)
logger.info(
f"Running with local configuration from {self.args.config.name}"
)
else:
self.config = {}
logger.warning("Running without any extra configuration")
def add_arguments(self):
"""Override this method to add argparse argument to this worker"""
@@ -208,6 +239,9 @@ class ElementsWorker(BaseWorker):
assert all(
isinstance(coord, (int, float)) for point in polygon for coord in point
), "polygon points should be lists of two numbers"
if self.is_read_only:
logger.warning("Cannot create element as this worker is in read-only mode")
return
sub_element = self.api_client.request(
"CreateElement",
@@ -241,6 +275,11 @@ class ElementsWorker(BaseWorker):
assert (
score and isinstance(score, float) and 0 <= score <= 1
), "score shouldn't be null and should be a float in [0..1] range"
if self.is_read_only:
logger.warning(
"Cannot create transcription as this worker is in read-only mode"
)
return
self.api_client.request(
"CreateTranscription",
@@ -272,6 +311,11 @@ class ElementsWorker(BaseWorker):
assert high_confidence and isinstance(
high_confidence, bool
), "high_confidence shouldn't be null and should be of type bool"
if self.is_read_only:
logger.warning(
"Cannot create classification as this worker is in read-only mode"
)
return
self.api_client.request(
"CreateClassification",
@@ -306,6 +350,9 @@ class ElementsWorker(BaseWorker):
assert isinstance(metas, dict), "metas should be of type dict"
if validated:
assert isinstance(validated, bool), "validated should be of type bool"
if self.is_read_only:
logger.warning("Cannot create entity as this worker is in read-only mode")
return
entity = self.api_client.request(
"CreateEntity",
@@ -365,6 +412,11 @@ class ElementsWorker(BaseWorker):
assert all(
isinstance(coord, (int, float)) for point in polygon for coord in point
), f"Transcription at index {index} in transcriptions: polygon points should be lists of two numbers"
if self.is_read_only:
logger.warning(
"Cannot create transcriptions as this worker is in read-only mode"
)
return
annotations = self.api_client.request(
"CreateElementTranscriptions",
Loading