Commit 592b4583 authored by Matthias's avatar Matthias
Browse files

improve handling of jobresults

parent 425c865e
Pipeline #25881 passed with stages
in 2 minutes and 44 seconds
from flask_restful import Resource, request
from flask_restful import current_app
from requests.auth import HTTPBasicAuth
from helpers.Error import ImportApiError
from datetime import datetime
import requests
......@@ -84,13 +86,12 @@ class WriteJobResultToDrupal(Resource):
"""
try:
body = request.json
report = json.dumps(body, indent=2)
current_app.logger.debug("report data: " + report)
current_app.logger.debug("report data: " + json.dumps(body, indent=2))
fatal = body['fatal']
status = 'FAILED'
if fatal == 0:
status = 'SUCCESS'
return write_results(job_drupal_uuid, job_log_drupal_uuid, status, report)
return write_results(job_drupal_uuid, job_log_drupal_uuid, status, body)
except ImportApiError as e:
return {'error': e.message}, 500
......@@ -98,18 +99,47 @@ class WriteJobResultToDrupal(Resource):
def write_results(job_drupal_uuid, job_log_drupal_uuid, status, report):
current_app.logger.debug("job-uuid to write: " + job_drupal_uuid)
current_app.logger.debug("job-report-uuid to write: " + job_log_drupal_uuid)
current_app.logger.debug("report to write: " + report)
current_app.logger.debug("report to write: " + json.dumps(report, indent=2))
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
calls = {}
url = current_app.config['drupal-api-url'] + \
user = current_app.config['drupal-user']
password = current_app.config['drupal-password']
auth = HTTPBasicAuth(user, password)
# read "status" and "message" from log_result
previous_status = ''
previous_report = ''
step = ''
try:
getUrl = current_app.config['drupal-api-url'] + \
'/jsonapi/node/log_result/' + job_log_drupal_uuid
response = requests.get(getUrl, headers=headers, auth=auth)
logResult = response.json()['data']
previous_status = logResult['attributes']['field_status']
previous_report = logResult['attributes']['field_message']
except requests.exceptions.RequestException:
message = 'It was not possible to read form the Drupal API:' + getUrl +\
'\nresponse: ' + response
current_app.logger.error(message)
raise ImportApiError(message)
if previous_status == 'FAILED':
status = previous_status
if previous_report is None:
previous_report = {}
step = report['step']
previous_report[step] = report
reportString = json.dumps(previous_report, indent=2)
# update values in drupal:
patchCalls = {}
patchUrl = current_app.config['drupal-api-url'] + \
'/jsonapi/node/import_process/' + job_drupal_uuid
data = {
patchData = {
"data": {
"id": job_drupal_uuid,
"type": "node--import_process",
......@@ -118,28 +148,28 @@ def write_results(job_drupal_uuid, job_log_drupal_uuid, status, report):
}
}
}
calls[url] = data
patchCalls[patchUrl] = patchData
url = current_app.config['drupal-api-url'] + \
patchUrl = current_app.config['drupal-api-url'] + \
'/jsonapi/node/log_result/' + job_log_drupal_uuid
data = {
patchData = {
"data": {
"id": job_log_drupal_uuid,
"type": "node--job_result",
"attributes": {
"field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00"),
"field_status": status,
"field_message": report
"field_message": reportString
}
}
}
calls[url] = data
patchCalls[patchUrl] = patchData
returnVal = {'message': ''}
for call in calls:
url = call
data = calls[call]
for patchCall in patchCalls:
url = patchCall
data = patchCalls[patchCall]
try:
response = requests.patch(
url,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment