update: remove maxInterval in scheduler
fix: unlinking a file without handling the case of file not exist
This commit is contained in:
parent
fb0c60a71e
commit
94bd14db52
@ -83,7 +83,9 @@ function deleteEncodedScreenshots() {
|
|||||||
const frames = stmt.all() as Frame[];
|
const frames = stmt.all() as Frame[];
|
||||||
for (const frame of frames) {
|
for (const frame of frames) {
|
||||||
if (!frame.imgFilename) continue;
|
if (!frame.imgFilename) continue;
|
||||||
fs.unlinkSync(path.join(getScreenshotsDir(), frame.imgFilename));
|
const imgPath = path.join(getScreenshotsDir(), frame.imgFilename);
|
||||||
|
if (!fs.existsSync(imgPath)) return;
|
||||||
|
fs.unlinkSync(imgPath);
|
||||||
const updateStmt = db.prepare(`
|
const updateStmt = db.prepare(`
|
||||||
UPDATE frame SET imgFilename = NULL WHERE id = ?;
|
UPDATE frame SET imgFilename = NULL WHERE id = ?;
|
||||||
`);
|
`);
|
||||||
|
@ -5,7 +5,6 @@ interface Task {
|
|||||||
id: TaskId;
|
id: TaskId;
|
||||||
func: TaskFunction;
|
func: TaskFunction;
|
||||||
interval?: number;
|
interval?: number;
|
||||||
maxInterval?: number;
|
|
||||||
lastRun?: number;
|
lastRun?: number;
|
||||||
nextRun?: number;
|
nextRun?: number;
|
||||||
isPaused: boolean;
|
isPaused: boolean;
|
||||||
@ -21,8 +20,6 @@ export interface TaskStatus {
|
|||||||
export class Scheduler {
|
export class Scheduler {
|
||||||
private tasks: Map<TaskId, Task> = new Map();
|
private tasks: Map<TaskId, Task> = new Map();
|
||||||
private timer: NodeJS.Timeout | null = null;
|
private timer: NodeJS.Timeout | null = null;
|
||||||
private nextTickTime: number | null = null;
|
|
||||||
|
|
||||||
constructor(private readonly minTickInterval: number = 500) {
|
constructor(private readonly minTickInterval: number = 500) {
|
||||||
this.start();
|
this.start();
|
||||||
}
|
}
|
||||||
@ -76,16 +73,6 @@ export class Scheduler {
|
|||||||
task.nextRun = now + task.interval!;
|
task.nextRun = now + task.interval!;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isTaskReadyForMaxIntervalRun =
|
|
||||||
task.maxInterval && task.lastRun && now - task.lastRun >= task.maxInterval;
|
|
||||||
if (isTaskReadyForMaxIntervalRun) {
|
|
||||||
task.func();
|
|
||||||
task.lastRun = now;
|
|
||||||
if (task.interval) {
|
|
||||||
task.nextRun = now + task.interval;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const isTaskNextRunEarlierThanNextTick = task.nextRun && task.nextRun < getNextTick();
|
const isTaskNextRunEarlierThanNextTick = task.nextRun && task.nextRun < getNextTick();
|
||||||
if (isTaskNextRunEarlierThanNextTick) {
|
if (isTaskNextRunEarlierThanNextTick) {
|
||||||
updateNextTick(task.nextRun!);
|
updateNextTick(task.nextRun!);
|
||||||
@ -113,15 +100,12 @@ export class Scheduler {
|
|||||||
* @param id A unique string identifier for the task.
|
* @param id A unique string identifier for the task.
|
||||||
* @param func The function to be executed by the task.
|
* @param func The function to be executed by the task.
|
||||||
* @param interval The interval (in milliseconds) between task executions.
|
* @param interval The interval (in milliseconds) between task executions.
|
||||||
* @param maxInterval The maximum time (in milliseconds) that a task can wait before being executed.
|
|
||||||
* If a task has not been executed in this amount of time, it will be executed immediately.
|
|
||||||
*/
|
*/
|
||||||
addTask(id: TaskId, func: TaskFunction, interval?: number, maxInterval?: number): void {
|
addTask(id: TaskId, func: TaskFunction, interval?: number): void {
|
||||||
this.tasks.set(id, {
|
this.tasks.set(id, {
|
||||||
id,
|
id,
|
||||||
func,
|
func,
|
||||||
interval,
|
interval,
|
||||||
maxInterval,
|
|
||||||
isPaused: false,
|
isPaused: false,
|
||||||
lastRun: undefined,
|
lastRun: undefined,
|
||||||
nextRun: interval ? Date.now() + interval : undefined
|
nextRun: interval ? Date.now() + interval : undefined
|
||||||
@ -165,7 +149,7 @@ export class Scheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resume a paused task, so that it can be executed according to its interval and maxInterval.
|
* Resume a paused task, so that it can be executed according to its interval.
|
||||||
*
|
*
|
||||||
* @param id The unique string identifier for the task.
|
* @param id The unique string identifier for the task.
|
||||||
*/
|
*/
|
||||||
|
@ -110,10 +110,10 @@ app.on("ready", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
initDatabase().then((db) => {
|
initDatabase().then((db) => {
|
||||||
scheduler.addTask("screenshot", takeScreenshot, 2000, 2000);
|
scheduler.addTask("screenshot", takeScreenshot, 2000);
|
||||||
scheduler.addTask("check-encoding", checkFramesForEncoding, 5000, 10000);
|
scheduler.addTask("check-encoding", checkFramesForEncoding, 5000);
|
||||||
scheduler.addTask("process-encoding", processEncodingTasks, 10000, 30000);
|
scheduler.addTask("process-encoding", processEncodingTasks, 10000);
|
||||||
scheduler.addTask("delete-screenshots", deleteUnnecessaryScreenshots, 20000, 60000);
|
scheduler.addTask("delete-screenshots", deleteUnnecessaryScreenshots, 20000);
|
||||||
dbConnection = db;
|
dbConnection = db;
|
||||||
cache.put("server:dbConnection", dbConnection);
|
cache.put("server:dbConnection", dbConnection);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user