Skip to content

Commit

Permalink
feat(job): restart job
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed May 23, 2017
1 parent 37936b3 commit e1022a4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/api/db/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ export function resetJobs(buildId: number): Promise<any> {
});
});
}

export function resetJob(jobId: number): Promise<any> {
return new Promise((resolve, reject) => {
const data = {
start_time: new Date(),
end_time: null,
status: 'queued',
log: ''
};

new Job({ id: jobId }).save(data, { method: 'update' }).then(job => {
if (!job) {
reject();
}

getJob(jobId).then(job => resolve(job));
});
});
}
26 changes: 25 additions & 1 deletion src/api/process-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { startBuildJob, Job, startDockerImageSetupJob } from './process';
import { Observable, Subject } from 'rxjs';
import { insertBuild, updateBuild, getBuild } from './db/build';
import { insertJob, resetJobs, updateJob } from './db/job';
import { insertJob, resetJobs, updateJob, resetJob } from './db/job';
import { getRepository } from './db/repository';
import { getRepositoryDetails, generateCommands } from './config';
import { killContainer } from './docker';
Expand Down Expand Up @@ -195,6 +195,30 @@ export function queueJob(buildId: number, jobId: number, commands: string[]): Su
return Subject.create(jobObserver, jobOutput);
}

export function restartJob(jobId: number): Promise<Subject<JobMessage>> {
stopJob(jobId);

return resetJob(jobId)
.then(job => {
console.log(job);
const commands = JSON.parse(job.commands);
return queueJob(job.build_id, jobId, commands);
});
}

export function stopJob(jobId: number): void {
const jobIndex = jobProcesses.findIndex(jobProcess => jobProcess.job_id === jobId);
if (jobIndex !== -1) {
jobProcesses[jobIndex].job.next({ action: 'exit' });
jobProcesses = jobProcesses.filter(jobProcess => jobProcess.job_id === jobId);
}
}

export function getBuildJobsData(buildId: number): Observable<JobMessage> {
const jobs = getJobsForBuild(buildId);
return Observable.merge(...jobs.map(job => job.job));
}

export function findDockerImageBuildJob(name: string): JobProcess | null {
const index = jobProcesses.findIndex(job => job.image_name && job.image_name === name);
return jobProcesses[index] || null;
Expand Down
12 changes: 9 additions & 3 deletions src/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
startBuild,
startSetup,
findDockerImageBuildJob,
getJobsForBuild
getJobsForBuild,
restartJob
} from './process-manager';
import { getBuild } from './db/build';

Expand All @@ -19,11 +20,9 @@ export interface ISocketServerOptions {
export class SocketServer {
options: ISocketServerOptions;
connections: Observable<any>;
ptyProcesses: any[];

constructor(options: ISocketServerOptions) {
this.options = options;
this.ptyProcesses = [];
}

start(): Observable<string> {
Expand Down Expand Up @@ -61,6 +60,13 @@ export class SocketServer {
case 'restartBuild':

break;
case 'restartJob':
restartJob(event.data.jobId).then(subj => {
subj.subscribe(event => {
console.log(event);
});
});
break;
}

// switch (event.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ <h3>#{{ build.id }}.{{ build.iteration }} ({{ build.uuid }})</h3>
<span>{{ job.status }}</span>
</div>
<div class="column is-1">
<button type="button" class="button green">
<button type="button" class="button green" (click)="restartJob($event, job.id)">
Restart
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export class AppBuildDetailsComponent implements OnInit {
this.socketService.emit({ type: 'restartBuild', data: buildId });
}

restartJob(e: MouseEvent, jobId: number): void {
e.preventDefault();
e.stopPropagation();

this.socketService.emit({ type: 'restartJob', data: { jobId: jobId } });
}

stopBuild(): void {
this.socketService.emit({ type: 'stopBuild', data: this.id });
}
Expand Down

0 comments on commit e1022a4

Please sign in to comment.