update: remove maxInterval in scheduler

fix: unlinking a file without handling the case of file not exist
This commit is contained in:
alikia2x (寒寒) 2025-01-21 02:12:17 +08:00
parent fb0c60a71e
commit 94bd14db52
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
3 changed files with 9 additions and 23 deletions

View File

@ -83,7 +83,9 @@ function deleteEncodedScreenshots() {
const frames = stmt.all() as Frame[];
for (const frame of frames) {
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(`
UPDATE frame SET imgFilename = NULL WHERE id = ?;
`);

View File

@ -5,7 +5,6 @@ interface Task {
id: TaskId;
func: TaskFunction;
interval?: number;
maxInterval?: number;
lastRun?: number;
nextRun?: number;
isPaused: boolean;
@ -21,8 +20,6 @@ export interface TaskStatus {
export class Scheduler {
private tasks: Map<TaskId, Task> = new Map();
private timer: NodeJS.Timeout | null = null;
private nextTickTime: number | null = null;
constructor(private readonly minTickInterval: number = 500) {
this.start();
}
@ -76,16 +73,6 @@ export class Scheduler {
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();
if (isTaskNextRunEarlierThanNextTick) {
updateNextTick(task.nextRun!);
@ -113,15 +100,12 @@ export class Scheduler {
* @param id A unique string identifier for the task.
* @param func The function to be executed by the task.
* @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, {
id,
func,
interval,
maxInterval,
isPaused: false,
lastRun: 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.
*/

View File

@ -110,10 +110,10 @@ app.on("ready", () => {
});
});
initDatabase().then((db) => {
scheduler.addTask("screenshot", takeScreenshot, 2000, 2000);
scheduler.addTask("check-encoding", checkFramesForEncoding, 5000, 10000);
scheduler.addTask("process-encoding", processEncodingTasks, 10000, 30000);
scheduler.addTask("delete-screenshots", deleteUnnecessaryScreenshots, 20000, 60000);
scheduler.addTask("screenshot", takeScreenshot, 2000);
scheduler.addTask("check-encoding", checkFramesForEncoding, 5000);
scheduler.addTask("process-encoding", processEncodingTasks, 10000);
scheduler.addTask("delete-screenshots", deleteUnnecessaryScreenshots, 20000);
dbConnection = db;
cache.put("server:dbConnection", dbConnection);
});