mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-06-04 06:28:45 -04:00
* feat: add restore agent RPC foundation * chore: temp event handlers * refactor: export restore progress from dto file
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { CronExpressionParser } from "cron-parser";
|
|
import { createBackupOptions as createAgentBackupOptions } from "../../../../apps/agent/src/commands/helpers/backup.helpers";
|
|
import type { BackupSchedule } from "~/server/db/schema";
|
|
import { toMessage } from "~/server/utils/errors";
|
|
import { logger } from "@zerobyte/core/node";
|
|
|
|
export const calculateNextRun = (cronExpression: string) => {
|
|
try {
|
|
const interval = CronExpressionParser.parse(cronExpression, {
|
|
currentDate: new Date(),
|
|
tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
});
|
|
return interval.next().getTime();
|
|
} catch (error) {
|
|
logger.error(`Failed to parse cron expression "${cronExpression}": ${toMessage(error)}`);
|
|
const fallback = new Date();
|
|
fallback.setMinutes(fallback.getMinutes() + 1);
|
|
return fallback.getTime();
|
|
}
|
|
};
|
|
|
|
export const isValidCron = (expression: string) => {
|
|
try {
|
|
CronExpressionParser.parse(expression);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export const createBackupOptions = (schedule: BackupSchedule, volumePath: string, signal?: AbortSignal) =>
|
|
createAgentBackupOptions(
|
|
{
|
|
scheduleId: schedule.shortId,
|
|
options: {
|
|
oneFileSystem: schedule.oneFileSystem,
|
|
excludePatterns: schedule.excludePatterns,
|
|
excludeIfPresent: schedule.excludeIfPresent,
|
|
includePaths: schedule.includePaths,
|
|
includePatterns: schedule.includePatterns,
|
|
customResticParams: schedule.customResticParams,
|
|
compressionMode: "auto",
|
|
},
|
|
},
|
|
volumePath,
|
|
signal,
|
|
);
|