-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskRunner.js
More file actions
200 lines (157 loc) · 5.78 KB
/
Copy pathTaskRunner.js
File metadata and controls
200 lines (157 loc) · 5.78 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const TaskRunner = (function () {
/* private member function */
function canTaskExecute() {
return this.runningTasks < this.concurrency && this.taskQueue.length > 0;
}
function updateRunningTasksCount(tasksCount) {
getNode('#runningTasksCount').textContent = tasksCount;
}
function insertTaskIntoQueueUI(task) {
getNode('#taskQueue').appendChild(
createNode('div', { class: 'task flex justify-center align-center', content: task.id })
)
}
function removeTaskFromQueueUI(task) {
getNode(`#taskQueue #T${task.id}`).classList.add('remove');
setTimeout((task) => {
getNode(`#taskQueue #T${task.id}`).remove();
}, 2000, task)
}
function updateTotalTasksInQueueCount(tasksCount) {
getNode('#totalTasks').textContent = tasksCount;
}
function insertTaskIntoCallStackUI(task) {
getNode('#callStack').appendChild(
createNode('div', { class: 'task flex justify-center align-center insert', content: task.id })
)
}
function removeTaskFromCallStackUI(task) {
getNode(`#callStack #T${task.id}`).classList.remove('insert');
getNode(`#callStack #T${task.id}`).classList.add('remove');
setTimeout((task) => {
getNode(`#callStack #T${task.id}`).remove();
}, 2000, task)
}
/* private member function ends */
function TaskRunner(concurrency = 3, runnerCycle = 0.5) {
this.taskQueue = [];
this.concurrency = concurrency;
this.runningTasks = 0;
this.runnerCycle = runnerCycle;
this.startTaskRunner();
}
TaskRunner.prototype.push = function (task) {
this.taskQueue.push(task);
this.updateUI('taskQueue', { data: task, insert: true });
this.updateUI('totalTasks', { data: this.taskQueue.length });
};
TaskRunner.prototype.setRunnerActive = function () {
this.taskRunnerActive = true;
getNode('#runnerActiveChip').classList.add('active');
getNode('#runnerStoppedChip').classList.remove('active');
}
TaskRunner.prototype.updateUI = function (updateElement, nodeData) {
switch (updateElement) {
case 'runningTasksCount': updateRunningTasksCount(this.runningTasks); break;
case 'taskQueue':
nodeData.insert && insertTaskIntoQueueUI(nodeData.data);
nodeData.remove && removeTaskFromQueueUI(nodeData.data);
break;
case 'totalTasks': updateTotalTasksInQueueCount(nodeData.data); break;
case 'callStack':
nodeData.insert && insertTaskIntoCallStackUI(nodeData.data);
nodeData.remove && removeTaskFromCallStackUI(nodeData.data);
break;
}
}
TaskRunner.prototype.constructor = function () {
this.setRunnerActive();
function asyncTaskRunner() {
if (this.runningTasks < this.concurrency && this.taskQueue.length > 0) {
// threads are available to execute the task
const taskToExecute = this.taskQueue.shift();
this.updateUI('taskQueue', { data: taskToExecute, remove: true });
this.updateUI('totalTasks', { data: this.taskQueue.length });
this.runningTasks++;
this.updateUI('runningTasksCount');
console.log(
`Lock ${this.runningTasks} acquired by task - `,
taskToExecute.displayName || taskToExecute.name
);
taskToExecute.timer &&
console.log(
taskToExecute.displayName || taskToExecute.name,
"Timer = ",
taskToExecute.timer
);
this.updateUI('callStack', { data: taskToExecute, insert: true });
taskToExecute().finally(() => {
const lockId = this.runningTasks;
this.runningTasks--;
this.updateUI('runningTasksCount');
this.updateUI('callStack', { data: taskToExecute, remove: true });
console.log(
`Lock ${lockId} released by task - `,
taskToExecute.displayName || taskToExecute.name
);
});
} else {
this.runningTasks < this.concurrency
? console.log("Task queue is empty.")
: console.log("All threads are busy, waiting to acquire the lock");
}
}
this.runnerInstanceId = setInterval(
asyncTaskRunner.bind(this),
this.runnerCycle * 1000
);
};
TaskRunner.prototype.stopTaskRunner = function () {
this.taskRunnerActive = false;
clearInterval(this.runnerInstanceId);
getNode('#runnerActiveChip').classList.remove('active');
getNode('#runnerStoppedChip').classList.add('active');
console.log("Runner stopped!!");
};
TaskRunner.prototype.startTaskRunner = function () {
console.log("Runner starting...");
!this.taskRunnerActive && this.constructor();
}
return TaskRunner;
})();
/**
* ======================================================
* ======================================================
* ======================================================
*/
const tasks = new TaskRunner(3, 0.25);
// Below code is to create the task and push it in the queue
function createTask(id, time = 0.5) {
task.displayName = `Task${id}`;
task.timer = time;
task.id = id;
function task() {
console.log(`Running task ${id}`);
return new Promise((resolve, reject) => {
setTimeout(() => {
const taskId = id;
console.log(`Task ${taskId} executed.`);
resolve(`Task ${taskId} executed.`);
}, time * 1000);
});
}
return task;
}
setTimeout(() => {
console.log("New tasks feeded to runner.");
const getRandomTimeDistibution = () => (Math.random() * 5.5).toFixed(2);
for (let i = 1; i < 10 + 7; i++) {
tasks.push(createTask(i, getRandomTimeDistibution()));
}
}, 1 * 1000);
console.log("-- Tasks feeding to TaskRunner complete. --\n");
// Attaching handlers to actions
getNode('#runnerStopBtn')
.addEventListener("click", () => tasks.stopTaskRunner());
getNode('#runnerStartBtn')
.addEventListener('click', () => tasks.startTaskRunner());