Skip to content

Commit

Permalink
Run api even if hdx api is not reachable , Display failed info in the…
Browse files Browse the repository at this point in the history
… API
  • Loading branch information
kshitijrajsharma committed Dec 31, 2023
1 parent 1c78e11 commit 2492ffb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
28 changes: 24 additions & 4 deletions API/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import html
import json
from datetime import datetime

Expand Down Expand Up @@ -39,7 +38,28 @@ def get_task_status(
Returns:
id: Id of the task
status : SUCCESS / PENDING
status : Possible values includes:
PENDING
The task is waiting for execution.
STARTED
The task has been started.
RETRY
The task is to be retried, possibly because of failure.
FAILURE
The task raised an exception, or has exceeded the retry limit. The result attribute then contains the exception raised by the task.
SUCCESS
The task executed successfully. The result attribute then contains the tasks return value.
result : Result of task
Successful task will have additional nested json inside
Expand All @@ -49,8 +69,8 @@ def get_task_status(
task_response_result = None
if task_result.status == "SUCCESS":
task_response_result = task_result.result
if task_result.status == "FAILED":
task_response_result = html.escape(task_result.traceback)
if task_result.state != "SUCCESS":
task_response_result = str(task_result.info)

result = {
"id": task_id,
Expand Down
8 changes: 7 additions & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,13 @@ def upload_dataset(self, dump_config_to_s3=False):
)

self.dataset.set_reference_period(datetime.now())
self.dataset.create_in_hdx(allow_no_resources=True)
try:
self.dataset.create_in_hdx(allow_no_resources=True)
dataset_info["hdx_upload"] = "SUCCESS"
except Exception as ex:
logging.error(ex)
dataset_info["hdx_upload"] = "FAILED"

dataset_info["name"] = self.dataset["name"]
dataset_info["hdx_url"] = f"{HDX_URL_PREFIX}/dataset/{self.dataset['name']}"
dataset_info["resources"] = self.resources
Expand Down
22 changes: 19 additions & 3 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
)


def not_raises(func, *args, **kwargs):
try:
func(*args, **kwargs)
return True
except Exception as ex:
logging.error(ex)
return False


####################

### EXPORT_UPLOAD CONFIG BLOCK
Expand Down Expand Up @@ -204,7 +213,7 @@
logging.debug(HDX_URL_PREFIX)
except Exception as e:
logging.error(
f"Error creating HDX configuration: {e}, Disabling the hdx exports feature"
"Error creating HDX configuration: %s, Disabling the hdx exports feature", e
)
ENABLE_HDX_EXPORTS = False

Expand All @@ -221,13 +230,20 @@
ALLOWED_HDX_TAGS = parse_list(
os.environ.get("ALLOWED_HDX_TAGS")
or config.get("HDX", "ALLOWED_HDX_TAGS", fallback=None)
or Vocabulary.approved_tags()
or (
Vocabulary.approved_tags() if not_raises(Vocabulary.approved_tags) else None
)
)
ALLOWED_HDX_UPDATE_FREQUENCIES = parse_list(
os.environ.get("ALLOWED_HDX_UPDATE_FREQUENCIES")
or config.get("HDX", "ALLOWED_HDX_UPDATE_FREQUENCIES", fallback=None)
or Dataset.list_valid_update_frequencies()
or (
Dataset.list_valid_update_frequencies()
if not_raises(Dataset.list_valid_update_frequencies)
else None
)
)

DUCK_DB_MEMORY_LIMIT = os.environ.get("DUCK_DB_MEMORY_LIMIT") or config.get(
"HDX", "DUCK_DB_MEMORY_LIMIT", fallback=None
)
Expand Down
18 changes: 10 additions & 8 deletions src/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,11 @@ def validate_tags(cls, value):
"""
if value:
for item in value:
if item.strip() not in ALLOWED_HDX_TAGS:
raise ValueError(
f"Invalid tag {item.strip()} , Should be within {ALLOWED_HDX_TAGS}"
)
if ALLOWED_HDX_TAGS:
if item.strip() not in ALLOWED_HDX_TAGS:
raise ValueError(
f"Invalid tag {item.strip()} , Should be within {ALLOWED_HDX_TAGS}"
)
return value


Expand Down Expand Up @@ -538,10 +539,11 @@ def validate_frequency(cls, value):
Returns:
_type_: _description_
"""
if value.strip() not in ALLOWED_HDX_UPDATE_FREQUENCIES:
raise ValueError(
f"Invalid update frequency , Should be within {ALLOWED_HDX_UPDATE_FREQUENCIES}"
)
if ALLOWED_HDX_UPDATE_FREQUENCIES:
if value.strip() not in ALLOWED_HDX_UPDATE_FREQUENCIES:
raise ValueError(
f"Invalid update frequency , Should be within {ALLOWED_HDX_UPDATE_FREQUENCIES}"
)
return value.strip()


Expand Down

0 comments on commit 2492ffb

Please sign in to comment.