Permalink
Browse files

fix(): update build end_time field when last job finish successfully …

…and fix build time issue
  • Loading branch information...
Izak88 committed Aug 11, 2017
1 parent fad6de8 commit e0eb1dfeac0be82f770fd2e76e27d29a00e99895
@@ -1,4 +1,5 @@
import { Build, BuildRun } from './model';
import { Build, BuildRun, Job } from './model';
import { getLastRun } from './job';

export function getBuilds(limit: number, offset: number): Promise<any> {
return new Promise((resolve, reject) => {
@@ -64,6 +65,20 @@ export function getBuild(id: number): Promise<any> {
});
}

export function getLastRunId(buildId: number): Promise<any> {
return new Promise((resolve, reject) => {
new Build({ id: buildId }).fetch({ withRelated: ['runs'] })
.then(build => {
if (!build) {
reject();
}
const runs = build.related('runs').toJSON();

resolve (runs[runs.length - 1].id);
});
});
}

export function insertBuild(data: any): Promise<any> {
return new Promise((resolve, reject) => {
new Build().save(data, { method: 'insert' }).then(build => {
@@ -91,3 +106,15 @@ export function updateBuild(data: any): Promise<boolean> {
});
});
}

export function getBuildStatus(buildId: number): Promise<any> {
return new Promise((resolve, reject) => {
new Job()
.query(q => q.where('builds_id', buildId))
.fetchAll()
.then(jobs => {
Promise.all(jobs.map(j => getLastRun(j.id).then(r => r.status === 'success')))
.then(data => resolve(data.reduce((curr, prev) => !curr ? curr : prev)));
});
});
}
@@ -54,6 +54,20 @@ export function getLastRunId(jobId: number): Promise<any> {
});
}

export function getLastRun(jobId: number): Promise<any> {
return new Promise((resolve, reject) => {
new Job({ id: jobId }).fetch({ withRelated: ['runs'] })
.then(job => {
if (!job) {
reject();
}
const runs = job.related('runs').toJSON();

resolve (runs[runs.length - 1]);
});
});
}

export function insertJob(data: any): Promise<any> {
return new Promise((resolve, reject) => {
new Job(data).save(null, { method: 'insert' }).then(job => {
@@ -17,7 +17,7 @@ export class Build extends Bookshelf.Model<any> {
get hasTimestamps() { return true; }
repository() { return this.belongsTo(Repository, 'repositories_id'); }
jobs() { return this.hasMany(Job, 'builds_id'); }
runs() { return this.hasMany(BuildRun, 'builds_id'); }
runs() { return this.hasMany(BuildRun, 'build_id'); }
}

export class BuildRun extends Bookshelf.Model<any> {
@@ -1,7 +1,7 @@
import { startDockerImageSetupJob, startBuildProcess } from './process';
import { Observable, Subject, BehaviorSubject, Subscription } from 'rxjs';
import { insertBuild, updateBuild, getBuild } from './db/build';
import { insertBuildRun } from './db/build-run';
import { insertBuild, updateBuild, getBuild, getBuildStatus, getLastRunId } from './db/build';
import { insertBuildRun, updateBuildRun } from './db/build-run';
import * as dbJob from './db/job';
import * as dbJobRuns from './db/job-run';
import { getRepositoryOnly } from './db/repository';
@@ -315,10 +315,20 @@ function prepareJob(buildId: number, jobId: number, cmds: any, sshAndVnc = false
});
});
}, () => {

dbJob.getLastRunId(jobId)
.then(runId => dbJobRuns.updateJobRun(
{id: runId, end_time: new Date(), status: 'success', log: process.log.join('')}))
.then(() => getBuildStatus(buildId))
.then(status => {
if (status) {
updateBuild({ id: buildId, end_time: new Date()});
getLastRunId(buildId)
.then(id => {
updateBuildRun({ id: id, end_time: new Date()});
});
}
Promise.resolve();
})
.then(() => {
processes = processes.filter(proc => proc.job_id !== jobId);
jobProcesses.next(processes);
@@ -66,8 +66,8 @@ <h2>{{ build?.message }}</h2>
<span>{{ totalTime }}</span>
</p>
</div>
<div class="column is-12" *ngIf="status === 'running' && build.lastBuild">
<span>approximately {{ approximatelyRemainingTime }} remaining</span>
<div class="column is-12" *ngIf="status === 'running' && approximatelyRemainingTime">
<span name="time-left">approximately {{ approximatelyRemainingTime }} remaining</span>
</div>
</div>
</div>
@@ -95,7 +95,7 @@ export class AppBuildDetailsComponent implements OnInit {
});

let runningTime = Math.max(...this.build.jobs.map(job => {
let date = new Date();
let date = new Date(0);
let splitted = job.time.split(':');
date.setUTCMinutes(splitted[0]);
date.setUTCSeconds(splitted[1]);
@@ -144,6 +144,11 @@ export class AppBuildDetailsComponent implements OnInit {
e.preventDefault();
e.stopPropagation();

if (this.getBuildStatus() === 'success') {
let minJobStartTime = Math.min(...this.build.jobs.map(job => job.start_time));
let maxJobEndTime = Math.max(...this.build.jobs.map(job => job.end_time));
this.previousRuntime = maxJobEndTime - minJobStartTime;
}
this.processingBuild = true;
this.socketService.emit({ type: 'restartBuild', data: { buildId: id } });
}

0 comments on commit e0eb1df

Please sign in to comment.