Skip to content
Snippets Groups Projects
Commit a8c9a750 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Add awx_job_id and ended_at to Task table

parent 3368ab75
No related branches found
No related tags found
No related merge requests found
...@@ -961,9 +961,11 @@ class Task(db.Model): ...@@ -961,9 +961,11 @@ class Task(db.Model):
# Use job id generated by RQ # Use job id generated by RQ
id = db.Column(postgresql.UUID, primary_key=True) id = db.Column(postgresql.UUID, primary_key=True)
created_at = db.Column(db.DateTime, default=utcnow()) created_at = db.Column(db.DateTime, default=utcnow())
ended_at = db.Column(db.DateTime)
name = db.Column(db.Text, nullable=False, index=True) name = db.Column(db.Text, nullable=False, index=True)
command = db.Column(db.Text) command = db.Column(db.Text)
status = db.Column(db.Enum(JobStatus, name='job_status')) status = db.Column(db.Enum(JobStatus, name='job_status'))
awx_job_id = db.Column(db.Integer)
user_id = db.Column(db.Integer, db.ForeignKey('user_account.id'), user_id = db.Column(db.Integer, db.ForeignKey('user_account.id'),
nullable=False, default=utils.fetch_current_user_id) nullable=False, default=utils.fetch_current_user_id)
...@@ -973,10 +975,12 @@ class Task(db.Model): ...@@ -973,10 +975,12 @@ class Task(db.Model):
def to_dict(self): def to_dict(self):
return { return {
'id': self.id, 'id': self.id,
'created_at': utils.format_field(self.created_at),
'name': self.name, 'name': self.name,
'created_at': utils.format_field(self.created_at),
'ended_at': utils.format_field(self.ended_at),
'status': self.status.name,
'awx_job_id': self.awx_job_id,
'command': self.command, 'command': self.command,
'status': self.status,
'user': str(self.user), 'user': str(self.user),
} }
......
...@@ -11,7 +11,7 @@ This module implements the task blueprint. ...@@ -11,7 +11,7 @@ This module implements the task blueprint.
""" """
from flask import Blueprint, render_template, jsonify from flask import Blueprint, render_template, jsonify
from flask_login import login_required, current_user from flask_login import login_required, current_user
from .. import utils, models from .. import models
bp = Blueprint('task', __name__) bp = Blueprint('task', __name__)
...@@ -32,11 +32,5 @@ def view_task(id_): ...@@ -32,11 +32,5 @@ def view_task(id_):
@bp.route('/_retrieve_tasks') @bp.route('/_retrieve_tasks')
@login_required @login_required
def retrieve_tasks(): def retrieve_tasks():
data = [(task.id, data = [list(task.to_dict().values()) for task in current_user.get_tasks()]
task.name,
utils.format_field(task.created_at),
task.status.name,
task.command,
str(task.user))
for task in current_user.get_tasks()]
return jsonify(data=data) return jsonify(data=data)
...@@ -12,7 +12,7 @@ This module implements tasks to run. ...@@ -12,7 +12,7 @@ This module implements tasks to run.
import time import time
import tower_cli import tower_cli
from flask import current_app from flask import current_app
from rq import Worker from rq import Worker, get_current_job
from .extensions import db from .extensions import db
from . import utils, models from . import utils, models
...@@ -23,7 +23,7 @@ class TaskWorker(Worker): ...@@ -23,7 +23,7 @@ class TaskWorker(Worker):
the task status in the CSEntry database the task status in the CSEntry database
""" """
def update_task_status(self, job, status): def update_task_attribute(self, job, name, value):
# The task is created after enqueueing the job. # The task is created after enqueueing the job.
# If the job is processed very quickly, the task might # If the job is processed very quickly, the task might
# not be found in the database. We wait up to 3 seconds. # not be found in the database. We wait up to 3 seconds.
...@@ -35,21 +35,23 @@ class TaskWorker(Worker): ...@@ -35,21 +35,23 @@ class TaskWorker(Worker):
self.log.warning('task not found...') self.log.warning('task not found...')
time.sleep(1) time.sleep(1)
else: else:
self.log.error(f'Task {job.id} not found! Task status not updated!') self.log.error(f'Task {job.id} not found! Task attribute not updated!')
return return
task.status = status setattr(task, name, value)
db.session.commit() db.session.commit()
def move_to_failed_queue(self, job, *exc_info): def move_to_failed_queue(self, job, *exc_info):
self.update_task_status(job, status=models.JobStatus.FAILED) self.update_task_attribute(job, 'ended_at', job.ended_at)
self.update_task_attribute(job, 'status', models.JobStatus.FAILED)
super().move_to_failed_queue(job, *exc_info) super().move_to_failed_queue(job, *exc_info)
def handle_job_success(self, job, queue, started_job_registry): def handle_job_success(self, job, queue, started_job_registry):
self.update_task_status(job, status=models.JobStatus.FINISHED) self.update_task_attribute(job, 'ended_at', job.ended_at)
self.update_task_attribute(job, 'status', models.JobStatus.FINISHED)
super().handle_job_success(job, queue, started_job_registry) super().handle_job_success(job, queue, started_job_registry)
def prepare_job_execution(self, job): def prepare_job_execution(self, job):
self.update_task_status(job, status=models.JobStatus.STARTED) self.update_task_attribute(job, 'status', models.JobStatus.STARTED)
super().prepare_job_execution(job) super().prepare_job_execution(job)
...@@ -118,7 +120,8 @@ def trigger_core_services_update(): ...@@ -118,7 +120,8 @@ def trigger_core_services_update():
return task return task
def launch_job_template(job_template, monitor=True, **kwargs): def launch_job_template(job_template, **kwargs):
rq_job = get_current_job()
if (job_template in (current_app.config['AWX_CREATE_VIOC'], current_app.config['AWX_CREATE_VM']) and if (job_template in (current_app.config['AWX_CREATE_VIOC'], current_app.config['AWX_CREATE_VM']) and
not current_app.config.get('AWX_VM_CREATION_ENABLED', False)): not current_app.config.get('AWX_VM_CREATION_ENABLED', False)):
current_app.logger.info('AWX VM creation is disabled. Not sending any request.') current_app.logger.info('AWX VM creation is disabled. Not sending any request.')
...@@ -126,6 +129,13 @@ def launch_job_template(job_template, monitor=True, **kwargs): ...@@ -126,6 +129,13 @@ def launch_job_template(job_template, monitor=True, **kwargs):
if not current_app.config.get('AWX_JOB_ENABLED', False): if not current_app.config.get('AWX_JOB_ENABLED', False):
current_app.logger.info('AWX job is disabled. Not sending any request.') current_app.logger.info('AWX job is disabled. Not sending any request.')
return 'AWX job not triggered' return 'AWX job not triggered'
# Launch the AWX job
resource = tower_cli.get_resource('job') resource = tower_cli.get_resource('job')
result = resource.launch(job_template=job_template, monitor=monitor, **kwargs) result = resource.launch(job_template=job_template, **kwargs)
# Save the AWX job id in the task
task = models.Task.query.get(rq_job.id)
task.awx_job_id = result['id']
db.session.commit()
# Monitor the job until done
result = resource.monitor(pk=result['id'])
return result return result
...@@ -21,7 +21,9 @@ ...@@ -21,7 +21,9 @@
<th>Id</th> <th>Id</th>
<th>Name</th> <th>Name</th>
<th>Created at</th> <th>Created at</th>
<th>Ended at</th>
<th>Status</th> <th>Status</th>
<th>AWX job</th>
<th>Command</th> <th>Command</th>
<th>User</th> <th>User</th>
</tr> </tr>
......
...@@ -18,8 +18,12 @@ ...@@ -18,8 +18,12 @@
<dd class="col-sm-9">{{ task.name }}</dd> <dd class="col-sm-9">{{ task.name }}</dd>
<dt class="col-sm-3">Created at</dt> <dt class="col-sm-3">Created at</dt>
<dd class="col-sm-9">{{ task.created_at }}</dd> <dd class="col-sm-9">{{ task.created_at }}</dd>
<dt class="col-sm-3">Ended at</dt>
<dd class="col-sm-9">{{ task.ended_at }}</dd>
<dt class="col-sm-3">Status</dt> <dt class="col-sm-3">Status</dt>
<dd class="col-sm-9">{{ task.status.name }}</dd> <dd class="col-sm-9">{{ task.status.name }}</dd>
<dt class="col-sm-3">AWX job</dt>
<dd class="col-sm-9">{{ task.awx_job_id }}</dd>
<dt class="col-sm-3">Command</dt> <dt class="col-sm-3">Command</dt>
<dd class="col-sm-9">{{ task.command }}</dd> <dd class="col-sm-9">{{ task.command }}</dd>
<dt class="col-sm-3">User</dt> <dt class="col-sm-3">User</dt>
......
"""add fields to task table
Revision ID: c0b8036078e7
Revises: 7d0d580cdb1a
Create Date: 2018-07-02 11:45:28.255006
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'c0b8036078e7'
down_revision = '7d0d580cdb1a'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('task', sa.Column('awx_job_id', sa.Integer(), nullable=True))
op.add_column('task', sa.Column('ended_at', sa.DateTime(), nullable=True))
def downgrade():
op.drop_column('task', 'ended_at')
op.drop_column('task', 'awx_job_id')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment