-
Notifications
You must be signed in to change notification settings - Fork 5
/
DeadlineScheduler.ts
57 lines (49 loc) · 1.56 KB
/
DeadlineScheduler.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
import { Scheduler, TaskState } from "./Scheduler"
import { Task, TaskStatus } from "./Task"
import { first, sortBy, concat, pullAt, findIndex } from "lodash"
const PriorityQueue = require("fastpriorityqueue")
/**
* Deadline based scheduler implementation, based on Scheduler
* implements EDF scheme
*/
export class DeadlineScheduler extends Scheduler {
/**
* queue used for managing the tasks, sorted by the deadline-member of the task structures
*/
private taskQueue: PriorityQueue<TaskState> = new PriorityQueue((a: TaskState, b: TaskState) => {
return b.absoluteDeadline > a.absoluteDeadline
})
/**
* enqueues a task into the queue
*/
enqueueTask(task: TaskState) {
this.forceReschedule = true
this.taskQueue.add(task)
}
emptyTaskQueue() {
while (this.taskQueue.poll() !== undefined);
}
/**
* determines, whether or not the scheduler needs to reshedule, based on the time passed
*/
needsReschedule(): boolean {
if (!this.currentTask || !this.currentTask.isRunnable) { return true }
let el = this.taskQueue.peek()
if (el && el.absoluteDeadline < this.currentTask!.absoluteDeadline) {
return true
}
return false
}
/**
* returns the number of tasks in the queue
*/
get numberOfTasks(): number {
return this.taskQueue.size
}
/**
* returns the next task to run
*/
protected getNextTask(): TaskState | undefined {
return this.taskQueue.removeOne(t => t.isRunnable)
}
}