Compare commits
2 commits
main
...
v0.29.3-be
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95b77aea97 | ||
|
|
2a332fa4fa |
4 changed files with 84 additions and 8 deletions
|
|
@ -75,7 +75,7 @@ export const PathsSection = ({
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Textarea
|
<Textarea
|
||||||
{...field}
|
{...field}
|
||||||
placeholder="/data/** /config/*.json *.db"
|
placeholder="/data/** /config/*.json *.db **/*.log"
|
||||||
className="font-mono text-sm min-h-25"
|
className="font-mono text-sm min-h-25"
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import cron, { type ScheduledTask } from "node-cron";
|
import CronExpressionParser from "cron-parser";
|
||||||
import { logger } from "../utils/logger";
|
import { logger } from "../utils/logger";
|
||||||
|
|
||||||
export abstract class Job {
|
export abstract class Job {
|
||||||
|
|
@ -7,6 +7,73 @@ export abstract class Job {
|
||||||
|
|
||||||
type JobConstructor = new () => Job;
|
type JobConstructor = new () => Job;
|
||||||
|
|
||||||
|
class ScheduledTask {
|
||||||
|
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
private active = true;
|
||||||
|
private running = false;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly jobName: string,
|
||||||
|
private readonly cronExpression: string,
|
||||||
|
private readonly run: () => Promise<void>,
|
||||||
|
) {
|
||||||
|
this.scheduleNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private getDelay(fromDate: Date) {
|
||||||
|
try {
|
||||||
|
const interval = CronExpressionParser.parse(this.cronExpression, {
|
||||||
|
currentDate: fromDate,
|
||||||
|
tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||||
|
});
|
||||||
|
const nextRun = interval.next().toDate();
|
||||||
|
return Math.max(0, nextRun.getTime() - Date.now());
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`Failed to parse cron expression for ${this.jobName}:`, error);
|
||||||
|
return 60_000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private scheduleNext(fromDate = new Date()) {
|
||||||
|
if (!this.active) return;
|
||||||
|
|
||||||
|
const delay = this.getDelay(fromDate);
|
||||||
|
this.timer = setTimeout(() => {
|
||||||
|
void this.tick();
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async tick() {
|
||||||
|
if (!this.active) return;
|
||||||
|
|
||||||
|
if (this.running) {
|
||||||
|
logger.warn(`Skipping overlapping run for job ${this.jobName}`);
|
||||||
|
this.scheduleNext(new Date());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.running = true;
|
||||||
|
try {
|
||||||
|
await this.run();
|
||||||
|
} finally {
|
||||||
|
this.running = false;
|
||||||
|
this.scheduleNext(new Date());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async stop() {
|
||||||
|
this.active = false;
|
||||||
|
if (this.timer) {
|
||||||
|
clearTimeout(this.timer);
|
||||||
|
this.timer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async destroy() {
|
||||||
|
await this.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class SchedulerClass {
|
class SchedulerClass {
|
||||||
private tasks: ScheduledTask[] = [];
|
private tasks: ScheduledTask[] = [];
|
||||||
|
|
||||||
|
|
@ -18,7 +85,7 @@ class SchedulerClass {
|
||||||
const job = new JobClass();
|
const job = new JobClass();
|
||||||
return {
|
return {
|
||||||
schedule: (cronExpression: string) => {
|
schedule: (cronExpression: string) => {
|
||||||
const task = cron.schedule(cronExpression, async () => {
|
const task = new ScheduledTask(JobClass.name, cronExpression, async () => {
|
||||||
try {
|
try {
|
||||||
await job.run();
|
await job.run();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,12 @@ describe("executeBackup - include / exclude patterns", () => {
|
||||||
expect.anything(),
|
expect.anything(),
|
||||||
volumePath,
|
volumePath,
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
include: ["*.zip", path.join(volumePath, "Photos"), `!${path.join(volumePath, "Temp")}`, "!*.log"],
|
include: [
|
||||||
|
path.join(volumePath, "*.zip"),
|
||||||
|
path.join(volumePath, "Photos"),
|
||||||
|
`!${path.join(volumePath, "Temp")}`,
|
||||||
|
`!${path.join(volumePath, "*.log")}`,
|
||||||
|
],
|
||||||
exclude: [".DS_Store", path.join(volumePath, "Config"), `!${path.join(volumePath, "Important")}`, "!*.tmp"],
|
exclude: [".DS_Store", path.join(volumePath, "Config"), `!${path.join(volumePath, "Important")}`, "!*.tmp"],
|
||||||
excludeIfPresent: [".nobackup"],
|
excludeIfPresent: [".nobackup"],
|
||||||
}),
|
}),
|
||||||
|
|
@ -109,7 +114,7 @@ describe("executeBackup - include / exclude patterns", () => {
|
||||||
expect.anything(),
|
expect.anything(),
|
||||||
volumePath,
|
volumePath,
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
include: [relativeInclude, path.join(volumePath, "anchored/include")],
|
include: [path.join(volumePath, relativeInclude), path.join(volumePath, "anchored/include")],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,14 @@ export const calculateNextRun = (cronExpression: string) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const processPattern = (pattern: string, volumePath: string) => {
|
export const processPattern = (pattern: string, volumePath: string, relative = false) => {
|
||||||
const isNegated = pattern.startsWith("!");
|
const isNegated = pattern.startsWith("!");
|
||||||
const p = isNegated ? pattern.slice(1) : pattern;
|
const p = isNegated ? pattern.slice(1) : pattern;
|
||||||
|
|
||||||
if (!p.startsWith("/")) {
|
if (!p.startsWith("/")) {
|
||||||
return pattern;
|
if (!relative) return pattern;
|
||||||
|
const processed = path.join(volumePath, p);
|
||||||
|
return isNegated ? `!${processed}` : processed;
|
||||||
}
|
}
|
||||||
|
|
||||||
const processed = path.join(volumePath, p.slice(1));
|
const processed = path.join(volumePath, p.slice(1));
|
||||||
|
|
@ -37,5 +39,7 @@ export const createBackupOptions = (schedule: BackupSchedule, volumePath: string
|
||||||
signal,
|
signal,
|
||||||
exclude: schedule.excludePatterns ? schedule.excludePatterns.map((p) => processPattern(p, volumePath)) : undefined,
|
exclude: schedule.excludePatterns ? schedule.excludePatterns.map((p) => processPattern(p, volumePath)) : undefined,
|
||||||
excludeIfPresent: schedule.excludeIfPresent ?? undefined,
|
excludeIfPresent: schedule.excludeIfPresent ?? undefined,
|
||||||
include: schedule.includePatterns ? schedule.includePatterns.map((p) => processPattern(p, volumePath)) : undefined,
|
include: schedule.includePatterns
|
||||||
|
? schedule.includePatterns.map((p) => processPattern(p, volumePath, true))
|
||||||
|
: undefined,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue