refactor: simplify hooks ceremonies
This commit is contained in:
parent
687810fb13
commit
95067c39fa
23 changed files with 2954 additions and 3163 deletions
|
|
@ -2434,15 +2434,17 @@ export type ListBackupSchedulesResponses = {
|
|||
includePatterns: Array<string> | null;
|
||||
oneFileSystem: boolean;
|
||||
customResticParams: Array<string> | null;
|
||||
preBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries: number;
|
||||
retryDelay: number;
|
||||
|
|
@ -2722,15 +2724,17 @@ export type CreateBackupScheduleData = {
|
|||
oneFileSystem?: boolean;
|
||||
tags?: Array<string>;
|
||||
customResticParams?: Array<string>;
|
||||
preBackupWebhook?: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook?: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks?: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries?: number;
|
||||
retryDelay?: number;
|
||||
|
|
@ -2767,15 +2771,17 @@ export type CreateBackupScheduleResponses = {
|
|||
includePatterns: Array<string> | null;
|
||||
oneFileSystem: boolean;
|
||||
customResticParams: Array<string> | null;
|
||||
preBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries: number;
|
||||
retryDelay: number;
|
||||
|
|
@ -2846,15 +2852,17 @@ export type GetBackupScheduleResponses = {
|
|||
includePatterns: Array<string> | null;
|
||||
oneFileSystem: boolean;
|
||||
customResticParams: Array<string> | null;
|
||||
preBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries: number;
|
||||
retryDelay: number;
|
||||
|
|
@ -3133,15 +3141,17 @@ export type UpdateBackupScheduleData = {
|
|||
oneFileSystem?: boolean;
|
||||
tags?: Array<string>;
|
||||
customResticParams?: Array<string>;
|
||||
preBackupWebhook?: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook?: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks?: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries?: number;
|
||||
retryDelay?: number;
|
||||
|
|
@ -3180,15 +3190,17 @@ export type UpdateBackupScheduleResponses = {
|
|||
includePatterns: Array<string> | null;
|
||||
oneFileSystem: boolean;
|
||||
customResticParams: Array<string> | null;
|
||||
preBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries: number;
|
||||
retryDelay: number;
|
||||
|
|
@ -3239,15 +3251,17 @@ export type GetBackupScheduleForVolumeResponses = {
|
|||
includePatterns: Array<string> | null;
|
||||
oneFileSystem: boolean;
|
||||
customResticParams: Array<string> | null;
|
||||
preBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
postBackupWebhook: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
backupWebhooks: {
|
||||
pre: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
post: {
|
||||
url: string;
|
||||
headers?: Array<string>;
|
||||
body?: string;
|
||||
} | null;
|
||||
} | null;
|
||||
maxRetries: number;
|
||||
retryDelay: number;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,72 @@ type AdvancedSectionProps = {
|
|||
form: UseFormReturn<InternalFormValues>;
|
||||
};
|
||||
|
||||
type WebhookFieldsProps = {
|
||||
form: UseFormReturn<InternalFormValues>;
|
||||
phase: "pre" | "post";
|
||||
urlPlaceholder: string;
|
||||
bodyPlaceholder: string;
|
||||
description: string;
|
||||
};
|
||||
|
||||
const WebhookFields = ({ form, phase, urlPlaceholder, bodyPlaceholder, description }: WebhookFieldsProps) => {
|
||||
const labelPrefix = phase === "pre" ? "Pre-backup" : "Post-backup";
|
||||
const fieldPrefix = phase === "pre" ? "preBackupWebhook" : "postBackupWebhook";
|
||||
|
||||
return (
|
||||
<>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`${fieldPrefix}Url`}
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{labelPrefix} webhook</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} type="url" placeholder={urlPlaceholder} />
|
||||
</FormControl>
|
||||
<FormDescription>{description}</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`${fieldPrefix}HeadersText`}
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{labelPrefix} webhook headers</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
{...field}
|
||||
placeholder="Authorization: Bearer token X-Custom-Header: value"
|
||||
className="font-mono text-sm min-h-24"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
One header per line in Key: Value format. Values are stored as plain text.
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name={`${fieldPrefix}Body`}
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{labelPrefix} webhook body</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea {...field} placeholder={bodyPlaceholder} className="font-mono text-sm min-h-24" />
|
||||
</FormControl>
|
||||
<FormDescription>Optional raw POST body. Leave empty to send the backup context JSON.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export const AdvancedSection = ({ form }: AdvancedSectionProps) => {
|
||||
return (
|
||||
<>
|
||||
|
|
@ -56,109 +122,19 @@ export const AdvancedSection = ({ form }: AdvancedSectionProps) => {
|
|||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="preBackupWebhookUrl"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Pre-backup webhook</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} type="url" placeholder="http://host.docker.internal:8080/stop" />
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
Called with POST before restic starts. A non-2xx response stops the backup.
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
<WebhookFields
|
||||
form={form}
|
||||
phase="pre"
|
||||
urlPlaceholder="http://host.docker.internal:8080/stop"
|
||||
bodyPlaceholder='{"action":"stop"}'
|
||||
description="Called with POST before restic starts. A non-2xx response stops the backup."
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="preBackupWebhookHeaders"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Pre-backup webhook headers</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
{...field}
|
||||
placeholder="Authorization: Bearer token X-Custom-Header: value"
|
||||
value={Array.isArray(field.value) ? field.value.join("\n") : ""}
|
||||
onChange={(e) => field.onChange(e.target.value.split("\n"))}
|
||||
className="font-mono text-sm min-h-24"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
One header per line in Key: Value format. Values are stored as plain text.
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="preBackupWebhookBody"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Pre-backup webhook body</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea {...field} placeholder='{"action":"stop"}' className="font-mono text-sm min-h-24" />
|
||||
</FormControl>
|
||||
<FormDescription>Optional raw POST body. Leave empty to send the backup context JSON.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="postBackupWebhookUrl"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Post-backup webhook</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} type="url" placeholder="http://host.docker.internal:8080/start" />
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
Called with POST after restic finishes, including failed or cancelled runs.
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="postBackupWebhookHeaders"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Post-backup webhook headers</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
{...field}
|
||||
placeholder="Authorization: Bearer token X-Custom-Header: value"
|
||||
value={Array.isArray(field.value) ? field.value.join("\n") : ""}
|
||||
onChange={(e) => field.onChange(e.target.value.split("\n"))}
|
||||
className="font-mono text-sm min-h-24"
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
One header per line in Key: Value format. Values are stored as plain text.
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="postBackupWebhookBody"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Post-backup webhook body</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea {...field} placeholder='{"action":"start"}' className="font-mono text-sm min-h-24" />
|
||||
</FormControl>
|
||||
<FormDescription>Optional raw POST body. Leave empty to send the backup context JSON.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
<WebhookFields
|
||||
form={form}
|
||||
phase="post"
|
||||
urlPlaceholder="http://host.docker.internal:8080/start"
|
||||
bodyPlaceholder='{"action":"start"}'
|
||||
description="Called with POST after restic finishes, including failed or cancelled runs."
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import { PathsSection } from "./paths-section";
|
|||
import { RetentionSection } from "./retention-section";
|
||||
import { SummarySection } from "./summary-section";
|
||||
import { internalFormSchema, type BackupScheduleFormValues, type InternalFormValues } from "./types";
|
||||
import { backupScheduleToFormValues, parseMultilineEntries, parseWebhookHeaders } from "./utils";
|
||||
import { backupScheduleToFormValues, parseMultilineEntries, toWebhookConfig } from "./utils";
|
||||
|
||||
export type { BackupScheduleFormValues };
|
||||
|
||||
|
|
@ -47,10 +47,10 @@ export const CreateScheduleForm = ({ initialValues, formId, onSubmit, volume }:
|
|||
maxRetries,
|
||||
retryDelay,
|
||||
preBackupWebhookUrl,
|
||||
preBackupWebhookHeaders,
|
||||
preBackupWebhookHeadersText,
|
||||
preBackupWebhookBody,
|
||||
postBackupWebhookUrl,
|
||||
postBackupWebhookHeaders,
|
||||
postBackupWebhookHeadersText,
|
||||
postBackupWebhookBody,
|
||||
...rest
|
||||
} = data;
|
||||
|
|
@ -58,10 +58,12 @@ export const CreateScheduleForm = ({ initialValues, formId, onSubmit, volume }:
|
|||
const excludeIfPresent = parseMultilineEntries(excludeIfPresentText);
|
||||
const parsedIncludePatterns = parseMultilineEntries(includePatterns);
|
||||
const customResticParams = parseMultilineEntries(customResticParamsText);
|
||||
const preBackupWebhookUrlValue = preBackupWebhookUrl?.trim();
|
||||
const postBackupWebhookUrlValue = postBackupWebhookUrl?.trim();
|
||||
const preBackupWebhookBodyValue = preBackupWebhookBody?.trim();
|
||||
const postBackupWebhookBodyValue = postBackupWebhookBody?.trim();
|
||||
const preBackupWebhook = toWebhookConfig(preBackupWebhookUrl, preBackupWebhookHeadersText, preBackupWebhookBody);
|
||||
const postBackupWebhook = toWebhookConfig(
|
||||
postBackupWebhookUrl,
|
||||
postBackupWebhookHeadersText,
|
||||
postBackupWebhookBody,
|
||||
);
|
||||
|
||||
onSubmit({
|
||||
...rest,
|
||||
|
|
@ -71,20 +73,8 @@ export const CreateScheduleForm = ({ initialValues, formId, onSubmit, volume }:
|
|||
excludePatterns,
|
||||
excludeIfPresent,
|
||||
customResticParams,
|
||||
preBackupWebhook: preBackupWebhookUrlValue
|
||||
? {
|
||||
url: preBackupWebhookUrlValue,
|
||||
headers: parseWebhookHeaders(preBackupWebhookHeaders),
|
||||
body: preBackupWebhookBodyValue || undefined,
|
||||
}
|
||||
: null,
|
||||
postBackupWebhook: postBackupWebhookUrlValue
|
||||
? {
|
||||
url: postBackupWebhookUrlValue,
|
||||
headers: parseWebhookHeaders(postBackupWebhookHeaders),
|
||||
body: postBackupWebhookBodyValue || undefined,
|
||||
}
|
||||
: null,
|
||||
backupWebhooks:
|
||||
preBackupWebhook || postBackupWebhook ? { pre: preBackupWebhook, post: postBackupWebhook } : null,
|
||||
maxRetries,
|
||||
retryDelay,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
import { z } from "zod";
|
||||
import type { BackupWebhooks } from "@zerobyte/core/backup-hooks";
|
||||
|
||||
const webhookHeadersSchema = z
|
||||
.array(
|
||||
z.string().refine((header) => !header.trim() || header.includes(":"), {
|
||||
message: "Headers must use Key: Value format",
|
||||
}),
|
||||
)
|
||||
.catch(() => [])
|
||||
.optional();
|
||||
const webhookHeadersSchema = z.string().refine(
|
||||
(value) =>
|
||||
value
|
||||
.split("\n")
|
||||
.map((header) => header.trim())
|
||||
.filter(Boolean)
|
||||
.every((header) => header.includes(":")),
|
||||
{ message: "Headers must use Key: Value format" },
|
||||
);
|
||||
|
||||
export const internalFormSchema = z.object({
|
||||
name: z.string().min(1).max(128),
|
||||
|
|
@ -30,10 +32,10 @@ export const internalFormSchema = z.object({
|
|||
oneFileSystem: z.boolean().optional(),
|
||||
customResticParamsText: z.string().optional(),
|
||||
preBackupWebhookUrl: z.union([z.url(), z.literal("")]).optional(),
|
||||
preBackupWebhookHeaders: webhookHeadersSchema,
|
||||
preBackupWebhookHeadersText: webhookHeadersSchema.optional(),
|
||||
preBackupWebhookBody: z.string().optional(),
|
||||
postBackupWebhookUrl: z.union([z.url(), z.literal("")]).optional(),
|
||||
postBackupWebhookHeaders: webhookHeadersSchema,
|
||||
postBackupWebhookHeadersText: webhookHeadersSchema.optional(),
|
||||
postBackupWebhookBody: z.string().optional(),
|
||||
maxRetries: z.number().min(0).max(32).optional(),
|
||||
retryDelay: z.number().min(1).max(1440).optional(),
|
||||
|
|
@ -58,16 +60,15 @@ export type BackupScheduleFormValues = Omit<
|
|||
| "includePatterns"
|
||||
| "customResticParamsText"
|
||||
| "preBackupWebhookUrl"
|
||||
| "preBackupWebhookHeaders"
|
||||
| "preBackupWebhookHeadersText"
|
||||
| "preBackupWebhookBody"
|
||||
| "postBackupWebhookUrl"
|
||||
| "postBackupWebhookHeaders"
|
||||
| "postBackupWebhookHeadersText"
|
||||
| "postBackupWebhookBody"
|
||||
> & {
|
||||
excludePatterns?: string[];
|
||||
excludeIfPresent?: string[];
|
||||
includePatterns?: string[];
|
||||
customResticParams?: string[];
|
||||
preBackupWebhook?: { url: string; headers?: string[]; body?: string } | null;
|
||||
postBackupWebhook?: { url: string; headers?: string[]; body?: string } | null;
|
||||
backupWebhooks?: BackupWebhooks | null;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,18 +2,25 @@ import type { BackupSchedule } from "~/client/lib/types";
|
|||
import { cronToFormValues } from "../../lib/cron-utils";
|
||||
import type { InternalFormValues } from "./types";
|
||||
|
||||
export const parseMultilineEntries = (value?: string) =>
|
||||
value
|
||||
? value
|
||||
.split("\n")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
export const parseMultilineEntries = (value?: string) => {
|
||||
if (!value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
export const parseWebhookHeaders = (headers?: string[]) => {
|
||||
const parsedHeaders = headers?.map((header) => header.trim()).filter(Boolean) ?? [];
|
||||
return value
|
||||
.split("\n")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean);
|
||||
};
|
||||
|
||||
return parsedHeaders.length > 0 ? parsedHeaders : undefined;
|
||||
export const toWebhookConfig = (url?: string, headers?: string, body?: string) => {
|
||||
const trimmedUrl = url?.trim();
|
||||
const trimmedBody = body?.trim();
|
||||
const parsedHeaders = parseMultilineEntries(headers);
|
||||
|
||||
return trimmedUrl
|
||||
? { url: trimmedUrl, headers: parsedHeaders.length > 0 ? parsedHeaders : undefined, body: trimmedBody || undefined }
|
||||
: null;
|
||||
};
|
||||
|
||||
export const backupScheduleToFormValues = (schedule?: BackupSchedule): InternalFormValues | undefined => {
|
||||
|
|
@ -32,12 +39,12 @@ export const backupScheduleToFormValues = (schedule?: BackupSchedule): InternalF
|
|||
excludeIfPresentText: schedule.excludeIfPresent?.join("\n") || undefined,
|
||||
oneFileSystem: schedule.oneFileSystem ?? false,
|
||||
customResticParamsText: schedule.customResticParams?.join("\n") ?? "",
|
||||
preBackupWebhookUrl: schedule.preBackupWebhook?.url ?? "",
|
||||
preBackupWebhookHeaders: schedule.preBackupWebhook?.headers ?? [],
|
||||
preBackupWebhookBody: schedule.preBackupWebhook?.body ?? "",
|
||||
postBackupWebhookUrl: schedule.postBackupWebhook?.url ?? "",
|
||||
postBackupWebhookHeaders: schedule.postBackupWebhook?.headers ?? [],
|
||||
postBackupWebhookBody: schedule.postBackupWebhook?.body ?? "",
|
||||
preBackupWebhookUrl: schedule.backupWebhooks?.pre?.url ?? "",
|
||||
preBackupWebhookHeadersText: schedule.backupWebhooks?.pre?.headers?.join("\n") ?? "",
|
||||
preBackupWebhookBody: schedule.backupWebhooks?.pre?.body ?? "",
|
||||
postBackupWebhookUrl: schedule.backupWebhooks?.post?.url ?? "",
|
||||
postBackupWebhookHeadersText: schedule.backupWebhooks?.post?.headers?.join("\n") ?? "",
|
||||
postBackupWebhookBody: schedule.backupWebhooks?.post?.body ?? "",
|
||||
maxRetries: schedule.maxRetries,
|
||||
retryDelay: schedule.retryDelay,
|
||||
...cronValues,
|
||||
|
|
|
|||
|
|
@ -79,8 +79,7 @@ const schedule = {
|
|||
excludeIfPresent: [],
|
||||
oneFileSystem: false,
|
||||
customResticParams: [],
|
||||
preBackupWebhook: null,
|
||||
postBackupWebhook: null,
|
||||
backupWebhooks: null,
|
||||
};
|
||||
|
||||
const snapshot = {
|
||||
|
|
|
|||
|
|
@ -49,8 +49,7 @@ const renderEditBackupPage = ({ enabled, cronExpression }: { enabled: boolean; c
|
|||
excludeIfPresent: [],
|
||||
oneFileSystem: false,
|
||||
customResticParams: [],
|
||||
preBackupWebhook: null,
|
||||
postBackupWebhook: null,
|
||||
backupWebhooks: null,
|
||||
});
|
||||
}),
|
||||
http.get("/api/v1/repositories", () => {
|
||||
|
|
@ -141,10 +140,13 @@ test("submits webhook headers and body as plain config values", async () => {
|
|||
await userEvent.click(screen.getByRole("button", { name: "Update schedule" }));
|
||||
|
||||
await expect(submittedBody).resolves.toMatchObject({
|
||||
preBackupWebhook: {
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["Authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
backupWebhooks: {
|
||||
pre: {
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["Authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
},
|
||||
post: null,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -167,8 +167,7 @@ export function ScheduleDetailsPage(props: Props) {
|
|||
excludeIfPresent: schedule.excludeIfPresent || [],
|
||||
oneFileSystem: schedule.oneFileSystem,
|
||||
customResticParams: schedule.customResticParams || [],
|
||||
preBackupWebhook: schedule.preBackupWebhook,
|
||||
postBackupWebhook: schedule.postBackupWebhook,
|
||||
backupWebhooks: schedule.backupWebhooks,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -75,8 +75,7 @@ export function CreateBackupPage() {
|
|||
excludeIfPresent: formValues.excludeIfPresent,
|
||||
oneFileSystem: formValues.oneFileSystem,
|
||||
customResticParams: formValues.customResticParams,
|
||||
preBackupWebhook: formValues.preBackupWebhook,
|
||||
postBackupWebhook: formValues.postBackupWebhook,
|
||||
backupWebhooks: formValues.backupWebhooks,
|
||||
maxRetries: formValues.maxRetries,
|
||||
retryDelay: formValues.retryDelay,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,2 +0,0 @@
|
|||
ALTER TABLE `backup_schedules_table` ADD `pre_backup_webhook` text;--> statement-breakpoint
|
||||
ALTER TABLE `backup_schedules_table` ADD `post_backup_webhook` text;
|
||||
File diff suppressed because it is too large
Load diff
1
app/drizzle/20260429194428_short_mentor/migration.sql
Normal file
1
app/drizzle/20260429194428_short_mentor/migration.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE `backup_schedules_table` ADD `backup_webhooks` text;
|
||||
2341
app/drizzle/20260429194428_short_mentor/snapshot.json
Normal file
2341
app/drizzle/20260429194428_short_mentor/snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -9,7 +9,7 @@ import type {
|
|||
DoctorResult,
|
||||
ResticStatsDto,
|
||||
} from "@zerobyte/core/restic";
|
||||
import type { BackupWebhookConfig } from "@zerobyte/core/backup-hooks";
|
||||
import type { BackupWebhooks } from "@zerobyte/core/backup-hooks";
|
||||
import type { BackendConfig, BackendStatus, BackendType } from "~/schemas/volumes";
|
||||
import type { NotificationConfig, NotificationType } from "~/schemas/notifications";
|
||||
import type { ShortId } from "~/server/utils/branded";
|
||||
|
|
@ -311,8 +311,7 @@ export const backupSchedulesTable = sqliteTable("backup_schedules_table", {
|
|||
nextBackupAt: int("next_backup_at", { mode: "number" }),
|
||||
oneFileSystem: int("one_file_system", { mode: "boolean" }).notNull().default(false),
|
||||
customResticParams: text("custom_restic_params", { mode: "json" }).$type<string[]>().default([]),
|
||||
preBackupWebhook: text("pre_backup_webhook", { mode: "json" }).$type<BackupWebhookConfig | null>(),
|
||||
postBackupWebhook: text("post_backup_webhook", { mode: "json" }).$type<BackupWebhookConfig | null>(),
|
||||
backupWebhooks: text("backup_webhooks", { mode: "json" }).$type<BackupWebhooks | null>(),
|
||||
sortOrder: int("sort_order", { mode: "number" }).notNull().default(0),
|
||||
failureRetryCount: int("failure_retry_count").notNull().default(0),
|
||||
maxRetries: int("max_retries").notNull().default(2),
|
||||
|
|
|
|||
|
|
@ -280,19 +280,23 @@ describe("backup execution - validation failures", () => {
|
|||
const { runBackupMock } = setup();
|
||||
const volume = await createTestVolume();
|
||||
const repository = await createTestRepository();
|
||||
const schedule = await createTestBackupSchedule({
|
||||
volumeId: volume.id,
|
||||
repositoryId: repository.id,
|
||||
preBackupWebhook: {
|
||||
const backupWebhooks = {
|
||||
pre: {
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
},
|
||||
postBackupWebhook: {
|
||||
post: {
|
||||
url: "http://localhost:8080/start",
|
||||
headers: ["authorization: Bearer start-token"],
|
||||
body: '{"action":"start"}',
|
||||
},
|
||||
};
|
||||
|
||||
const schedule = await createTestBackupSchedule({
|
||||
volumeId: volume.id,
|
||||
repositoryId: repository.id,
|
||||
backupWebhooks,
|
||||
});
|
||||
|
||||
await backupsService.executeBackup(schedule.id);
|
||||
|
|
@ -301,18 +305,7 @@ describe("backup execution - validation failures", () => {
|
|||
"local",
|
||||
expect.objectContaining({
|
||||
payload: expect.objectContaining({
|
||||
webhooks: {
|
||||
pre: {
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
},
|
||||
post: {
|
||||
url: "http://localhost:8080/start",
|
||||
headers: ["authorization: Bearer start-token"],
|
||||
body: '{"action":"start"}',
|
||||
},
|
||||
},
|
||||
webhooks: backupWebhooks,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -408,36 +408,31 @@ describe("schedule webhooks", () => {
|
|||
const volume = await createTestVolume();
|
||||
const repository = await createTestRepository();
|
||||
|
||||
const backupWebhooks = {
|
||||
pre: {
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
},
|
||||
post: {
|
||||
url: "http://localhost:8080/start",
|
||||
headers: ["authorization: Bearer start-token"],
|
||||
body: '{"action":"start"}',
|
||||
},
|
||||
};
|
||||
|
||||
const schedule = await backupsService.createSchedule({
|
||||
name: "with-webhooks",
|
||||
volumeId: volume.shortId,
|
||||
repositoryId: repository.shortId,
|
||||
enabled: true,
|
||||
cronExpression: "0 0 * * *",
|
||||
preBackupWebhook: {
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
},
|
||||
postBackupWebhook: {
|
||||
url: "http://localhost:8080/start",
|
||||
headers: ["authorization: Bearer start-token"],
|
||||
body: '{"action":"start"}',
|
||||
},
|
||||
backupWebhooks,
|
||||
retryDelay: 15 * 60 * 1000,
|
||||
maxRetries: 2,
|
||||
});
|
||||
|
||||
expect(schedule.preBackupWebhook).toEqual({
|
||||
url: "http://localhost:8080/stop",
|
||||
headers: ["authorization: Bearer stop-token"],
|
||||
body: '{"action":"stop"}',
|
||||
});
|
||||
expect(schedule.postBackupWebhook).toEqual({
|
||||
url: "http://localhost:8080/start",
|
||||
headers: ["authorization: Bearer start-token"],
|
||||
body: '{"action":"start"}',
|
||||
});
|
||||
expect(schedule.backupWebhooks).toEqual(backupWebhooks);
|
||||
});
|
||||
|
||||
test("clears backup webhooks when updating a schedule with null values", async () => {
|
||||
|
|
@ -445,21 +440,21 @@ describe("schedule webhooks", () => {
|
|||
const repository = await createTestRepository();
|
||||
const schedule = await createTestBackupSchedule({
|
||||
repositoryId: repository.id,
|
||||
preBackupWebhook: { url: "http://localhost:8080/stop" },
|
||||
postBackupWebhook: { url: "http://localhost:8080/start" },
|
||||
backupWebhooks: {
|
||||
pre: { url: "http://localhost:8080/stop" },
|
||||
post: { url: "http://localhost:8080/start" },
|
||||
},
|
||||
});
|
||||
|
||||
const updated = await backupsService.updateSchedule(schedule.id, {
|
||||
repositoryId: repository.shortId,
|
||||
cronExpression: schedule.cronExpression,
|
||||
preBackupWebhook: null,
|
||||
postBackupWebhook: null,
|
||||
backupWebhooks: null,
|
||||
retryDelay: 15 * 60 * 1000,
|
||||
maxRetries: 2,
|
||||
});
|
||||
|
||||
expect(updated.preBackupWebhook).toBeNull();
|
||||
expect(updated.postBackupWebhook).toBeNull();
|
||||
expect(updated.backupWebhooks).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { Effect } from "effect";
|
||||
import { createBackupWebhooks, runBackupWithWebhooks } from "@zerobyte/core/backup-hooks";
|
||||
import { runBackupLifecycle } from "@zerobyte/core/backup-hooks";
|
||||
import type { BackupSchedule, Volume, Repository } from "../../db/schema";
|
||||
import { config } from "../../core/config";
|
||||
import { restic, resticDeps } from "../../core/restic";
|
||||
|
|
@ -8,7 +8,7 @@ import { agentManager, type BackupExecutionProgress } from "../agents/agents-man
|
|||
import { getVolumePath } from "../volumes/helpers";
|
||||
import { decryptRepositoryConfig } from "../repositories/repository-config-secrets";
|
||||
import { createBackupOptions } from "./backup.helpers";
|
||||
import { toErrorDetails, toMessage } from "../../utils/errors";
|
||||
import { toErrorDetails } from "../../utils/errors";
|
||||
|
||||
const LOCAL_AGENT_ID = "local";
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ const createBackupRunPayload = async ({
|
|||
rcloneConfigFile: resticDeps.rcloneConfigFile,
|
||||
hostname: resticDeps.hostname,
|
||||
},
|
||||
webhooks: createBackupWebhooks(schedule.preBackupWebhook, schedule.postBackupWebhook),
|
||||
webhooks: schedule.backupWebhooks ?? { pre: null, post: null },
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -64,31 +64,18 @@ const executeBackupWithoutAgent = async (
|
|||
{ signal, onProgress }: Pick<BackupExecutionRequest, "signal" | "onProgress">,
|
||||
) => {
|
||||
return Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata: {
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
organizationId: payload.organizationId,
|
||||
sourcePath: payload.sourcePath,
|
||||
},
|
||||
runBackupLifecycle({
|
||||
restic,
|
||||
repositoryConfig: payload.repositoryConfig,
|
||||
sourcePath: payload.sourcePath,
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
organizationId: payload.organizationId,
|
||||
options: payload.options,
|
||||
webhooks: payload.webhooks,
|
||||
signal,
|
||||
formatErrorDetails: toErrorDetails,
|
||||
formatErrorMessage: toMessage,
|
||||
runBackup: () =>
|
||||
restic.backup(payload.repositoryConfig, payload.sourcePath, {
|
||||
...payload.options,
|
||||
organizationId: payload.organizationId,
|
||||
signal,
|
||||
onProgress,
|
||||
}).pipe(
|
||||
Effect.map((result) => ({
|
||||
status: "completed" as const,
|
||||
exitCode: result.exitCode,
|
||||
result: result.result,
|
||||
warningDetails: result.warningDetails,
|
||||
})),
|
||||
),
|
||||
onProgress,
|
||||
formatError: toErrorDetails,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { z } from "zod";
|
||||
import { describeRoute, resolver } from "hono-openapi";
|
||||
import { backupWebhookConfigSchema } from "@zerobyte/core/backup-hooks";
|
||||
import { backupWebhooksSchema } from "@zerobyte/core/backup-hooks";
|
||||
import { volumeSchema } from "../volumes/volume.dto";
|
||||
import { repositorySchema } from "../repositories/repositories.dto";
|
||||
import { backupProgressEventSchema } from "~/schemas/events-dto";
|
||||
|
|
@ -30,8 +30,7 @@ const backupScheduleSchema = z.object({
|
|||
includePatterns: z.array(z.string()).nullable(),
|
||||
oneFileSystem: z.boolean(),
|
||||
customResticParams: z.array(z.string()).nullable(),
|
||||
preBackupWebhook: backupWebhookConfigSchema.nullable(),
|
||||
postBackupWebhook: backupWebhookConfigSchema.nullable(),
|
||||
backupWebhooks: backupWebhooksSchema.nullable(),
|
||||
maxRetries: z.number(),
|
||||
retryDelay: z.number().transform((ms) => Math.round(ms / 60000)),
|
||||
lastBackupAt: z.number().nullable(),
|
||||
|
|
@ -129,8 +128,7 @@ export const createBackupScheduleBody = z.object({
|
|||
oneFileSystem: z.boolean().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
customResticParams: z.array(z.string()).optional(),
|
||||
preBackupWebhook: backupWebhookConfigSchema.nullable().optional(),
|
||||
postBackupWebhook: backupWebhookConfigSchema.nullable().optional(),
|
||||
backupWebhooks: backupWebhooksSchema.nullable().optional(),
|
||||
maxRetries: z.number().min(0).max(32).optional().default(2),
|
||||
retryDelay: z
|
||||
.number()
|
||||
|
|
@ -176,8 +174,7 @@ export const updateBackupScheduleBody = z.object({
|
|||
oneFileSystem: z.boolean().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
customResticParams: z.array(z.string()).optional(),
|
||||
preBackupWebhook: backupWebhookConfigSchema.nullable().optional(),
|
||||
postBackupWebhook: backupWebhookConfigSchema.nullable().optional(),
|
||||
backupWebhooks: backupWebhooksSchema.nullable().optional(),
|
||||
maxRetries: z.number().min(0).max(32).optional().default(2),
|
||||
retryDelay: z
|
||||
.number()
|
||||
|
|
|
|||
|
|
@ -104,8 +104,7 @@ const createSchedule = async (data: CreateBackupScheduleBody) => {
|
|||
includePatterns: data.includePatterns ?? [],
|
||||
oneFileSystem: data.oneFileSystem,
|
||||
customResticParams: data.customResticParams ?? [],
|
||||
preBackupWebhook: data.preBackupWebhook ?? null,
|
||||
postBackupWebhook: data.postBackupWebhook ?? null,
|
||||
backupWebhooks: data.backupWebhooks ?? null,
|
||||
nextBackupAt: nextBackupAt,
|
||||
shortId: generateShortId(),
|
||||
maxRetries: data.maxRetries,
|
||||
|
|
@ -168,8 +167,7 @@ const updateSchedule = async (scheduleIdOrShortId: number | string, data: Update
|
|||
.set({
|
||||
...data,
|
||||
repositoryId: repository.id,
|
||||
preBackupWebhook: data.preBackupWebhook === undefined ? schedule.preBackupWebhook : data.preBackupWebhook,
|
||||
postBackupWebhook: data.postBackupWebhook === undefined ? schedule.postBackupWebhook : data.postBackupWebhook,
|
||||
backupWebhooks: data.backupWebhooks === undefined ? schedule.backupWebhooks : data.backupWebhooks,
|
||||
nextBackupAt,
|
||||
updatedAt: Date.now(),
|
||||
})
|
||||
|
|
|
|||
|
|
@ -211,7 +211,7 @@ test("fails without running restic when the pre-backup webhook fails", async ()
|
|||
expect(backupMock).not.toHaveBeenCalled();
|
||||
expect(failed?.success).toBe(true);
|
||||
if (failed?.success && failed.data.type === "backup.failed") {
|
||||
expect(failed.data.payload.errorDetails).toContain("Pre-backup webhook returned HTTP 500");
|
||||
expect(failed.data.payload.errorDetails).toContain("pre webhook returned HTTP 500");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -239,7 +239,40 @@ test("reports a post-backup webhook failure as completed warning details", async
|
|||
const completed = messages.find((message) => message?.success && message.data.type === "backup.completed");
|
||||
expect(completed?.success).toBe(true);
|
||||
if (completed?.success && completed.data.type === "backup.completed") {
|
||||
expect(completed.data.payload.warningDetails).toContain("Post-backup webhook returned HTTP 500");
|
||||
expect(completed.data.payload.warningDetails).toContain("post webhook returned HTTP 500");
|
||||
}
|
||||
});
|
||||
|
||||
test("includes post-backup webhook failure details when a backup is cancelled", async () => {
|
||||
server.use(
|
||||
http.post("http://localhost:8080/post", () => {
|
||||
return new HttpResponse("start failed", { status: 500 });
|
||||
}),
|
||||
);
|
||||
vi.spyOn(resticServer, "createRestic").mockReturnValue(
|
||||
fromPartial({
|
||||
backup: (_config: unknown, _source: string, options: { signal: AbortSignal }) =>
|
||||
Effect.sync(() => {
|
||||
vi.spyOn(options.signal, "aborted", "get").mockReturnValue(true);
|
||||
vi.spyOn(options.signal, "reason", "get").mockReturnValue(new Error("Backup was cancelled"));
|
||||
return { exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
const messages = await runBackupCommand(
|
||||
createRunPayload({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
const cancelled = messages.find((message) => message?.success && message.data.type === "backup.cancelled");
|
||||
expect(cancelled?.success).toBe(true);
|
||||
if (cancelled?.success && cancelled.data.type === "backup.cancelled") {
|
||||
expect(cancelled.data.payload.message).toContain("post webhook returned HTTP 500: start failed");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { Effect, Runtime } from "effect";
|
||||
import { createAgentMessage, type BackupRunPayload } from "@zerobyte/contracts/agent-protocol";
|
||||
import { runBackupWithWebhooks } from "@zerobyte/core/backup-hooks";
|
||||
import { runBackupLifecycle } from "@zerobyte/core/backup-hooks";
|
||||
import { logger } from "@zerobyte/core/node";
|
||||
import { type ResticDeps } from "@zerobyte/core/restic";
|
||||
import { createRestic } from "@zerobyte/core/restic/server";
|
||||
|
|
@ -27,12 +27,12 @@ export const handleBackupRunCommand = (context: ControllerCommandContext, payloa
|
|||
|
||||
yield* Effect.fork(
|
||||
Effect.gen(function* () {
|
||||
const sendCancelled = () => {
|
||||
const sendCancelled = (message?: string) => {
|
||||
return context.offerOutbound(
|
||||
createAgentMessage("backup.cancelled", {
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
message: "Backup was cancelled",
|
||||
message: message ?? "Backup was cancelled",
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
|
@ -57,44 +57,29 @@ export const handleBackupRunCommand = (context: ControllerCommandContext, payloa
|
|||
const restic = createRestic(deps);
|
||||
const runtime = yield* Effect.runtime<never>();
|
||||
|
||||
const backupResult = yield* runBackupWithWebhooks({
|
||||
metadata: {
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
organizationId: payload.organizationId,
|
||||
sourcePath: payload.sourcePath,
|
||||
},
|
||||
const backupResult = yield* runBackupLifecycle({
|
||||
restic,
|
||||
repositoryConfig: payload.repositoryConfig,
|
||||
sourcePath: payload.sourcePath,
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
organizationId: payload.organizationId,
|
||||
options: payload.options,
|
||||
webhooks: payload.webhooks,
|
||||
signal: abortController.signal,
|
||||
runBackup: () => {
|
||||
return restic
|
||||
.backup(payload.repositoryConfig, payload.sourcePath, {
|
||||
...payload.options,
|
||||
organizationId: payload.organizationId,
|
||||
signal: abortController.signal,
|
||||
onProgress: (progress) => {
|
||||
void Runtime.runPromise(
|
||||
runtime,
|
||||
context.offerOutbound(
|
||||
createAgentMessage("backup.progress", {
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
progress,
|
||||
}),
|
||||
),
|
||||
).catch((error) => {
|
||||
logger.error(`Failed to send backup progress update: ${toMessage(error)}`);
|
||||
});
|
||||
},
|
||||
})
|
||||
.pipe(
|
||||
Effect.map((result) => ({
|
||||
status: "completed" as const,
|
||||
exitCode: result.exitCode,
|
||||
result: result.result,
|
||||
warningDetails: result.warningDetails,
|
||||
})),
|
||||
);
|
||||
onProgress: (progress) => {
|
||||
void Runtime.runPromise(
|
||||
runtime,
|
||||
context.offerOutbound(
|
||||
createAgentMessage("backup.progress", {
|
||||
jobId: payload.jobId,
|
||||
scheduleId: payload.scheduleId,
|
||||
progress,
|
||||
}),
|
||||
),
|
||||
).catch((error) => {
|
||||
logger.error(`Failed to send backup progress update: ${toMessage(error)}`);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -121,7 +106,7 @@ export const handleBackupRunCommand = (context: ControllerCommandContext, payloa
|
|||
);
|
||||
return;
|
||||
case "cancelled":
|
||||
yield* sendCancelled();
|
||||
yield* sendCancelled(backupResult.message);
|
||||
return;
|
||||
}
|
||||
}).pipe(Effect.ensuring(context.deleteRunningJob(payload.jobId))),
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { Effect } from "effect";
|
|||
import { HttpResponse, http } from "msw";
|
||||
import { setupServer } from "msw/node";
|
||||
import { afterAll, afterEach, beforeAll, expect, test } from "vitest";
|
||||
import { runBackupWithWebhooks } from "../index.js";
|
||||
import { runBackupLifecycle } from "../index.js";
|
||||
|
||||
const server = setupServer();
|
||||
|
||||
|
|
@ -25,6 +25,31 @@ const metadata = {
|
|||
sourcePath: "/tmp/source",
|
||||
};
|
||||
|
||||
const defaultSignal = () => new AbortController().signal;
|
||||
|
||||
const completedBackup = <TResult>(result: TResult, exitCode = 0, warningDetails: string | null = null) =>
|
||||
Effect.succeed({ exitCode, result, warningDetails });
|
||||
|
||||
const runWithHooks = <TResult>(
|
||||
overrides: Omit<Partial<Parameters<typeof runBackupLifecycle<TResult>>[0]>, "restic"> & {
|
||||
runBackup: () => Effect.Effect<{ exitCode: number; result: TResult; warningDetails: string | null }, unknown>;
|
||||
},
|
||||
) => {
|
||||
const { runBackup, ...options } = overrides;
|
||||
|
||||
return Effect.runPromise(
|
||||
runBackupLifecycle({
|
||||
...metadata,
|
||||
restic: { backup: runBackup },
|
||||
repositoryConfig: { backend: "local", path: "/tmp/repository" },
|
||||
options: {},
|
||||
webhooks: { pre: null, post: null },
|
||||
signal: defaultSignal(),
|
||||
...options,
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
type WebhookBody = {
|
||||
phase?: string;
|
||||
event?: string;
|
||||
|
|
@ -54,21 +79,17 @@ test("runs pre and post webhooks around a successful backup", async () => {
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: { url: "http://localhost:8080/pre" },
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: new AbortController().signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
events.push("backup");
|
||||
return { status: "completed" as const, exitCode: 0, result: "snapshot-1", warningDetails: null };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: { url: "http://localhost:8080/pre" },
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
events.push("backup");
|
||||
return { exitCode: 0, result: "snapshot-1", warningDetails: null };
|
||||
}),
|
||||
});
|
||||
|
||||
expect(events).toEqual(["pre", "backup", "post"]);
|
||||
expect(preBody).toMatchObject({ ...metadata, phase: "pre", event: "backup.pre" });
|
||||
|
|
@ -86,23 +107,13 @@ test("sends warning details to the post-backup webhook for a non-zero completed
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: new AbortController().signal,
|
||||
runBackup: () =>
|
||||
Effect.succeed({
|
||||
status: "completed" as const,
|
||||
exitCode: 3,
|
||||
result: "snapshot-1",
|
||||
warningDetails: "some files could not be read",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
runBackup: () => completedBackup("snapshot-1", 3, "some files could not be read"),
|
||||
});
|
||||
|
||||
expect(postBody).toMatchObject({ status: "warning", error: "some files could not be read" });
|
||||
expect(result).toEqual({
|
||||
|
|
@ -123,17 +134,13 @@ test("sends error details to the post-backup webhook when the backup fails", asy
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: new AbortController().signal,
|
||||
runBackup: () => Effect.succeed({ status: "failed" as const, error: new Error("restic failed") }),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
runBackup: () => Effect.fail(new Error("restic failed")),
|
||||
});
|
||||
|
||||
expect(postBody).toMatchObject({ status: "error", error: "restic failed" });
|
||||
expect(result).toEqual({ status: "failed", error: "restic failed" });
|
||||
|
|
@ -153,27 +160,23 @@ test("fails without running the backup or post webhook when the pre-backup webho
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: { url: "http://localhost:8080/pre" },
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: new AbortController().signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
backupRan = true;
|
||||
return { status: "completed" as const, exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: { url: "http://localhost:8080/pre" },
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
backupRan = true;
|
||||
return { exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
});
|
||||
|
||||
expect(backupRan).toBe(false);
|
||||
expect(postRan).toBe(false);
|
||||
expect(result.status).toBe("failed");
|
||||
if (result.status === "failed") {
|
||||
expect(result.error).toContain("Pre-backup webhook returned HTTP 500: stop failed");
|
||||
expect(result.error).toContain("pre webhook returned HTTP 500: stop failed");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -191,22 +194,17 @@ test("sends configured webhook headers and body without replacing them", async (
|
|||
}),
|
||||
);
|
||||
|
||||
await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: {
|
||||
url: "http://localhost:8080/post",
|
||||
headers: ["authorization: Bearer post-token"],
|
||||
body: "start-container",
|
||||
},
|
||||
await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: {
|
||||
url: "http://localhost:8080/post",
|
||||
headers: ["authorization: Bearer post-token"],
|
||||
body: "start-container",
|
||||
},
|
||||
signal: new AbortController().signal,
|
||||
runBackup: () =>
|
||||
Effect.succeed({ status: "completed" as const, exitCode: 0, result: null, warningDetails: null }),
|
||||
}),
|
||||
);
|
||||
},
|
||||
runBackup: () => completedBackup(null),
|
||||
});
|
||||
|
||||
expect(body).toBe("start-container");
|
||||
expect(authorization).toBe("Bearer post-token");
|
||||
|
|
@ -224,21 +222,18 @@ test("runs the post-backup webhook after cancellation without using the cancelle
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return { status: "failed" as const, error: new Error("restic cancelled") };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.gen(function* () {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return yield* Effect.fail(new Error("restic cancelled"));
|
||||
}),
|
||||
});
|
||||
|
||||
expect(postBody).toMatchObject({ status: "cancelled", error: "restic cancelled" });
|
||||
expect(result).toEqual({ status: "cancelled", message: "Backup was cancelled" });
|
||||
|
|
@ -255,21 +250,18 @@ test("runs the post-backup webhook when cancellation returns a completed backup
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return { status: "completed" as const, exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return { exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
});
|
||||
|
||||
expect(postBody).toMatchObject({ status: "cancelled" });
|
||||
expect(result).toEqual({ status: "cancelled", message: "Backup was cancelled" });
|
||||
|
|
@ -286,27 +278,24 @@ test("includes post-backup webhook failure details after cancellation", async ()
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return { status: "failed" as const, error: new Error("restic cancelled") };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.gen(function* () {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return yield* Effect.fail(new Error("restic cancelled"));
|
||||
}),
|
||||
});
|
||||
|
||||
expect(postBody).toMatchObject({ status: "cancelled", error: "restic cancelled" });
|
||||
expect(result.status).toBe("cancelled");
|
||||
if (result.status === "cancelled") {
|
||||
expect(result.message).toContain("Backup was cancelled");
|
||||
expect(result.message).toContain("Post-backup webhook returned HTTP 500: start failed");
|
||||
expect(result.message).toContain("post webhook returned HTTP 500: start failed");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -321,27 +310,24 @@ test("includes post-backup webhook failure details after completed cancellation"
|
|||
}),
|
||||
);
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return { status: "completed" as const, exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: null,
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
return { exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
});
|
||||
|
||||
expect(postBody).toMatchObject({ status: "cancelled", error: "Backup was cancelled" });
|
||||
expect(result.status).toBe("cancelled");
|
||||
if (result.status === "cancelled") {
|
||||
expect(result.message).toContain("Backup was cancelled");
|
||||
expect(result.message).toContain("Post-backup webhook returned HTTP 500: cleanup failed");
|
||||
expect(result.message).toContain("post webhook returned HTTP 500: cleanup failed");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -351,21 +337,18 @@ test("cancels before the pre-backup webhook without running the backup", async (
|
|||
|
||||
abortController.abort(new Error("Backup was cancelled"));
|
||||
|
||||
const result = await Effect.runPromise(
|
||||
runBackupWithWebhooks({
|
||||
metadata,
|
||||
webhooks: {
|
||||
pre: { url: "http://localhost:8080/pre" },
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
backupRan = true;
|
||||
return { status: "completed" as const, exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const result = await runWithHooks({
|
||||
webhooks: {
|
||||
pre: { url: "http://localhost:8080/pre" },
|
||||
post: { url: "http://localhost:8080/post" },
|
||||
},
|
||||
signal: abortController.signal,
|
||||
runBackup: () =>
|
||||
Effect.sync(() => {
|
||||
backupRan = true;
|
||||
return { exitCode: 0, result: null, warningDetails: null };
|
||||
}),
|
||||
});
|
||||
|
||||
expect(backupRan).toBe(false);
|
||||
expect(result).toEqual({ status: "cancelled", message: "Backup was cancelled" });
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
import { Data, Effect } from "effect";
|
||||
import { z } from "zod";
|
||||
import type { CompressionMode, RepositoryConfig, ResticBackupProgressDto } from "../restic/index.js";
|
||||
import { toErrorDetails, toMessage } from "../utils/index.js";
|
||||
|
||||
export const DEFAULT_BACKUP_WEBHOOK_TIMEOUT_MS = 60_000;
|
||||
const DEFAULT_BACKUP_WEBHOOK_TIMEOUT_MS = 60_000;
|
||||
|
||||
export const backupWebhookConfigSchema = z.object({
|
||||
url: z.url(),
|
||||
headers: z
|
||||
.array(z.string())
|
||||
.catch(() => [])
|
||||
.optional(),
|
||||
headers: z.array(z.string()).optional(),
|
||||
body: z.string().optional(),
|
||||
});
|
||||
|
||||
|
|
@ -21,17 +19,10 @@ export const backupWebhooksSchema = z.object({
|
|||
export type BackupWebhookConfig = z.infer<typeof backupWebhookConfigSchema>;
|
||||
export type BackupWebhooks = z.infer<typeof backupWebhooksSchema>;
|
||||
|
||||
export type BackupWebhookPhase = "pre" | "post";
|
||||
export type BackupWebhookStatus = "success" | "warning" | "error" | "cancelled";
|
||||
type BackupWebhookPhase = "pre" | "post";
|
||||
type BackupWebhookStatus = "success" | "warning" | "error" | "cancelled";
|
||||
|
||||
export type BackupWebhookMetadata = {
|
||||
jobId: string;
|
||||
scheduleId: string;
|
||||
organizationId: string;
|
||||
sourcePath: string;
|
||||
};
|
||||
|
||||
export type BackupWebhookContext = {
|
||||
type BackupWebhookContext = {
|
||||
phase: BackupWebhookPhase;
|
||||
event: "backup.pre" | "backup.post";
|
||||
jobId: string;
|
||||
|
|
@ -42,60 +33,53 @@ export type BackupWebhookContext = {
|
|||
error?: string;
|
||||
};
|
||||
|
||||
export type BackupOperationResult<TResult> =
|
||||
| { status: "completed"; exitCode: number; result: TResult; warningDetails: string | null }
|
||||
| { status: "failed"; error: unknown };
|
||||
type BackupResult<TResult> = { exitCode: number; result: TResult; warningDetails: string | null };
|
||||
|
||||
export type BackupHookedExecutionResult<TResult> =
|
||||
type BackupLifecycleResult<TResult> =
|
||||
| { status: "completed"; exitCode: number; result: TResult; warningDetails: string | null }
|
||||
| { status: "failed"; error: string }
|
||||
| { status: "cancelled"; message?: string };
|
||||
|
||||
export type BackupHookedExecutionOptions<TResult, R = never> = {
|
||||
metadata: BackupWebhookMetadata;
|
||||
webhooks: BackupWebhooks;
|
||||
signal: AbortSignal;
|
||||
runBackup: () => Effect.Effect<BackupOperationResult<TResult>, unknown, R>;
|
||||
formatErrorDetails?: (error: unknown) => string;
|
||||
formatErrorMessage?: (error: unknown) => string;
|
||||
type BackupOptions = {
|
||||
tags?: string[];
|
||||
oneFileSystem?: boolean;
|
||||
exclude?: string[];
|
||||
excludeIfPresent?: string[];
|
||||
includePaths?: string[];
|
||||
includePatterns?: string[];
|
||||
customResticParams?: string[];
|
||||
compressionMode?: CompressionMode;
|
||||
};
|
||||
|
||||
export class BackupWebhookError extends Data.TaggedError("BackupWebhookError")<{
|
||||
type BackupLifecycleOptions<TResult> = {
|
||||
jobId: string;
|
||||
scheduleId: string;
|
||||
organizationId: string;
|
||||
sourcePath: string;
|
||||
restic: {
|
||||
backup: (
|
||||
config: RepositoryConfig,
|
||||
sourcePath: string,
|
||||
options: BackupOptions & {
|
||||
organizationId: string;
|
||||
signal: AbortSignal;
|
||||
onProgress?: (progress: ResticBackupProgressDto) => void;
|
||||
},
|
||||
) => Effect.Effect<BackupResult<TResult>, unknown>;
|
||||
};
|
||||
repositoryConfig: RepositoryConfig;
|
||||
options: BackupOptions;
|
||||
webhooks: BackupWebhooks;
|
||||
signal: AbortSignal;
|
||||
onProgress?: (progress: ResticBackupProgressDto) => void;
|
||||
formatError?: (error: unknown) => string;
|
||||
};
|
||||
|
||||
class BackupWebhookError extends Data.TaggedError("BackupWebhookError")<{
|
||||
cause: unknown;
|
||||
message: string;
|
||||
}> {}
|
||||
|
||||
export const createBackupWebhooks = (
|
||||
pre: BackupWebhookConfig | null | undefined,
|
||||
post: BackupWebhookConfig | null | undefined,
|
||||
): BackupWebhooks => ({
|
||||
pre: pre ?? null,
|
||||
post: post ?? null,
|
||||
});
|
||||
|
||||
const createWebhookContext = (
|
||||
metadata: BackupWebhookMetadata,
|
||||
phase: BackupWebhookPhase,
|
||||
status?: BackupWebhookStatus,
|
||||
error?: string,
|
||||
): BackupWebhookContext => {
|
||||
const context: BackupWebhookContext = {
|
||||
phase,
|
||||
event: phase === "pre" ? "backup.pre" : "backup.post",
|
||||
...metadata,
|
||||
};
|
||||
|
||||
if (status) {
|
||||
context.status = status;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
context.error = error;
|
||||
}
|
||||
|
||||
return context;
|
||||
};
|
||||
|
||||
const appendDetails = (primary: string | null | undefined, next: string | null | undefined) => {
|
||||
return [primary, next].filter(Boolean).join("\n\n");
|
||||
};
|
||||
|
|
@ -108,8 +92,6 @@ const getCompletedStatus = (exitCode: number, signal: AbortSignal): BackupWebhoo
|
|||
return exitCode === 0 ? "success" : "warning";
|
||||
};
|
||||
|
||||
const formatWebhookPhase = (phase: BackupWebhookPhase) => `${phase === "pre" ? "Pre" : "Post"}-backup`;
|
||||
|
||||
const createAbortController = (timeoutMs: number, signal?: AbortSignal) => {
|
||||
const abortController = new AbortController();
|
||||
const timeout = setTimeout(() => {
|
||||
|
|
@ -151,127 +133,135 @@ const createRequestInit = (config: BackupWebhookConfig, context: BackupWebhookCo
|
|||
}
|
||||
}
|
||||
|
||||
return {
|
||||
method: "POST",
|
||||
headers,
|
||||
body,
|
||||
};
|
||||
return { method: "POST", headers, body };
|
||||
};
|
||||
|
||||
export const runBackupWebhook = async (
|
||||
config: BackupWebhookConfig,
|
||||
context: BackupWebhookContext,
|
||||
options: { signal?: AbortSignal; timeoutMs?: number } = {},
|
||||
) => {
|
||||
const timeoutMs = options.timeoutMs ?? DEFAULT_BACKUP_WEBHOOK_TIMEOUT_MS;
|
||||
const controller = createAbortController(timeoutMs, options.signal);
|
||||
|
||||
try {
|
||||
const parsedConfig = backupWebhookConfigSchema.parse(config);
|
||||
const url = new URL(parsedConfig.url);
|
||||
const phase = formatWebhookPhase(context.phase);
|
||||
|
||||
const response = await fetch(url, {
|
||||
...createRequestInit(parsedConfig, context),
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const responseText = await response.text().catch(() => "");
|
||||
const details = responseText.trim().slice(0, 500);
|
||||
throw new BackupWebhookError({
|
||||
cause: new Error(`${phase} webhook returned HTTP ${response.status}`),
|
||||
message: `${phase} webhook returned HTTP ${response.status}${details ? `: ${details}` : ""}`,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof BackupWebhookError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
const phase = formatWebhookPhase(context.phase);
|
||||
|
||||
if (controller.signal.aborted && controller.signal.reason instanceof Error) {
|
||||
throw new BackupWebhookError({
|
||||
cause: controller.signal.reason,
|
||||
message: `${phase} webhook failed: ${controller.signal.reason.message}`,
|
||||
});
|
||||
}
|
||||
|
||||
throw new BackupWebhookError({ cause: error, message: `${phase} webhook failed: ${toMessage(error)}` });
|
||||
} finally {
|
||||
controller.cleanup();
|
||||
}
|
||||
};
|
||||
|
||||
const runConfiguredWebhook = (
|
||||
const runBackupWebhook = (
|
||||
config: BackupWebhookConfig | null,
|
||||
context: BackupWebhookContext,
|
||||
formatErrorDetails: (error: unknown) => string,
|
||||
signal?: AbortSignal,
|
||||
) => {
|
||||
if (!config) {
|
||||
return Effect.succeed(null);
|
||||
}
|
||||
options: {
|
||||
formatError: (error: unknown) => string;
|
||||
signal?: AbortSignal;
|
||||
timeoutMs?: number;
|
||||
},
|
||||
) =>
|
||||
Effect.suspend(() => {
|
||||
if (!config) {
|
||||
return Effect.succeed(null);
|
||||
}
|
||||
|
||||
return Effect.tryPromise({
|
||||
try: () => runBackupWebhook(config, context, { signal }),
|
||||
catch: (error) => {
|
||||
if (error instanceof BackupWebhookError) {
|
||||
return error;
|
||||
}
|
||||
const timeoutMs = options.timeoutMs ?? DEFAULT_BACKUP_WEBHOOK_TIMEOUT_MS;
|
||||
const controller = createAbortController(timeoutMs, options.signal);
|
||||
|
||||
return new BackupWebhookError({ cause: error, message: toMessage(error) });
|
||||
},
|
||||
}).pipe(
|
||||
Effect.as(null),
|
||||
Effect.catchAll((error) => Effect.succeed(formatErrorDetails(error))),
|
||||
);
|
||||
};
|
||||
return Effect.tryPromise({
|
||||
try: async () => {
|
||||
const response = await fetch(config.url, { ...createRequestInit(config, context), signal: controller.signal });
|
||||
|
||||
export const runBackupWithWebhooks = <TResult, R = never>({
|
||||
metadata,
|
||||
if (!response.ok) {
|
||||
const responseText = await response.text().catch(() => "");
|
||||
const details = responseText.trim().slice(0, 500);
|
||||
throw new BackupWebhookError({
|
||||
cause: new Error(`${context.phase} webhook returned HTTP ${response.status}`),
|
||||
message: `${context.phase} webhook returned HTTP ${response.status}${details ? `: ${details}` : ""}`,
|
||||
});
|
||||
}
|
||||
},
|
||||
catch: (error) => {
|
||||
if (error instanceof BackupWebhookError) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (controller.signal.aborted && controller.signal.reason instanceof Error) {
|
||||
return new BackupWebhookError({
|
||||
cause: controller.signal.reason,
|
||||
message: `${context.phase} webhook failed: ${controller.signal.reason.message}`,
|
||||
});
|
||||
}
|
||||
|
||||
return new BackupWebhookError({
|
||||
cause: error,
|
||||
message: `${context.phase} webhook failed: ${toMessage(error)}`,
|
||||
});
|
||||
},
|
||||
}).pipe(
|
||||
Effect.as(null),
|
||||
Effect.catchAll((error) => Effect.succeed(options.formatError(error))),
|
||||
Effect.ensuring(Effect.sync(controller.cleanup)),
|
||||
);
|
||||
});
|
||||
|
||||
export const runBackupLifecycle = <TResult>({
|
||||
jobId,
|
||||
scheduleId,
|
||||
organizationId,
|
||||
sourcePath,
|
||||
restic,
|
||||
repositoryConfig,
|
||||
options,
|
||||
webhooks,
|
||||
signal,
|
||||
runBackup,
|
||||
formatErrorDetails = toErrorDetails,
|
||||
formatErrorMessage = toMessage,
|
||||
}: BackupHookedExecutionOptions<TResult, R>): Effect.Effect<BackupHookedExecutionResult<TResult>, never, R> =>
|
||||
onProgress,
|
||||
formatError = toErrorDetails,
|
||||
}: BackupLifecycleOptions<TResult>): Effect.Effect<BackupLifecycleResult<TResult>, never> =>
|
||||
Effect.gen(function* () {
|
||||
const preHookError = yield* runConfiguredWebhook(
|
||||
const context = { jobId, scheduleId, organizationId, sourcePath };
|
||||
const preHookError = yield* runBackupWebhook(
|
||||
webhooks.pre,
|
||||
createWebhookContext(metadata, "pre"),
|
||||
formatErrorDetails,
|
||||
signal,
|
||||
{ ...context, phase: "pre", event: "backup.pre" },
|
||||
{
|
||||
formatError,
|
||||
signal,
|
||||
},
|
||||
);
|
||||
if (preHookError) {
|
||||
if (signal.aborted) {
|
||||
return { status: "cancelled", message: formatErrorMessage(signal.reason) };
|
||||
return { status: "cancelled", message: formatError(signal.reason) };
|
||||
}
|
||||
|
||||
return { status: "failed", error: preHookError };
|
||||
}
|
||||
|
||||
const backupResult = yield* Effect.suspend(runBackup).pipe(
|
||||
Effect.catchAll((error) => Effect.succeed({ status: "failed" as const, error })),
|
||||
const backupResult = yield* Effect.suspend(() =>
|
||||
restic.backup(repositoryConfig, sourcePath, { ...options, organizationId, signal, onProgress }),
|
||||
).pipe(
|
||||
Effect.map((result) => ({
|
||||
status: "completed" as const,
|
||||
...result,
|
||||
hookStatus: getCompletedStatus(result.exitCode, signal),
|
||||
hookError: signal.aborted ? formatError(signal.reason) : (result.warningDetails ?? undefined),
|
||||
})),
|
||||
Effect.catchAll((error) => {
|
||||
const errorDetails = formatError(error);
|
||||
|
||||
return Effect.succeed({
|
||||
status: "failed" as const,
|
||||
errorDetails,
|
||||
hookStatus: signal.aborted ? ("cancelled" as const) : ("error" as const),
|
||||
hookError: errorDetails,
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
const postHookError = yield* runBackupWebhook(
|
||||
webhooks.post,
|
||||
{
|
||||
...context,
|
||||
phase: "post",
|
||||
event: "backup.post",
|
||||
status: backupResult.hookStatus,
|
||||
error: backupResult.hookError,
|
||||
},
|
||||
{ formatError },
|
||||
);
|
||||
|
||||
if (signal.aborted) {
|
||||
return {
|
||||
status: "cancelled",
|
||||
message: appendDetails(formatError(signal.reason || backupResult.hookError), postHookError) || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
if (backupResult.status === "completed") {
|
||||
const hookStatus = getCompletedStatus(backupResult.exitCode, signal);
|
||||
const hookError = signal.aborted ? formatErrorMessage(signal.reason) : (backupResult.warningDetails ?? undefined);
|
||||
const postHookError = yield* runConfiguredWebhook(
|
||||
webhooks.post,
|
||||
createWebhookContext(metadata, "post", hookStatus, hookError),
|
||||
formatErrorDetails,
|
||||
);
|
||||
|
||||
if (signal.aborted) {
|
||||
return {
|
||||
status: "cancelled",
|
||||
message: appendDetails(formatErrorMessage(signal.reason), postHookError) || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: "completed",
|
||||
exitCode: backupResult.exitCode,
|
||||
|
|
@ -280,22 +270,8 @@ export const runBackupWithWebhooks = <TResult, R = never>({
|
|||
};
|
||||
}
|
||||
|
||||
const errorDetails = formatErrorDetails(backupResult.error);
|
||||
const postHookError = yield* runConfiguredWebhook(
|
||||
webhooks.post,
|
||||
createWebhookContext(metadata, "post", signal.aborted ? "cancelled" : "error", errorDetails),
|
||||
formatErrorDetails,
|
||||
);
|
||||
|
||||
if (signal.aborted) {
|
||||
return {
|
||||
status: "cancelled",
|
||||
message: appendDetails(formatErrorMessage(signal.reason || backupResult.error), postHookError) || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: "failed",
|
||||
error: appendDetails(errorDetails, postHookError) || errorDetails,
|
||||
error: appendDetails(backupResult.errorDetails, postHookError) || backupResult.errorDetails,
|
||||
};
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue