-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevators.js
45 lines (40 loc) · 1.65 KB
/
elevators.js
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
const config = require('config');
const { sortArray } = require('./helpers');
const { createQueue } = require('./queue');
/**
* A function to sendElevatorsToFloors as functions to execute with time elements associated
* as an actual task/job.
* @param {*} arrayOfFloors
*/
const sendElevatorToFloors = (arrayOfFloors = []) => () => {
if ((Array.isArray(arrayOfFloors) && arrayOfFloors.length === 0) || !Array.isArray(arrayOfFloors)) {
return [];
}
const sortedFloors = sortArray(arrayOfFloors, false);
const topFloor = sortedFloors.shift();
const tripTime = (config.get('application.fastSimulation')) ? (2 * parseInt(topFloor)) + parseInt(topFloor) : ((2 * parseInt(topFloor)) + parseInt(topFloor)) * 1000;
return new Promise(resolve => {
return setTimeout(() => resolve(tripTime), tripTime);
});
}
/**
* Create the elevator jobs/task queue to runElevators
* @param {*} queuedArrayCalls
* @param {*} maxNumberOfWorkers
*/
const runElevators = (queuedArrayCalls, maxNumberOfWorkers) => {
const taskQueue = [];
if ((Array.isArray(queuedArrayCalls) && queuedArrayCalls.length === 0) || !Array.isArray(queuedArrayCalls)) {
return taskQueue;
}
for(let i = 0; i < queuedArrayCalls.length; i++) {
if (process.env.NODE_ENV !== 'test') console.log(queuedArrayCalls[i]);
taskQueue.push(sendElevatorToFloors(queuedArrayCalls[i]));
}
if (process.env.NODE_ENV !== 'test') console.log(`Total number of tasks in queue: ${taskQueue.length} serviced by: ${maxNumberOfWorkers} elevators/workers`);
createQueue(taskQueue, maxNumberOfWorkers).then(result => console.log(result));
};
module.exports = {
sendElevatorToFloors,
runElevators,
}