Skip to content

Commit

Permalink
Fail and exit worker if there is mappings.json is missing or has a sy…
Browse files Browse the repository at this point in the history
…ntax error
  • Loading branch information
jensens committed Jan 16, 2024
1 parent 4eb6422 commit df9f426
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
------------------

- Fix: Do not fail on `field_remove` or `full_remove` if section or name does not exist. [jensens]
- Fix: Fail and exit worker if there is `mappings.json` is missing or has a syntax error. [jensens]

2.0.0 (2023-12-05)
------------------
Expand Down
13 changes: 13 additions & 0 deletions src/collective/elastic/ingest/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from celery import Celery

import os
import sys


# sentry integration
Expand Down Expand Up @@ -38,6 +39,9 @@
def index(path, timestamp, index_name):
try:
content = fetch_content(path, timestamp)
except RuntimeError:
logger.error("Fatal error, stop worker")
sys.exit(1)
except Exception:
msg = "Error while fetching content from Plone"
# xxx: retry handling!
Expand All @@ -47,12 +51,18 @@ def index(path, timestamp, index_name):
return
try:
schema = fetch_schema()
except RuntimeError:
logger.error("Fatal error, stop worker")
sys.exit(1)
except Exception:
msg = "Error while fetching schema from Plone"
logger.exception(msg)
return msg
try:
process_ingest(content, schema, index_name)
except RuntimeError:
logger.error("Fatal error, stop worker")
sys.exit(1)
except Exception:
# xxx: retry handling!
msg = "Error while writing data to ElasticSearch"
Expand All @@ -65,6 +75,9 @@ def index(path, timestamp, index_name):
def unindex(uid, index_name):
try:
remove(uid, index_name)
except RuntimeError:
logger.error("Fatal error, stop worker")
sys.exit(1)
except Exception:
# xxx: retry handling!
msg = "Error while removing data from ElasticSearch"
Expand Down
9 changes: 7 additions & 2 deletions src/collective/elastic/ingest/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ def get_field_map() -> dict:
if STATE["fieldmap"] == {}:
_mappings_file = os.environ.get("MAPPINGS_FILE", None)
if not _mappings_file:
raise ValueError("No mappings file configured.")
raise RuntimeError("No mappings file configured.")
with open(_mappings_file) as fp:
STATE["fieldmap"] = json.load(fp)
try:
STATE["fieldmap"] = json.load(fp)
except json.decoder.JSONDecodeError as e:
raise RuntimeError(
f"Error while loading mappings file {_mappings_file}: {e}"
)
assert isinstance(STATE["fieldmap"], dict)
return STATE["fieldmap"]

Expand Down

0 comments on commit df9f426

Please sign in to comment.