Commit e9263f57 authored by Lionel Walter's avatar Lionel Walter

Update status and end time together with report summary, fix MEMO-451

parent b8a69cb2
Pipeline #10906 failed with stages
in 8 minutes and 4 seconds
from import_api_app.models.Error import ImportApiError
from flask_restful import current_app
from datetime import datetime
import requests
import json
......@@ -18,7 +19,8 @@ def write_results(job_log_drupal_uuid, report):
"id": job_log_drupal_uuid,
"type": "paragraph--job_log_result",
"attributes": {
"field_summary": report
"field_summary": report,
"field_end_date": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S+00:00")
}
}
}
......@@ -52,3 +54,56 @@ def write_results(job_log_drupal_uuid, report):
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
# make the job as not active any more and write the end data
# todo refactor this in a more generic function
def update_status(job_drupal_uuid, job_name):
# in drupal job names have underscore instead of minus
job_name = job_name.replace("-", "_")
current_app.logger.debug("uuid to write: " + job_drupal_uuid)
headers = {
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json',
'X-API-Key': current_app.config['drupal-api-key']
}
data = {
"data": {
"id": job_drupal_uuid,
"type": "paragraph--job_"+job_name,
"attributes": {
"field_is_active": False
}
}
}
url = current_app.config['DRUPAL_API_URL'] + \
'/paragraph/job_'+job_name+'/' + job_drupal_uuid
try:
response = requests.patch(
url,
headers=headers,
data=json.dumps(data)
)
except requests.exceptions.RequestException:
message = "It was not possible to write to Drupal API \
via the following url " + url
current_app.logger.error(message)
raise ImportApiError(message)
if response.status_code == 200:
current_app.logger.error('Updated: '+url)
return {'message': 'Updated: ' + url}
elif response.status_code == 403:
message = "Not authorized to write to: " + url
current_app.logger.error(message)
raise ImportApiError(message)
elif response.status_code == 404:
message = 'Not Found: ' + url
current_app.logger.error(message)
raise ImportApiError(message)
else:
message = "Unknown response status code for drupal api for url " + url
current_app.logger.error(message)
raise ImportApiError(message)
......@@ -8,7 +8,7 @@ from import_api_app.models.Error import ImportApiError
from import_api_app.resources.KafkaTopics import create_topics
from import_api_app.resources.HelmStart import start
from import_api_app.resources.JobReport import get_report
from import_api_app.resources.WriteJobResultsToDrupal import write_results
from import_api_app.resources.WriteJobResultsToDrupal import write_results, update_status
from kubernetes import client
......@@ -107,8 +107,10 @@ class JobStart(Resource):
return {'error': e.message}, 500
FlaskThread(target=reporting, args=(
job_name,
job_reporting_topic,
job_parameters['drupalJobLogResultUuid']
job_parameters['drupalJobLogResultUuid'],
job_parameters['drupalJobUuid']
)).start()
return helm_result
......@@ -124,7 +126,7 @@ class FlaskThread(Thread):
super().run()
def reporting(job_reporting_topic, drupal_job_log_uuid):
def reporting(job_name, job_reporting_topic, drupal_job_log_uuid, drupal_job_uuid):
# wait a bit before consuming results
time.sleep(3)
......@@ -139,3 +141,8 @@ def reporting(job_reporting_topic, drupal_job_log_uuid):
write_results(drupal_job_log_uuid, report["report"])
except ImportApiError as e:
return {'error': e.message}, 500
try:
update_status(drupal_job_uuid, job_name)
except ImportApiError as e:
return {'error': e.message}, 500
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