Skip to content
Snippets Groups Projects
Commit 3ab410b6 authored by Eva Bardou's avatar Eva Bardou
Browse files

Use element_id instead of element in update_activity

parent 8f5cf687
No related branches found
No related tags found
1 merge request!85Use element_id instead of element in update_activity
Pipeline #78407 passed
......@@ -99,16 +99,16 @@ class ElementsWorker(
logger.info(f"Processing {element} ({i}/{count})")
# Report start of process, run process, then report end of process
self.update_activity(element, ActivityState.Started)
self.update_activity(element.id, ActivityState.Started)
self.process_element(element)
self.update_activity(element, ActivityState.Processed)
self.update_activity(element.id, ActivityState.Processed)
except ErrorResponse as e:
failed += 1
logger.warning(
f"An API error occurred while processing element {element_id}: {e.title} - {e.content}",
exc_info=e if self.args.verbose else None,
)
self.update_activity(element, ActivityState.Error)
self.update_activity(element_id, ActivityState.Error)
self.report.error(element_id, e)
except Exception as e:
failed += 1
......@@ -116,7 +116,7 @@ class ElementsWorker(
f"Failed running worker on element {element_id}: {e}",
exc_info=e if self.args.verbose else None,
)
self.update_activity(element, ActivityState.Error)
self.update_activity(element_id, ActivityState.Error)
self.report.error(element_id, e)
# Save report as local artifact
......@@ -134,14 +134,14 @@ class ElementsWorker(
def process_element(self, element):
"""Override this method to analyze an Arkindex element from the provided list"""
def update_activity(self, element, state):
def update_activity(self, element_id, state):
"""
Update worker activity for this element
This method should not raise a runtime exception, but simply warn users
"""
assert element and isinstance(
element, Element
), "element shouldn't be null and should be of type Element"
assert element_id and isinstance(
element_id, str
), "element_id shouldn't be null and should be of type str"
assert isinstance(state, ActivityState), "state should be an ActivityState"
if not self.features.get("workers_activity"):
......@@ -157,17 +157,17 @@ class ElementsWorker(
"UpdateWorkerActivity",
id=self.worker_version_id,
body={
"element_id": element.id,
"element_id": element_id,
"state": state.value,
},
)
logger.debug(f"Updated activity of element {element.id} to {state}")
logger.debug(f"Updated activity of element {element_id} to {state}")
return out
except ErrorResponse as e:
logger.warning(
f"Failed to update activity of element {element.id} to {state.value} due to an API error: {e.content}"
f"Failed to update activity of element {element_id} to {state.value} due to an API error: {e.content}"
)
except Exception as e:
logger.warning(
f"Failed to update activity of element {element.id} to {state.value}: {e}"
f"Failed to update activity of element {element_id} to {state.value}: {e}"
)
......@@ -4,7 +4,6 @@ import json
import pytest
from apistar.exceptions import ErrorResponse
from arkindex_worker.models import Element
from arkindex_worker.worker import ActivityState
# Common API calls for all workers
......@@ -81,9 +80,7 @@ def test_feature_disabled(responses, mock_elements_worker):
"""Test disabled calls do not trigger any API calls"""
assert not mock_elements_worker.is_read_only
out = mock_elements_worker.update_activity(
Element({"id": "1234-deadbeef"}), ActivityState.Processed
)
out = mock_elements_worker.update_activity("1234-deadbeef", ActivityState.Processed)
assert out is None
assert len(responses.calls) == 2
......@@ -98,9 +95,7 @@ def test_readonly(responses, mock_elements_worker):
assert mock_elements_worker.is_read_only is True
mock_elements_worker.features["workers_activity"] = True
out = mock_elements_worker.update_activity(
Element({"id": "1234-deadbeef"}), ActivityState.Processed
)
out = mock_elements_worker.update_activity("1234-deadbeef", ActivityState.Processed)
assert out is None
assert len(responses.calls) == 2
......@@ -122,9 +117,7 @@ def test_update_call(responses, mock_elements_worker):
# Enable worker activity
mock_elements_worker.features["workers_activity"] = True
out = mock_elements_worker.update_activity(
Element({"id": "1234-deadbeef"}), ActivityState.Processed
)
out = mock_elements_worker.update_activity("1234-deadbeef", ActivityState.Processed)
# Check the response received by worker
assert out == {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment