Commit 5e2bb96b authored by Lionel Walter's avatar Lionel Walter
Browse files

Adapt get Job and list Job to the new syntax

parent 381b3563
Pipeline #9891 passed with stages
in 6 minutes and 52 seconds
......@@ -55,7 +55,7 @@ def create_app(test_config=None):
'/v1/job/<institution_id>/<record_set_id>/<process_id>/<job_name>/start'
)
api.add_resource(JobStop, '/v1/job/<process_id>/<job_name>/stop')
api.add_resource(Job, '/v1/job/<job_id>')
api.add_resource(Job, '/v1/job/<process_id>/<job_name>')
# api.add_resource(ReadJobOptionsFromDrupal, '/v1/fromDrupal/<job_drupal_uuid>')
# TODO : maybe take that to a configuration (development vs pod running in
......
from kubernetes import client
from flask_restful import Resource, current_app, abort
from import_api_app import utility
# Job
# shows a single Job
class Job(Resource):
def get(self, job_id):
def get(self, process_id, job_name):
"""
Get Job Info
---
tags:
- Job Info and Job Delete
- Job Info
parameters:
- in: path
name: job_id
name: process_id
required: true
description: The full job id (you get it from list jobs),
for example p0001-j0001-text-file-validation-4jlrc
description: The identifier of the import
process. Must be lowercase and contain only letters, numbers and -
default: 789
- in: path
type: string
name: job_name
example: text-file-validation
enum: ['text-file-validation', 'table-data-transform', 'mapper-service']
required: true
responses:
200:
description: The job data
......@@ -38,48 +45,11 @@ class Job(Resource):
type: string
example: Job XXX doesn't exist
"""
v1 = client.CoreV1Api()
v1 = client.BatchV1Api()
try:
pod = v1.read_namespaced_pod(namespace=current_app.config['NAMESPACE'], name=job_id)
response = {
"status": pod.status.phase,
"started": pod.status.start_time.strftime("%m/%d/%Y, %H:%M:%S")
}
return response
helm_name = utility.generate_helm_name(process_id, job_name)
job = v1.read_namespaced_job(namespace=current_app.config['NAMESPACE'], name=helm_name)
return utility.get_job_info(job)
except Exception as exception:
abort(404, message="Job {} doesn't exist".format(
job_id) + ". Details :" + exception.body)
def delete(self, job_id):
"""
Delete a job
---
tags:
- Job Info and Job Delete
parameters:
- in: path
name: job_id
required: true
description: The full job id (you get it from list jobs),
for example p0001-j0001-text-file-validation-4jlrc
type: string
responses:
204:
description: job deleted
404:
description: Not found
schema:
properties:
message:
type: string
example: Job XXX doesn't exist
"""
v1 = client.CoreV1Api()
try:
v1.delete_namespaced_pod(
name=job_id, namespace=current_app.config['NAMESPACE'])
except Exception as exception:
abort(404, message="Job {} doesn't exist".format(
job_id) + ". Details :" + exception.body)
return '', 204
helm_name) + ". Details :" + exception.body)
from kubernetes import client
from flask_restful import Resource, current_app
from datetime import timezone
from import_api_app import utility
class JobList(Resource):
......@@ -35,15 +35,5 @@ class JobList(Resource):
response = {}
for job in jobs.items:
utc_start_time = job.status.start_time
local_start_time = utc_start_time.replace(tzinfo=timezone.utc).astimezone(tz=None)
status = 'Unknown'
if job.status.failed:
status = 'Failed'
elif (job.status.active):
status = 'Running'
response[job.metadata.name] = {
"status": status,
"started": local_start_time.strftime("%m/%d/%Y, %H:%M:%S")
}
response[job.metadata.name] = utility.get_job_info(job)
return response
from datetime import timezone
def generate_helm_name(process_id, job_name):
return process_id + '-' + job_name
def get_job_info(job):
""""
job is a Kubernetes V1Job
"""
utc_start_time = job.status.start_time
local_start_time = utc_start_time.replace(tzinfo=timezone.utc).astimezone(tz=None)
status = 'Unknown'
if job.status.failed:
status = 'Failed'
elif (job.status.active):
status = 'Running'
return {
"status": status,
"started": local_start_time.strftime("%m/%d/%Y, %H:%M:%S")
}
......@@ -50,9 +50,20 @@ def test_job_start(client):
assert response.status_code == 500
def test_get_job(client):
response = client.get('/v1/job/789/text-file-validation')
assert response.status_code == 200
response = client.get('/v1/job/78999999/text-file-validation')
assert response.status_code == 404
def test_job_stop(client):
response = client.delete('/v1/job/789/text-file-validation/stop')
assert response.status_code == 200
response = client.delete('/v1/job/789/jdskfhjkdshjkewhrjkewrjkewhjkhrjkewhr/stop')
assert response.status_code == 500
response = client.get('/v1/job/789/text-file-validation')
assert response.status_code == 404
Supports Markdown
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