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 (11)
0.2.1-beta1
0.2.1-beta2
......@@ -72,19 +72,28 @@ class CachedElement(Model):
"""
if not self.image_id or not self.polygon:
raise ValueError(f"Element {self.id} has no image")
# Always fetch the image from the bounding box when size differs from full image
bounding_box = polygon_bounding_box(self.polygon)
if (
bounding_box.width != self.image.width
or bounding_box.height != self.image.height
):
box = f"{bounding_box.x},{bounding_box.y},{bounding_box.x + bounding_box.width},{bounding_box.y + bounding_box.height}"
else:
box = "full"
if max_size is None:
resize = "full"
else:
bounding_box = polygon_bounding_box(self.polygon)
# Do not resize for polygons that do not exactly match the images
# as the resize is made directly by the IIIF server using the box parameter
if (
bounding_box.width != self.image.width
or bounding_box.height != self.image.height
):
resize = "full"
logger.warning(
"Only full size elements covered, downloading full size image"
)
# Do not resize when the image is below the maximum size
elif self.image.width <= max_size and self.image.height <= max_size:
resize = "full"
......@@ -99,7 +108,7 @@ class CachedElement(Model):
if not url.endswith("/"):
url += "/"
return open_image(f"{url}full/{resize}/0/default.jpg", *args, **kwargs)
return open_image(f"{url}{box}/{resize}/0/default.jpg", *args, **kwargs)
class CachedTranscription(Model):
......
......@@ -86,6 +86,8 @@ class ElementsWorker(
@property
def store_activity(self):
if self.args.dev:
return False
assert (
self.process_information
), "Worker must be configured to access its process activity state"
......
......@@ -61,6 +61,7 @@ class BaseWorker(object):
logger.info(f"Worker will use {self.work_dir} as working directory")
self.process_information = None
self.support_cache = support_cache
# 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
......@@ -68,8 +69,8 @@ class BaseWorker(object):
@property
def is_read_only(self):
"""Worker cannot publish anything without a worker version ID"""
return self.worker_version_id is None
"""Worker cannot publish anything in dev mode or without a worker version ID"""
return self.args.dev or self.worker_version_id is None
def configure(self):
"""
......@@ -95,6 +96,15 @@ class BaseWorker(object):
action="store_true",
default=False,
)
self.parser.add_argument(
"--dev",
help=(
"Run worker in developer mode. "
"Worker will be in read-only state even if a worker_version is supplied. "
"ARKINDEX_PROCESS_ID environment variable is not required with this flag."
),
action="store_true",
)
# Call potential extra arguments
self.add_arguments()
......@@ -116,13 +126,14 @@ class BaseWorker(object):
logger.debug(f"Connected as {user['display_name']} - {user['email']}")
self.features = user["features"]
# Load process information
assert os.environ.get(
"ARKINDEX_PROCESS_ID"
), "ARKINDEX_PROCESS_ID environment variable is not defined"
self.process_information = self.request(
"RetrieveDataImport", id=os.environ["ARKINDEX_PROCESS_ID"]
)
# Load process information except in developer mode
if not self.args.dev:
assert os.environ.get(
"ARKINDEX_PROCESS_ID"
), "ARKINDEX_PROCESS_ID environment variable is not defined"
self.process_information = self.request(
"RetrieveDataImport", id=os.environ["ARKINDEX_PROCESS_ID"]
)
if self.worker_version_id:
# Retrieve initial configuration from API
......
arkindex-client==1.0.6
peewee==3.14.4
Pillow==8.1.0
python-gitlab==2.6.0
Pillow==8.2.0
python-gitlab==2.7.1
python-gnupg==0.4.7
sh==1.14.1
sh==1.14.2
tenacity==7.0.0
pytest==6.2.3
pytest-mock==3.5.1
pytest-responses==0.4.0
pytest==6.2.4
pytest-mock==3.6.1
pytest-responses==0.5.0
......@@ -119,6 +119,20 @@ def test_cli_arg_verbose_given(mocker, mock_config_api):
logger.setLevel(logging.NOTSET)
def test_configure_dev_mode(mocker, mock_user_api, mock_worker_version_api):
"""
Configuring a worker in developer mode avoid retrieving process information
"""
worker = BaseWorker()
mocker.patch.object(sys, "argv", ["worker", "--dev"])
worker.configure()
assert worker.args.dev is True
assert worker.process_information is None
assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234"
assert worker.is_read_only is True
def test_load_missing_secret():
worker = BaseWorker()
worker.api_client = MockApiClient()
......
......@@ -78,13 +78,15 @@ CREATE TABLE "transcriptions" ("id" TEXT NOT NULL PRIMARY KEY, "element_id" TEXT
[
# No max_size: no resize
(400, 600, 400, 600, None, "http://something/full/full/0/default.jpg"),
# No max_size: resize on bbox
(400, 600, 200, 100, None, "http://something/0,0,200,100/full/0/default.jpg"),
# max_size equal to the image size, no resize
(400, 600, 400, 600, 600, "http://something/full/full/0/default.jpg"),
(600, 400, 600, 400, 600, "http://something/full/full/0/default.jpg"),
(400, 400, 400, 400, 400, "http://something/full/full/0/default.jpg"),
# max_size is smaller than the image, resize
(400, 600, 400, 600, 400, "http://something/full/266,400/0/default.jpg"),
(400, 600, 200, 600, 400, "http://something/full/266,400/0/default.jpg"),
(400, 600, 200, 600, 400, "http://something/0,0,200,600/full/0/default.jpg"),
(600, 400, 600, 400, 400, "http://something/full/400,266/0/default.jpg"),
(400, 400, 400, 400, 200, "http://something/full/200,200/0/default.jpg"),
# max_size above the image size, no resize
......@@ -118,9 +120,9 @@ def test_element_open_image(
image=image,
polygon=[
[0, 0],
[image_width, 0],
[image_width, image_height],
[0, image_height],
[polygon_width, 0],
[polygon_width, polygon_height],
[0, polygon_height],
[0, 0],
],
)
......
......@@ -91,6 +91,7 @@ def test_list_elements_element_arg(mocker, mock_elements_worker):
verbose=False,
elements_list=None,
database=None,
dev=False,
),
)
......@@ -121,6 +122,7 @@ def test_list_elements_both_args_error(mocker, mock_elements_worker):
verbose=False,
elements_list=open(path),
database=None,
dev=False,
),
)
......@@ -144,6 +146,7 @@ def test_database_arg(mocker, mock_elements_worker, tmp_path):
verbose=False,
elements_list=None,
database=str(database_path),
dev=False,
),
)
......
......@@ -100,6 +100,20 @@ def test_activities_disabled(
] == BASE_API_CALLS
def test_activities_dev_mode(mocker, mock_user_api, mock_worker_version_api):
"""
Worker activities are not stored in dev mode
"""
worker = ElementsWorker()
mocker.patch.object(sys, "argv", ["worker", "--dev"])
worker.configure()
assert worker.args.dev is True
assert worker.process_information is None
assert worker.is_read_only is True
assert worker.store_activity is False
def test_update_call(responses, mock_elements_worker, mock_process_api):
"""Test an update call with feature enabled triggers an API call"""
responses.add(
......
......@@ -435,7 +435,7 @@ def test_create_merge_request__no_retry_5xx_error(
def test_create_merge_request__retry_5xx_error(
fake_responses, fake_gitlab_helper_factory
):
request_url = f"https://gitlab.com/api/v4/projects/{PROJECT_ID}/merge_requests?retry_transient_errors=True"
request_url = f"https://gitlab.com/api/v4/projects/{PROJECT_ID}/merge_requests"
fake_responses.add(
fake_responses.POST,
......
arkindex-base-worker==0.1.14
arkindex-base-worker==0.2.0