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