-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.ts
107 lines (96 loc) · 2.94 KB
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import EventEmitter from "events";
import { JobId, Job } from './models';
export default class Queue extends EventEmitter {
private jobId: number;
private jobs: Job[];
private pausedJobs: Job[];
private jobTimeouts: {timeout: NodeJS.Timer, id: JobId} [];
isRunning: boolean;
constructor() {
super();
this.jobId = 0;
this.jobs = [];
this.isRunning = true;
this.jobTimeouts = [];
this.pausedJobs = [];
}
private generateJobId(): JobId {
//Logic to generate job id goes here
this.jobId++;
return this.jobId;
}
private removeTimeout(jobId: JobId) {
let index = this.jobTimeouts.findIndex(jobTimeout => jobTimeout.id == jobId);
if (index == -1) return;
clearTimeout(this.jobTimeouts[index].timeout);
this.jobTimeouts.splice(index, 1);
}
public addJob(data: any, time: Date): JobId {
//Logic to add a job goes here
let jobId: JobId = this.generateJobId();
let currentTime = new Date().getTime();
let scheduleTime = time.getTime();
let timeDiff = Math.max(0, scheduleTime - currentTime);
let job: Job = {
data,
id: jobId,
time
}
let timeout = setTimeout(() => {
this.emit("job", job.data);
let jobIndexToRemove = this.jobs.findIndex(job1 => job1.id == job.id);
this.jobs.splice(jobIndexToRemove, 1);
}, timeDiff);
this.jobs.push(job);
this.jobTimeouts.push({timeout, id: jobId});
return jobId;
}
public getJob(jobId: JobId): Job | undefined {
//Logic to get a job goes here
if (this.isRunning) {
let index = this.jobs.findIndex(job => job.id == jobId);
if (index == -1) return undefined;
return this.jobs[index];
} else {
let index = this.pausedJobs.findIndex(job => job.id == jobId);
if (index == -1) return undefined;
return this.pausedJobs[index];
}
}
public removeJob(jobId: JobId): Job | undefined {
//Logic to delete a job goes here
if (this.isRunning) {
let index = this.jobs.findIndex(job => job.id == jobId);
if (index == -1) return undefined;
this.removeTimeout(jobId);
return this.jobs.splice(index, 1)[0];
} else {
let index = this.pausedJobs.findIndex(job => job.id == jobId);
if (index == -1) return undefined;
return this.pausedJobs.splice(index, 1)[0];
}
}
public listJobs(): Array<Job> {
//Logic to get job list goes here
return this.isRunning ? this.jobs : this.pausedJobs;
}
public pauseQueue() {
//Logic to pause queue goes here
this.isRunning = false;
this.pausedJobs = [];
for (let job of this.jobs) {
this.removeTimeout(job.id);
this.pausedJobs.push(Object.assign({}, job));
}
this.jobs = [];
}
public resumeQueue() {
//Logic to resume queue goes here
this.isRunning = true;
this.jobs = [];
for (let job of this.pausedJobs) {
this.addJob(job.data, job.time);
}
this.pausedJobs = [];
}
}