Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • workers/base-worker
1 result
Show changes
Commits on Source (5)
0.2.1 0.2.2-beta2
...@@ -156,6 +156,7 @@ class CachedTranscriptionEntity(Model): ...@@ -156,6 +156,7 @@ class CachedTranscriptionEntity(Model):
entity = ForeignKeyField(CachedEntity, backref="transcription_entities") entity = ForeignKeyField(CachedEntity, backref="transcription_entities")
offset = IntegerField(constraints=[Check("offset >= 0")]) offset = IntegerField(constraints=[Check("offset >= 0")])
length = IntegerField(constraints=[Check("length > 0")]) length = IntegerField(constraints=[Check("length > 0")])
worker_version_id = UUIDField()
class Meta: class Meta:
primary_key = CompositeKey("transcription", "entity") primary_key = CompositeKey("transcription", "entity")
......
...@@ -62,6 +62,7 @@ class BaseWorker(object): ...@@ -62,6 +62,7 @@ class BaseWorker(object):
logger.info(f"Worker will use {self.work_dir} as working directory") logger.info(f"Worker will use {self.work_dir} as working directory")
self.process_information = None self.process_information = None
self.user_configuration = None
self.support_cache = support_cache self.support_cache = support_cache
# use_cache will be updated in configure() if the cache is supported and if there # use_cache will be updated in configure() if the cache is supported and if there
# is at least one available sqlite database either given or in the parent tasks # is at least one available sqlite database either given or in the parent tasks
...@@ -160,6 +161,15 @@ class BaseWorker(object): ...@@ -160,6 +161,15 @@ class BaseWorker(object):
# Load all required secrets # Load all required secrets
self.secrets = {name: self.load_secret(name) for name in required_secrets} self.secrets = {name: self.load_secret(name) for name in required_secrets}
# Load worker run configuration when available and not in dev mode
if os.environ.get("ARKINDEX_WORKER_RUN_ID") and not self.args.dev:
worker_run = self.request(
"RetrieveWorkerRun", id=os.environ["ARKINDEX_WORKER_RUN_ID"]
)
self.user_configuration = worker_run.get("configuration")
if self.user_configuration:
logger.info("Loaded user configuration from WorkerRun")
task_id = os.environ.get("PONOS_TASK") task_id = os.environ.get("PONOS_TASK")
paths = None paths = None
if self.support_cache and self.args.database is not None: if self.support_cache and self.args.database is not None:
......
...@@ -31,7 +31,7 @@ class ElementMixin(object): ...@@ -31,7 +31,7 @@ class ElementMixin(object):
if missing_slugs: if missing_slugs:
raise MissingTypeError( raise MissingTypeError(
f'Element type(s) {", ".join(missing_slugs)} were not found in the {corpus["name"]} corpus ({corpus["id"]}).' f'Element type(s) {", ".join(sorted(missing_slugs))} were not found in the {corpus["name"]} corpus ({corpus["id"]}).'
) )
return True return True
......
...@@ -21,7 +21,7 @@ class EntityType(Enum): ...@@ -21,7 +21,7 @@ class EntityType(Enum):
class EntityMixin(object): class EntityMixin(object):
def create_entity( def create_entity(
self, element, name, type, corpus=None, metas=None, validated=None self, element, name, type, corpus=None, metas=dict(), validated=None
): ):
""" """
Create an entity on the given corpus through API Create an entity on the given corpus through API
...@@ -111,6 +111,7 @@ class EntityMixin(object): ...@@ -111,6 +111,7 @@ class EntityMixin(object):
"entity": entity, "entity": entity,
"length": length, "length": length,
"offset": offset, "offset": offset,
"worker_version_id": self.worker_version_id,
}, },
) )
# TODO: Report transcription entity creation # TODO: Report transcription entity creation
...@@ -118,15 +119,13 @@ class EntityMixin(object): ...@@ -118,15 +119,13 @@ class EntityMixin(object):
if self.use_cache: if self.use_cache:
# Store transcription entity in local cache # Store transcription entity in local cache
try: try:
to_insert = [ CachedTranscriptionEntity.create(
{ transcription=transcription,
"transcription": transcription, entity=entity,
"entity": entity, offset=offset,
"offset": offset, length=length,
"length": length, worker_version_id=self.worker_version_id,
} )
]
CachedTranscriptionEntity.insert_many(to_insert).execute()
except IntegrityError as e: except IntegrityError as e:
logger.warning( logger.warning(
f"Couldn't save created transcription entity in local cache: {e}" f"Couldn't save created transcription entity in local cache: {e}"
......
...@@ -119,18 +119,56 @@ def test_cli_arg_verbose_given(mocker, mock_config_api): ...@@ -119,18 +119,56 @@ def test_cli_arg_verbose_given(mocker, mock_config_api):
logger.setLevel(logging.NOTSET) logger.setLevel(logging.NOTSET)
def test_configure_dev_mode(mocker, mock_user_api, mock_worker_version_api): def test_configure_dev_mode(
mocker, monkeypatch, mock_user_api, mock_worker_version_api
):
""" """
Configuring a worker in developer mode avoid retrieving process information Configuring a worker in developer mode avoid retrieving process information
""" """
worker = BaseWorker() worker = BaseWorker()
mocker.patch.object(sys, "argv", ["worker", "--dev"]) mocker.patch.object(sys, "argv", ["worker", "--dev"])
monkeypatch.setenv(
"ARKINDEX_WORKER_RUN_ID", "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
)
worker.configure() worker.configure()
assert worker.args.dev is True assert worker.args.dev is True
assert worker.process_information is None assert worker.process_information is None
assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234"
assert worker.is_read_only is True assert worker.is_read_only is True
assert worker.user_configuration is None
def test_configure_worker_run(mocker, monkeypatch, responses, mock_config_api):
worker = BaseWorker()
mocker.patch.object(sys, "argv", ["worker"])
run_id = "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
monkeypatch.setenv("ARKINDEX_WORKER_RUN_ID", run_id)
responses.add(
responses.GET,
f"http://testserver/api/v1/imports/workers/{run_id}/",
json={"id": run_id, "configuration": {"a": "b"}},
)
worker.configure()
assert worker.user_configuration == {"a": "b"}
def test_configure_worker_run_missing_conf(
mocker, monkeypatch, responses, mock_config_api
):
worker = BaseWorker()
mocker.patch.object(sys, "argv", ["worker"])
run_id = "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
monkeypatch.setenv("ARKINDEX_WORKER_RUN_ID", run_id)
responses.add(
responses.GET,
f"http://testserver/api/v1/imports/workers/{run_id}/",
json={"id": run_id},
)
worker.configure()
assert worker.user_configuration is None
def test_load_missing_secret(): def test_load_missing_secret():
......
...@@ -58,7 +58,7 @@ def test_create_tables(tmp_path): ...@@ -58,7 +58,7 @@ def test_create_tables(tmp_path):
CREATE TABLE "elements" ("id" TEXT NOT NULL PRIMARY KEY, "parent_id" TEXT, "type" VARCHAR(50) NOT NULL, "image_id" TEXT, "polygon" text, "initial" INTEGER NOT NULL, "worker_version_id" TEXT, FOREIGN KEY ("image_id") REFERENCES "images" ("id")) CREATE TABLE "elements" ("id" TEXT NOT NULL PRIMARY KEY, "parent_id" TEXT, "type" VARCHAR(50) NOT NULL, "image_id" TEXT, "polygon" text, "initial" INTEGER NOT NULL, "worker_version_id" TEXT, FOREIGN KEY ("image_id") REFERENCES "images" ("id"))
CREATE TABLE "entities" ("id" TEXT NOT NULL PRIMARY KEY, "type" VARCHAR(50) NOT NULL, "name" TEXT NOT NULL, "validated" INTEGER NOT NULL, "metas" text, "worker_version_id" TEXT NOT NULL) CREATE TABLE "entities" ("id" TEXT NOT NULL PRIMARY KEY, "type" VARCHAR(50) NOT NULL, "name" TEXT NOT NULL, "validated" INTEGER NOT NULL, "metas" text, "worker_version_id" TEXT NOT NULL)
CREATE TABLE "images" ("id" TEXT NOT NULL PRIMARY KEY, "width" INTEGER NOT NULL, "height" INTEGER NOT NULL, "url" TEXT NOT NULL) CREATE TABLE "images" ("id" TEXT NOT NULL PRIMARY KEY, "width" INTEGER NOT NULL, "height" INTEGER NOT NULL, "url" TEXT NOT NULL)
CREATE TABLE "transcription_entities" ("transcription_id" TEXT NOT NULL, "entity_id" TEXT NOT NULL, "offset" INTEGER NOT NULL CHECK (offset >= 0), "length" INTEGER NOT NULL CHECK (length > 0), PRIMARY KEY ("transcription_id", "entity_id"), FOREIGN KEY ("transcription_id") REFERENCES "transcriptions" ("id"), FOREIGN KEY ("entity_id") REFERENCES "entities" ("id")) CREATE TABLE "transcription_entities" ("transcription_id" TEXT NOT NULL, "entity_id" TEXT NOT NULL, "offset" INTEGER NOT NULL CHECK (offset >= 0), "length" INTEGER NOT NULL CHECK (length > 0), "worker_version_id" TEXT NOT NULL, PRIMARY KEY ("transcription_id", "entity_id"), FOREIGN KEY ("transcription_id") REFERENCES "transcriptions" ("id"), FOREIGN KEY ("entity_id") REFERENCES "entities" ("id"))
CREATE TABLE "transcriptions" ("id" TEXT NOT NULL PRIMARY KEY, "element_id" TEXT NOT NULL, "text" TEXT NOT NULL, "confidence" REAL NOT NULL, "worker_version_id" TEXT NOT NULL, FOREIGN KEY ("element_id") REFERENCES "elements" ("id"))""" CREATE TABLE "transcriptions" ("id" TEXT NOT NULL PRIMARY KEY, "element_id" TEXT NOT NULL, "text" TEXT NOT NULL, "confidence" REAL NOT NULL, "worker_version_id" TEXT NOT NULL, FOREIGN KEY ("element_id") REFERENCES "elements" ("id"))"""
actual_schema = "\n".join( actual_schema = "\n".join(
......
...@@ -52,7 +52,7 @@ def test_check_required_types(monkeypatch, tmp_path, mock_elements_worker, respo ...@@ -52,7 +52,7 @@ def test_check_required_types(monkeypatch, tmp_path, mock_elements_worker, respo
assert worker.check_required_types(corpus_id, "page", "text_line", "act") assert worker.check_required_types(corpus_id, "page", "text_line", "act")
assert ( assert (
str(e.value) str(e.value)
== "Element type(s) text_line, act were not found in the Some Corpus corpus (12341234-1234-1234-1234-123412341234)." == "Element type(s) act, text_line were not found in the Some Corpus corpus (12341234-1234-1234-1234-123412341234)."
) )
......
...@@ -213,7 +213,7 @@ def test_create_entity(responses, mock_elements_worker): ...@@ -213,7 +213,7 @@ def test_create_entity(responses, mock_elements_worker):
assert json.loads(responses.calls[-1].request.body) == { assert json.loads(responses.calls[-1].request.body) == {
"name": "Bob Bob", "name": "Bob Bob",
"type": "person", "type": "person",
"metas": None, "metas": {},
"validated": None, "validated": None,
"corpus": "12341234-1234-1234-1234-123412341234", "corpus": "12341234-1234-1234-1234-123412341234",
"worker_version": "12341234-1234-1234-1234-123412341234", "worker_version": "12341234-1234-1234-1234-123412341234",
...@@ -247,7 +247,7 @@ def test_create_entity_with_cache(responses, mock_elements_worker_with_cache): ...@@ -247,7 +247,7 @@ def test_create_entity_with_cache(responses, mock_elements_worker_with_cache):
assert json.loads(responses.calls[-1].request.body) == { assert json.loads(responses.calls[-1].request.body) == {
"name": "Bob Bob", "name": "Bob Bob",
"type": "person", "type": "person",
"metas": None, "metas": {},
"validated": None, "validated": None,
"corpus": "12341234-1234-1234-1234-123412341234", "corpus": "12341234-1234-1234-1234-123412341234",
"worker_version": "12341234-1234-1234-1234-123412341234", "worker_version": "12341234-1234-1234-1234-123412341234",
...@@ -261,7 +261,7 @@ def test_create_entity_with_cache(responses, mock_elements_worker_with_cache): ...@@ -261,7 +261,7 @@ def test_create_entity_with_cache(responses, mock_elements_worker_with_cache):
type="person", type="person",
name="Bob Bob", name="Bob Bob",
validated=False, validated=False,
metas=None, metas={},
worker_version_id=UUID("12341234-1234-1234-1234-123412341234"), worker_version_id=UUID("12341234-1234-1234-1234-123412341234"),
) )
] ]
...@@ -449,6 +449,7 @@ def test_create_transcription_entity(responses, mock_elements_worker): ...@@ -449,6 +449,7 @@ def test_create_transcription_entity(responses, mock_elements_worker):
"entity": "11111111-1111-1111-1111-111111111111", "entity": "11111111-1111-1111-1111-111111111111",
"offset": 5, "offset": 5,
"length": 10, "length": 10,
"worker_version_id": "12341234-1234-1234-1234-123412341234",
} }
...@@ -504,6 +505,7 @@ def test_create_transcription_entity_with_cache( ...@@ -504,6 +505,7 @@ def test_create_transcription_entity_with_cache(
"entity": "11111111-1111-1111-1111-111111111111", "entity": "11111111-1111-1111-1111-111111111111",
"offset": 5, "offset": 5,
"length": 10, "length": 10,
"worker_version_id": "12341234-1234-1234-1234-123412341234",
} }
# Check that created transcription entity was properly stored in SQLite cache # Check that created transcription entity was properly stored in SQLite cache
...@@ -513,5 +515,6 @@ def test_create_transcription_entity_with_cache( ...@@ -513,5 +515,6 @@ def test_create_transcription_entity_with_cache(
entity=UUID("11111111-1111-1111-1111-111111111111"), entity=UUID("11111111-1111-1111-1111-111111111111"),
offset=5, offset=5,
length=10, length=10,
worker_version_id=UUID("12341234-1234-1234-1234-123412341234"),
) )
] ]