Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

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 Resource, request
from flask_restful import current_app from flask_restful import current_app
from requests.auth import HTTPBasicAuth
from helpers.Error import ImportApiError from helpers.Error import ImportApiError
from datetime import datetime from datetime import datetime
import requests import requests
...@@ -84,13 +86,12 @@ class WriteJobResultToDrupal(Resource): ...@@ -84,13 +86,12 @@ class WriteJobResultToDrupal(Resource):
""" """
try: try:
body = request.json body = request.json
report = json.dumps(body, indent=2) current_app.logger.debug("report data: " + json.dumps(body, indent=2))
current_app.logger.debug("report data: " + report)
fatal = body['fatal'] fatal = body['fatal']
status = 'FAILED' status = 'FAILED'
if fatal == 0: if fatal == 0:
status = 'SUCCESS' 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: except ImportApiError as e:
return {'error': e.message}, 500 return {'error': e.message}, 500
...@@ -98,18 +99,47 @@ class WriteJobResultToDrupal(Resource): ...@@ -98,18 +99,47 @@ class WriteJobResultToDrupal(Resource):
def write_results(job_drupal_uuid, job_log_drupal_uuid, status, report): 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-uuid to write: " + job_drupal_uuid)
current_app.logger.debug("job-report-uuid to write: " + job_log_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 = { headers = {
'Content-Type': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json', 'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key'] 'X-API-Key': current_app.config['drupal-api-key']
} }
calls = {} user = current_app.config['drupal-user']
url = current_app.config['drupal-api-url'] + \ 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 '/jsonapi/node/import_process/' + job_drupal_uuid
data = { patchData = {
"data": { "data": {
"id": job_drupal_uuid, "id": job_drupal_uuid,
"type": "node--import_process", "type": "node--import_process",
...@@ -118,28 +148,28 @@ def write_results(job_drupal_uuid, job_log_drupal_uuid, status, report): ...@@ -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 '/jsonapi/node/log_result/' + job_log_drupal_uuid
data = { patchData = {
"data": { "data": {
"id": job_log_drupal_uuid, "id": job_log_drupal_uuid,
"type": "node--job_result", "type": "node--job_result",
"attributes": { "attributes": {
"field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00"), "field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00"),
"field_status": status, "field_status": status,
"field_message": report "field_message": reportString
} }
} }
} }
calls[url] = data patchCalls[patchUrl] = patchData
returnVal = {'message': ''} returnVal = {'message': ''}
for call in calls: for patchCall in patchCalls:
url = call url = patchCall
data = calls[call] data = patchCalls[patchCall]
try: try:
response = requests.patch( response = requests.patch(
url, 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