Limit number of tasks in queue

This commit is contained in:
Zaid Abdulla committed 2020-09-28 12:47:15 -07:00
1 parent 01e01dae69
commit 1956441d52
4 files changed
+44 -16

No files matched your search

+2 -1
View File
@@ -81,7 +81,8 @@ const component: ClosureComponent = (): Component => {
store.fulfill(0, Date.now());
})
.catch((err) => {
throw err;
e.target.disabled = false;
notifications.push("error", err.message);
});
},
},
+10 -12
View File
@@ -23,7 +23,7 @@ import config from "./config";
import indexTableComponent from "./index-table-component";
import filterComponent from "./filter-component";
import * as store from "./store";
import * as taskQueue from "./task-queue";
import { queueTask, stageDownload } from "./task-queue";
import * as notifications from "./notifications";
import { parse, stringify } from "../lib/common/expression-parser";
import { evaluate, extractParams } from "../lib/common/expression";
@@ -91,12 +91,11 @@ function renderActions(selected: Set<string>): Children {
title: "Reboot selected devices",
disabled: !selected.size,
onclick: () => {
for (const d of selected) {
taskQueue.queueTask({
const tasks = [...selected].map((s) => ({
name: "reboot",
device: d,
});
}
device: s,
}));
queueTask(...tasks);
},
},
"Reboot"
@@ -110,12 +109,11 @@ function renderActions(selected: Set<string>): Children {
title: "Factory reset selected devices",
disabled: !selected.size,
onclick: () => {
for (const d of selected) {
taskQueue.queueTask({
const tasks = [...selected].map((s) => ({
name: "factoryReset",
device: d,
});
}
device: s,
}));
queueTask(...tasks);
},
},
"Reset"
@@ -129,7 +127,7 @@ function renderActions(selected: Set<string>): Children {
title: "Push a firmware or a config file",
disabled: !selected.size,
onclick: () => {
taskQueue.stageDownload({
stageDownload({
name: "download",
devices: [...selected],
});
+1 -1
View File
@@ -468,7 +468,7 @@ const component: ClosureComponent = (): Component => {
store.fulfill(0, Date.now());
})
.catch((err) => {
throw err;
notifications.push("error", err.message);
});
},
},
+31 -2
View File
@@ -19,6 +19,7 @@
import * as store from "./store";
import { Task } from "../lib/types";
import * as notifications from "./notifications";
export interface QueueTask extends Task {
status?: string;
@@ -29,13 +30,28 @@ export interface StageTask extends Task {
devices: string[];
}
const MAX_QUEUE = 100;
const queue: Set<QueueTask> = new Set();
const staging: Set<StageTask> = new Set();
export function queueTask(task: QueueTask): void {
function canQueue(tasks: QueueTask[]): boolean {
let count = queue.size;
for (const task of tasks) if (!queue.has(task)) ++count;
return count <= MAX_QUEUE;
}
export function queueTask(...tasks: QueueTask[]): void {
if (!canQueue(tasks)) {
notifications.push("error", "Too many tasks in queue");
return;
}
for (const task of tasks) {
task.status = "queued";
queue.add(task);
}
}
export function deleteTask(task: QueueTask): void {
queue.delete(task);
@@ -58,10 +74,18 @@ export function clearStaging(): void {
}
export function stageSpv(task: StageTask): void {
if (queue.size + task.devices.length > MAX_QUEUE) {
notifications.push("error", "Too many tasks in queue");
return;
}
staging.add(task);
}
export function stageDownload(task: StageTask): void {
if (queue.size + task.devices.length > MAX_QUEUE) {
notifications.push("error", "Too many tasks in queue");
return;
}
staging.add(task);
}
@@ -75,10 +99,15 @@ export function commit(
) => void
): Promise<void> {
const devices: { [deviceId: string]: any[] } = {};
if (!canQueue(tasks))
return Promise.reject(new Error("Too many tasks in queue"));
for (const t of tasks) {
devices[t.device] = devices[t.device] || [];
devices[t.device].push(t);
queueTask(t);
t.status = "queued";
queue.add(t);
}
return new Promise((resolve) => {