Merge remote-tracking branch 'upstream/develop' into feat/accept-status-codes-backend
# Conflicts: # server/src/service/infrastructure/network/HttpProvider.ts
This commit is contained in:
commit
d2c13fac5a
34 files changed
+1887
-540
No files matched your search
@@ -264,10 +264,10 @@ const CreateMonitorPage = () => {
|
|||||||
reset(defaults);
|
reset(defaults);
|
||||||
}, [defaults, reset]);
|
}, [defaults, reset]);
|
||||||
|
|
||||||
// Multi-step wizard, uptime create only (showTypeSelector). Other flows
|
// Multi-step wizard on every create flow; edit mode keeps the flat form
|
||||||
// keep the flat form, where showStep() is always true.
|
// where showStep() is always true.
|
||||||
const [currentStep, setCurrentStep] = useState(0);
|
const [currentStep, setCurrentStep] = useState(0);
|
||||||
const showStep = (step: number) => !showTypeSelector || currentStep === step;
|
const showStep = (step: number) => isEditMode || currentStep === step;
|
||||||
|
|
||||||
const watchedType = watch("type") as MonitorType;
|
const watchedType = watch("type") as MonitorType;
|
||||||
const watchedUseAdvancedMatching = watch("useAdvancedMatching") as boolean;
|
const watchedUseAdvancedMatching = watch("useAdvancedMatching") as boolean;
|
||||||
@@ -362,7 +362,7 @@ const CreateMonitorPage = () => {
|
|||||||
refetch={refetchMonitor}
|
refetch={refetchMonitor}
|
||||||
onDelete={handleDeleteClick}
|
onDelete={handleDeleteClick}
|
||||||
/>
|
/>
|
||||||
{showTypeSelector && (
|
{!isEditMode && (
|
||||||
<StepProgress
|
<StepProgress
|
||||||
steps={totalSteps}
|
steps={totalSteps}
|
||||||
current={currentStep}
|
current={currentStep}
|
||||||
@@ -1335,9 +1335,9 @@ const CreateMonitorPage = () => {
|
|||||||
|
|
||||||
<Stack
|
<Stack
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent={showTypeSelector ? "space-between" : "flex-end"}
|
justifyContent={!isEditMode ? "space-between" : "flex-end"}
|
||||||
>
|
>
|
||||||
{showTypeSelector && (
|
{!isEditMode && (
|
||||||
<Button
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
@@ -1348,7 +1348,7 @@ const CreateMonitorPage = () => {
|
|||||||
{t("common.buttons.back")}
|
{t("common.buttons.back")}
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
{showTypeSelector && currentStep < totalSteps - 1 ? (
|
{!isEditMode && currentStep < totalSteps - 1 ? (
|
||||||
<Button
|
<Button
|
||||||
key="wizard-next"
|
key="wizard-next"
|
||||||
type="button"
|
type="button"
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ import Stack from "@mui/material/Stack";
|
|||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
import { TableJobs, TableFailedJobs } from "./components/TableJobs";
|
import { TableJobs, TableFailedJobs } from "./components/TableJobs";
|
||||||
|
import { TableWorkers } from "./components/TableWorkers";
|
||||||
import { useTheme } from "@mui/material";
|
import { useTheme } from "@mui/material";
|
||||||
|
import { useState } from "react";
|
||||||
import { useTranslation, Trans } from "react-i18next";
|
import { useTranslation, Trans } from "react-i18next";
|
||||||
import { useGet, usePost } from "@/Hooks/UseApi";
|
import { useGet, usePost } from "@/Hooks/UseApi";
|
||||||
import type { QueueData } from "@/Types/Queue";
|
import type { QueueData } from "@/Types/Queue";
|
||||||
@@ -15,16 +16,27 @@ import { SPACING } from "@/Utils/Theme/constants";
|
|||||||
export const TabQueue = () => {
|
export const TabQueue = () => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const [page, setPage] = useState(0);
|
||||||
|
const [rowsPerPage, setRowsPerPage] = useState(10);
|
||||||
const {
|
const {
|
||||||
data: queueData,
|
data: queueData,
|
||||||
isLoading,
|
isLoading,
|
||||||
// error,
|
// error,
|
||||||
refetch,
|
refetch,
|
||||||
} = useGet<QueueData>("/queue/all-metrics", {}, { refreshInterval: 1000 });
|
} = useGet<QueueData>(
|
||||||
|
`/queue/all-metrics?page=${page}&rowsPerPage=${rowsPerPage}`,
|
||||||
|
{},
|
||||||
|
{ refreshInterval: 1000, keepPreviousData: true }
|
||||||
|
);
|
||||||
|
|
||||||
const { post, loading: isFlushing } = usePost();
|
const { post, loading: isFlushing } = usePost();
|
||||||
|
|
||||||
const jobs = queueData?.jobs ?? [];
|
const jobs = queueData?.jobs ?? [];
|
||||||
|
const jobsCount = queueData?.count ?? 0;
|
||||||
|
const workers = (queueData?.metrics?.workers ?? []).map((worker, idx) => ({
|
||||||
|
...worker,
|
||||||
|
id: idx,
|
||||||
|
}));
|
||||||
const metrics = queueData?.metrics ?? null;
|
const metrics = queueData?.metrics ?? null;
|
||||||
const hasNeverRun = !isLoading && metrics !== null && (metrics.totalRuns ?? 0) === 0;
|
const hasNeverRun = !isLoading && metrics !== null && (metrics.totalRuns ?? 0) === 0;
|
||||||
const noQueueData = !isLoading && metrics === null && jobs.length === 0;
|
const noQueueData = !isLoading && metrics === null && jobs.length === 0;
|
||||||
@@ -46,6 +58,34 @@ export const TabQueue = () => {
|
|||||||
return (
|
return (
|
||||||
<Stack gap={theme.spacing(16)}>
|
<Stack gap={theme.spacing(16)}>
|
||||||
<Metrics metrics={metrics} />
|
<Metrics metrics={metrics} />
|
||||||
|
<Stack gap={theme.spacing(3)}>
|
||||||
|
<Stack gap={theme.spacing(1)}>
|
||||||
|
<Typography
|
||||||
|
variant="eyebrow"
|
||||||
|
color={theme.palette.text.secondary}
|
||||||
|
>
|
||||||
|
{t("pages.logs.workers")}
|
||||||
|
</Typography>
|
||||||
|
<Typography color={theme.palette.text.secondary}>
|
||||||
|
<Trans
|
||||||
|
i18nKey="pages.logs.workersExplainers"
|
||||||
|
components={{
|
||||||
|
highlight: (
|
||||||
|
<Box
|
||||||
|
component="span"
|
||||||
|
bgcolor={theme.palette.rowStatus.running}
|
||||||
|
color={theme.palette.text.primary}
|
||||||
|
px={SPACING.SM}
|
||||||
|
py={SPACING.XXS}
|
||||||
|
borderRadius={theme.shape.borderRadius}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Typography>
|
||||||
|
</Stack>
|
||||||
|
<TableWorkers queueWorkers={workers} />
|
||||||
|
</Stack>
|
||||||
<Stack gap={theme.spacing(3)}>
|
<Stack gap={theme.spacing(3)}>
|
||||||
<Stack gap={theme.spacing(1)}>
|
<Stack gap={theme.spacing(1)}>
|
||||||
<Typography
|
<Typography
|
||||||
@@ -72,7 +112,17 @@ export const TabQueue = () => {
|
|||||||
/>
|
/>
|
||||||
</Typography>
|
</Typography>
|
||||||
</Stack>
|
</Stack>
|
||||||
<TableJobs jobs={jobs} />
|
<TableJobs
|
||||||
|
jobs={jobs}
|
||||||
|
count={jobsCount}
|
||||||
|
page={page}
|
||||||
|
rowsPerPage={rowsPerPage}
|
||||||
|
onPageChange={(_, newPage) => setPage(newPage)}
|
||||||
|
onRowsPerPageChange={(event) => {
|
||||||
|
setRowsPerPage(parseInt(event.target.value, 10));
|
||||||
|
setPage(0);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
<Stack gap={theme.spacing(3)}>
|
<Stack gap={theme.spacing(3)}>
|
||||||
<Stack gap={theme.spacing(1)}>
|
<Stack gap={theme.spacing(1)}>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Table } from "@/Components/design-elements";
|
import { Table, Pagination } from "@/Components/design-elements";
|
||||||
import { Typography, useTheme } from "@mui/material";
|
import { Typography, useTheme } from "@mui/material";
|
||||||
import prettyMilliseconds from "pretty-ms";
|
import prettyMilliseconds from "pretty-ms";
|
||||||
import { formatTimestamp } from "@/Utils/TimeUtils";
|
import { formatTimestamp } from "@/Utils/TimeUtils";
|
||||||
@@ -6,14 +6,27 @@ import { formatTimestamp } from "@/Utils/TimeUtils";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import type { QueueJobFailure, QueueJobSummary, QueueMetrics } from "@/Types/Queue";
|
import type { QueueJobFailure, QueueJobSummary, QueueMetrics } from "@/Types/Queue";
|
||||||
import type { Header } from "@/Components/design-elements";
|
import type { Header } from "@/Components/design-elements";
|
||||||
|
import type { TablePaginationProps } from "@mui/material/TablePagination";
|
||||||
|
|
||||||
type QueueJobWithId = QueueJobSummary & { id: string | number };
|
type QueueJobWithId = QueueJobSummary & { id: string | number };
|
||||||
|
|
||||||
interface TableJobsProps {
|
interface TableJobsProps {
|
||||||
jobs: QueueJobSummary[];
|
jobs: QueueJobSummary[];
|
||||||
|
count: number;
|
||||||
|
page: number;
|
||||||
|
rowsPerPage: number;
|
||||||
|
onPageChange: TablePaginationProps["onPageChange"];
|
||||||
|
onRowsPerPageChange: TablePaginationProps["onRowsPerPageChange"];
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TableJobs = ({ jobs }: TableJobsProps) => {
|
export const TableJobs = ({
|
||||||
|
jobs,
|
||||||
|
count,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
onPageChange,
|
||||||
|
onRowsPerPageChange,
|
||||||
|
}: TableJobsProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
|
||||||
@@ -127,21 +140,53 @@ export const TableJobs = ({ jobs }: TableJobsProps) => {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "lockedBy",
|
||||||
|
content: t("pages.logs.table.headers.lockedBy"),
|
||||||
|
render: (row) => {
|
||||||
|
const lockedBy = row.lockedBy;
|
||||||
|
if (!lockedBy) {
|
||||||
|
return <Typography sx={cellSx}>-</Typography>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workerId = lockedBy.split(":")[2];
|
||||||
|
const shortWorkerId = workerId.slice(workerId.length - 8);
|
||||||
|
return (
|
||||||
|
<Typography
|
||||||
|
title={workerId}
|
||||||
|
sx={cellSx}
|
||||||
|
>
|
||||||
|
{`...${shortWorkerId}`}
|
||||||
|
</Typography>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Table
|
<>
|
||||||
headers={headers}
|
<Table
|
||||||
data={jobsWithId}
|
headers={headers}
|
||||||
getRowSx={(row) => ({
|
data={jobsWithId}
|
||||||
...(row.lockedAt && {
|
getRowSx={(row) => ({
|
||||||
"& td": { backgroundColor: theme.palette.rowStatus.running },
|
...(row.lockedAt && {
|
||||||
}),
|
"& td": { backgroundColor: theme.palette.rowStatus.running },
|
||||||
...(row.monitorActive === false && {
|
}),
|
||||||
"& td": { backgroundColor: theme.palette.rowStatus.paused },
|
...(row.monitorActive === false && {
|
||||||
}),
|
"& td": { backgroundColor: theme.palette.rowStatus.paused },
|
||||||
})}
|
}),
|
||||||
/>
|
})}
|
||||||
|
/>
|
||||||
|
<Pagination
|
||||||
|
component="div"
|
||||||
|
count={count}
|
||||||
|
page={page}
|
||||||
|
rowsPerPage={rowsPerPage}
|
||||||
|
onPageChange={onPageChange}
|
||||||
|
onRowsPerPageChange={onRowsPerPageChange}
|
||||||
|
itemsOnPage={jobs.length}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
import { Table } from "@/Components/design-elements";
|
||||||
|
import { Typography, useTheme } from "@mui/material";
|
||||||
|
import { formatTimestamp } from "@/Utils/TimeUtils";
|
||||||
|
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import type { QueueWorker } from "@/Types/Queue";
|
||||||
|
import type { Header } from "@/Components/design-elements";
|
||||||
|
|
||||||
|
type QueueWorkerWithId = QueueWorker & { id: number };
|
||||||
|
|
||||||
|
interface TableWorkersProps {
|
||||||
|
queueWorkers: QueueWorkerWithId[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TableWorkers = ({ queueWorkers }: TableWorkersProps) => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const theme = useTheme();
|
||||||
|
|
||||||
|
const cellSx = {
|
||||||
|
whiteSpace: "nowrap" as const,
|
||||||
|
overflow: "hidden",
|
||||||
|
textOverflow: "ellipsis",
|
||||||
|
maxWidth: 220,
|
||||||
|
};
|
||||||
|
|
||||||
|
const headers: Header<QueueWorkerWithId>[] = [
|
||||||
|
{
|
||||||
|
id: "host",
|
||||||
|
content: t("pages.logs.table.headers.host"),
|
||||||
|
render: (row) => {
|
||||||
|
const host = row.workerId.split(":")[0];
|
||||||
|
return (
|
||||||
|
<Typography
|
||||||
|
fontFamily={theme.typography.fontFamilyMonospace}
|
||||||
|
sx={cellSx}
|
||||||
|
>
|
||||||
|
{host}
|
||||||
|
</Typography>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "processId",
|
||||||
|
content: t("pages.logs.table.headers.processId"),
|
||||||
|
render: (row) => {
|
||||||
|
const processId = row.workerId.split(":")[1];
|
||||||
|
return (
|
||||||
|
<Typography
|
||||||
|
fontFamily={theme.typography.fontFamilyMonospace}
|
||||||
|
sx={cellSx}
|
||||||
|
>
|
||||||
|
{processId}
|
||||||
|
</Typography>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "id",
|
||||||
|
content: t("pages.logs.table.headers.workerId"),
|
||||||
|
render: (row) => {
|
||||||
|
const workerId = row.workerId.split(":")[2];
|
||||||
|
const shortWorkerId = workerId.slice(workerId.length - 8);
|
||||||
|
return (
|
||||||
|
<Typography
|
||||||
|
fontFamily={theme.typography.fontFamilyMonospace}
|
||||||
|
title={workerId}
|
||||||
|
sx={cellSx}
|
||||||
|
>
|
||||||
|
{`...${shortWorkerId}`}
|
||||||
|
</Typography>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "mode",
|
||||||
|
content: t("pages.logs.table.headers.workerMode"),
|
||||||
|
render: (row) => <Typography sx={cellSx}>{row.mode}</Typography>,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "lastSeen",
|
||||||
|
content: t("pages.logs.table.headers.lastSeenAt"),
|
||||||
|
render: (row) => (
|
||||||
|
<Typography sx={cellSx}>{formatTimestamp(row.lastSeenAt)}</Typography>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Table
|
||||||
|
headers={headers}
|
||||||
|
data={queueWorkers}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -8,7 +8,7 @@ import { useTranslation } from "react-i18next";
|
|||||||
|
|
||||||
const LogsPage = () => {
|
const LogsPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [activeTab, setActiveTab] = useState<number>(0);
|
const [activeTab, setActiveTab] = useState<number>(1);
|
||||||
return (
|
return (
|
||||||
<BasePage headerKey="logs">
|
<BasePage headerKey="logs">
|
||||||
<Tabs
|
<Tabs
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { BasePage, ConfigBox } from "@/Components/design-elements";
|
import { BasePage, ConfigBox, StepProgress } from "@/Components/design-elements";
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import { logger } from "@/Utils/logger";
|
import { logger } from "@/Utils/logger";
|
||||||
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
|
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
|
||||||
@@ -26,6 +26,7 @@ import { useForm, Controller } from "react-hook-form";
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { useStatusPageForm } from "@/Hooks/useStatusPageForm";
|
import { useStatusPageForm } from "@/Hooks/useStatusPageForm";
|
||||||
import type { StatusPageFormData } from "@/Validation/statusPage";
|
import type { StatusPageFormData } from "@/Validation/statusPage";
|
||||||
|
import { statusPageStepCount, statusPageStepFieldsFor } from "@/Validation/statusPage";
|
||||||
import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi";
|
import { useGet, usePost, usePut, useDelete } from "@/Hooks/UseApi";
|
||||||
import type { Monitor } from "@/Types/Monitor";
|
import type { Monitor } from "@/Types/Monitor";
|
||||||
import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage";
|
import type { MonitorDisplayType, StatusPageResponse } from "@/Types/StatusPage";
|
||||||
@@ -86,13 +87,27 @@ const CreateStatusPage = () => {
|
|||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { control, reset, handleSubmit } = form;
|
const { control, reset, handleSubmit, trigger } = form;
|
||||||
|
|
||||||
// Reset form when defaults change (from fetched data)
|
// Reset form when defaults change (from fetched data)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
reset(defaults);
|
reset(defaults);
|
||||||
}, [defaults, reset]);
|
}, [defaults, reset]);
|
||||||
|
|
||||||
|
// Multi-step wizard, create only. The configure (edit) flow keeps the flat
|
||||||
|
// form, where showStep() is always true.
|
||||||
|
const [currentStep, setCurrentStep] = useState(0);
|
||||||
|
const showStep = (step: number) => !isCreate || currentStep === step;
|
||||||
|
const totalSteps = statusPageStepCount();
|
||||||
|
|
||||||
|
const handleNext = async () => {
|
||||||
|
const isValid = await trigger(statusPageStepFieldsFor(currentStep));
|
||||||
|
if (isValid) {
|
||||||
|
setCurrentStep((step) => step + 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const handleBack = () => setCurrentStep((step) => step - 1);
|
||||||
|
|
||||||
const watchedMonitorIds: string[] = form.watch("monitors") ?? [];
|
const watchedMonitorIds: string[] = form.watch("monitors") ?? [];
|
||||||
const computedTypes: MonitorDisplayType[] = useMemo(() => {
|
const computedTypes: MonitorDisplayType[] = useMemo(() => {
|
||||||
const selectedMonitors = (watchedMonitorIds ?? [])
|
const selectedMonitors = (watchedMonitorIds ?? [])
|
||||||
@@ -200,315 +215,334 @@ const CreateStatusPage = () => {
|
|||||||
onSubmit={handleSubmit(onSubmit, onError)}
|
onSubmit={handleSubmit(onSubmit, onError)}
|
||||||
>
|
>
|
||||||
{!isCreate && <HeaderConfigStatusControls onDelete={handleDeleteClick} />}
|
{!isCreate && <HeaderConfigStatusControls onDelete={handleDeleteClick} />}
|
||||||
<ConfigBox
|
{isCreate && (
|
||||||
title={t("pages.statusPages.form.access.title")}
|
<StepProgress
|
||||||
subtitle={t("pages.statusPages.form.access.description")}
|
steps={totalSteps}
|
||||||
rightContent={
|
current={currentStep}
|
||||||
<Stack
|
/>
|
||||||
direction="row"
|
)}
|
||||||
alignItems="center"
|
{showStep(0) && (
|
||||||
spacing={theme.spacing(SPACING.MD)}
|
<ConfigBox
|
||||||
>
|
title={t("pages.statusPages.form.access.title")}
|
||||||
<Controller
|
subtitle={t("pages.statusPages.form.access.description")}
|
||||||
name="isPublished"
|
rightContent={
|
||||||
control={control}
|
<Stack
|
||||||
render={({ field }) => (
|
direction="row"
|
||||||
<SwitchComponent
|
alignItems="center"
|
||||||
checked={field.value ?? false}
|
spacing={theme.spacing(SPACING.MD)}
|
||||||
onChange={(e) => field.onChange(e.target.checked)}
|
>
|
||||||
/>
|
<Controller
|
||||||
)}
|
name="isPublished"
|
||||||
/>
|
control={control}
|
||||||
<Typography>
|
render={({ field }) => (
|
||||||
{t("pages.statusPages.form.access.option.published.name")}
|
<SwitchComponent
|
||||||
</Typography>
|
checked={field.value ?? false}
|
||||||
</Stack>
|
onChange={(e) => field.onChange(e.target.checked)}
|
||||||
}
|
|
||||||
/>
|
|
||||||
<ConfigBox
|
|
||||||
title={t("pages.statusPages.form.basicInfo.title")}
|
|
||||||
subtitle={t("pages.statusPages.form.basicInfo.description")}
|
|
||||||
rightContent={
|
|
||||||
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
|
||||||
<Controller
|
|
||||||
name="companyName"
|
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
fieldLabel={t("pages.statusPages.form.basicInfo.option.name.label")}
|
|
||||||
placeholder={t(
|
|
||||||
"pages.statusPages.form.basicInfo.option.name.placeholder"
|
|
||||||
)}
|
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<Controller
|
|
||||||
name="url"
|
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => (
|
|
||||||
<TextField
|
|
||||||
{...field}
|
|
||||||
fieldLabel={t("pages.statusPages.form.basicInfo.option.url.label")}
|
|
||||||
placeholder={t(
|
|
||||||
"pages.statusPages.form.basicInfo.option.url.placeholder"
|
|
||||||
)}
|
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</Stack>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<ConfigBox
|
|
||||||
title={t("pages.statusPages.form.monitors.title")}
|
|
||||||
subtitle={t("pages.statusPages.form.monitors.description")}
|
|
||||||
rightContent={
|
|
||||||
<Controller
|
|
||||||
name="monitors"
|
|
||||||
control={control}
|
|
||||||
render={({ field, fieldState }) => {
|
|
||||||
const selectedMonitors = field.value
|
|
||||||
.map((id: string) => monitors.find((m) => m.id === id))
|
|
||||||
.filter((m): m is Monitor => m !== undefined);
|
|
||||||
|
|
||||||
const handleDragEnd = (result: DropResult) => {
|
|
||||||
if (!result.destination) return;
|
|
||||||
const reordered = Array.from(field.value);
|
|
||||||
const [removed] = reordered.splice(result.source.index, 1);
|
|
||||||
reordered.splice(result.destination.index, 0, removed);
|
|
||||||
field.onChange(reordered);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
|
||||||
<Autocomplete
|
|
||||||
multiple
|
|
||||||
options={monitors}
|
|
||||||
getOptionLabel={(option: Monitor) => option.name}
|
|
||||||
value={selectedMonitors}
|
|
||||||
onChange={(_, newValue) => {
|
|
||||||
field.onChange(newValue.map((m: Monitor) => m.id));
|
|
||||||
}}
|
|
||||||
fieldLabel={t(
|
|
||||||
"pages.statusPages.form.monitors.option.monitors.label"
|
|
||||||
)}
|
|
||||||
renderInput={(params) => (
|
|
||||||
<TextField
|
|
||||||
{...params}
|
|
||||||
placeholder={
|
|
||||||
selectedMonitors.length === 0
|
|
||||||
? t(
|
|
||||||
"pages.statusPages.form.monitors.option.monitors.placeholder"
|
|
||||||
)
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
error={!!fieldState.error}
|
|
||||||
helperText={fieldState.error?.message}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
{selectedMonitors.length > 0 && (
|
|
||||||
<DragDropContext onDragEnd={handleDragEnd}>
|
|
||||||
<Droppable droppableId="monitors-list">
|
|
||||||
{(provided) => (
|
|
||||||
<Stack
|
|
||||||
{...provided.droppableProps}
|
|
||||||
ref={provided.innerRef}
|
|
||||||
>
|
|
||||||
{selectedMonitors.map((monitor, index) => (
|
|
||||||
<Draggable
|
|
||||||
key={monitor.id}
|
|
||||||
draggableId={monitor.id}
|
|
||||||
index={index}
|
|
||||||
>
|
|
||||||
{(provided) => (
|
|
||||||
<Stack
|
|
||||||
ref={provided.innerRef}
|
|
||||||
{...provided.draggableProps}
|
|
||||||
{...provided.dragHandleProps}
|
|
||||||
direction="row"
|
|
||||||
alignItems="center"
|
|
||||||
spacing={theme.spacing(LAYOUT.XS)}
|
|
||||||
padding={theme.spacing(LAYOUT.XS)}
|
|
||||||
marginTop={theme.spacing(SPACING.LG)}
|
|
||||||
borderRadius={1}
|
|
||||||
sx={{
|
|
||||||
border: `1px solid ${theme.palette.divider}`,
|
|
||||||
cursor: "grab",
|
|
||||||
"&:active": { cursor: "grabbing" },
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<GripVertical size={20} />
|
|
||||||
<Typography
|
|
||||||
flexGrow={1}
|
|
||||||
>{`${monitor.name} (${getMonitorTypeLabel(monitor.type, t)})`}</Typography>
|
|
||||||
<IconButton
|
|
||||||
size="small"
|
|
||||||
onClick={() => {
|
|
||||||
field.onChange(
|
|
||||||
field.value.filter(
|
|
||||||
(id: string) => id !== monitor.id
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
aria-label="Remove monitor"
|
|
||||||
>
|
|
||||||
<Trash2 size={16} />
|
|
||||||
</IconButton>
|
|
||||||
</Stack>
|
|
||||||
)}
|
|
||||||
</Draggable>
|
|
||||||
))}
|
|
||||||
{provided.placeholder}
|
|
||||||
</Stack>
|
|
||||||
)}
|
|
||||||
</Droppable>
|
|
||||||
</DragDropContext>
|
|
||||||
)}
|
|
||||||
</Stack>
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<ConfigBox
|
|
||||||
title={t("pages.statusPages.form.timezone.title")}
|
|
||||||
subtitle={t("pages.statusPages.form.timezone.description")}
|
|
||||||
rightContent={
|
|
||||||
<Controller
|
|
||||||
name="timezone"
|
|
||||||
control={control}
|
|
||||||
render={({ field }) => (
|
|
||||||
<Autocomplete
|
|
||||||
options={timezones}
|
|
||||||
getOptionLabel={(option: TimezoneOption) => option.name}
|
|
||||||
value={
|
|
||||||
timezones.find((tz: TimezoneOption) => tz._id === field.value) ?? null
|
|
||||||
}
|
|
||||||
onChange={(_, newValue: TimezoneOption | null) => {
|
|
||||||
field.onChange(newValue?._id ?? "");
|
|
||||||
}}
|
|
||||||
fieldLabel={t("pages.statusPages.form.timezone.option.timezone.label")}
|
|
||||||
renderInput={(params) => (
|
|
||||||
<TextField
|
|
||||||
{...params}
|
|
||||||
placeholder={t(
|
|
||||||
"pages.statusPages.form.timezone.option.timezone.placeholder"
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
)}
|
<Typography>
|
||||||
/>
|
{t("pages.statusPages.form.access.option.published.name")}
|
||||||
}
|
</Typography>
|
||||||
/>
|
</Stack>
|
||||||
<ConfigBox
|
}
|
||||||
title={t("pages.statusPages.form.appearance.title")}
|
/>
|
||||||
subtitle={t("pages.statusPages.form.appearance.description")}
|
)}
|
||||||
rightContent={
|
{showStep(0) && (
|
||||||
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
<ConfigBox
|
||||||
<Stack alignItems={"center"}>
|
title={t("pages.statusPages.form.basicInfo.title")}
|
||||||
|
subtitle={t("pages.statusPages.form.basicInfo.description")}
|
||||||
|
rightContent={
|
||||||
|
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
||||||
<Controller
|
<Controller
|
||||||
name="logo"
|
name="companyName"
|
||||||
control={control}
|
control={control}
|
||||||
render={({ field }) => (
|
render={({ field, fieldState }) => (
|
||||||
<ImageUpload
|
<TextField
|
||||||
src={field.value?.data}
|
{...field}
|
||||||
onChange={(file) => {
|
fieldLabel={t("pages.statusPages.form.basicInfo.option.name.label")}
|
||||||
if (file) {
|
placeholder={t(
|
||||||
field.onChange({
|
"pages.statusPages.form.basicInfo.option.name.placeholder"
|
||||||
data: file.src,
|
)}
|
||||||
contentType: file.file.type,
|
error={!!fieldState.error}
|
||||||
});
|
helperText={fieldState.error?.message}
|
||||||
} else {
|
/>
|
||||||
field.onChange(null);
|
)}
|
||||||
}
|
/>
|
||||||
}}
|
<Controller
|
||||||
|
name="url"
|
||||||
|
control={control}
|
||||||
|
render={({ field, fieldState }) => (
|
||||||
|
<TextField
|
||||||
|
{...field}
|
||||||
|
fieldLabel={t("pages.statusPages.form.basicInfo.option.url.label")}
|
||||||
|
placeholder={t(
|
||||||
|
"pages.statusPages.form.basicInfo.option.url.placeholder"
|
||||||
|
)}
|
||||||
|
error={!!fieldState.error}
|
||||||
|
helperText={fieldState.error?.message}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(0) && (
|
||||||
|
<ConfigBox
|
||||||
|
title={t("pages.statusPages.form.monitors.title")}
|
||||||
|
subtitle={t("pages.statusPages.form.monitors.description")}
|
||||||
|
rightContent={
|
||||||
<Controller
|
<Controller
|
||||||
name="color"
|
name="monitors"
|
||||||
control={control}
|
control={control}
|
||||||
render={({ field }) => (
|
render={({ field, fieldState }) => {
|
||||||
<ColorInput
|
const selectedMonitors = field.value
|
||||||
format="hex"
|
.map((id: string) => monitors.find((m) => m.id === id))
|
||||||
value={field.value}
|
.filter((m): m is Monitor => m !== undefined);
|
||||||
onChange={field.onChange}
|
|
||||||
fieldLabel={t("pages.statusPages.form.appearance.option.color.label")}
|
const handleDragEnd = (result: DropResult) => {
|
||||||
/>
|
if (!result.destination) return;
|
||||||
)}
|
const reordered = Array.from(field.value);
|
||||||
/>
|
const [removed] = reordered.splice(result.source.index, 1);
|
||||||
</Stack>
|
reordered.splice(result.destination.index, 0, removed);
|
||||||
}
|
field.onChange(reordered);
|
||||||
/>
|
};
|
||||||
<ConfigBox
|
|
||||||
title={t("pages.statusPages.form.theme.title")}
|
return (
|
||||||
subtitle={t("pages.statusPages.form.theme.description")}
|
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
||||||
rightContent={
|
<Autocomplete
|
||||||
<Controller
|
multiple
|
||||||
name="theme"
|
options={monitors}
|
||||||
control={control}
|
getOptionLabel={(option: Monitor) => option.name}
|
||||||
render={({ field: themeField }) => (
|
value={selectedMonitors}
|
||||||
<Controller
|
onChange={(_, newValue) => {
|
||||||
name="themeMode"
|
field.onChange(newValue.map((m: Monitor) => m.id));
|
||||||
control={control}
|
}}
|
||||||
render={({ field: modeField }) => (
|
fieldLabel={t(
|
||||||
<ThemePicker
|
"pages.statusPages.form.monitors.option.monitors.label"
|
||||||
theme={themeField.value ?? "refined"}
|
)}
|
||||||
themeMode={modeField.value ?? "auto"}
|
renderInput={(params) => (
|
||||||
onThemeChange={themeField.onChange}
|
<TextField
|
||||||
onThemeModeChange={modeField.onChange}
|
{...params}
|
||||||
/>
|
placeholder={
|
||||||
)}
|
selectedMonitors.length === 0
|
||||||
/>
|
? t(
|
||||||
)}
|
"pages.statusPages.form.monitors.option.monitors.placeholder"
|
||||||
/>
|
)
|
||||||
}
|
: ""
|
||||||
/>
|
}
|
||||||
<ConfigBox
|
error={!!fieldState.error}
|
||||||
title={t("pages.statusPages.form.features.title")}
|
helperText={fieldState.error?.message}
|
||||||
subtitle={t("pages.statusPages.form.features.description")}
|
/>
|
||||||
rightContent={
|
)}
|
||||||
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
|
||||||
<Controller
|
|
||||||
name="showCharts"
|
|
||||||
control={control}
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormControlLabel
|
|
||||||
control={
|
|
||||||
<Checkbox
|
|
||||||
checked={field.value}
|
|
||||||
onChange={field.onChange}
|
|
||||||
/>
|
/>
|
||||||
}
|
{selectedMonitors.length > 0 && (
|
||||||
label={t("pages.statusPages.form.features.option.showCharts.label")}
|
<DragDropContext onDragEnd={handleDragEnd}>
|
||||||
/>
|
<Droppable droppableId="monitors-list">
|
||||||
)}
|
{(provided) => (
|
||||||
|
<Stack
|
||||||
|
{...provided.droppableProps}
|
||||||
|
ref={provided.innerRef}
|
||||||
|
>
|
||||||
|
{selectedMonitors.map((monitor, index) => (
|
||||||
|
<Draggable
|
||||||
|
key={monitor.id}
|
||||||
|
draggableId={monitor.id}
|
||||||
|
index={index}
|
||||||
|
>
|
||||||
|
{(provided) => (
|
||||||
|
<Stack
|
||||||
|
ref={provided.innerRef}
|
||||||
|
{...provided.draggableProps}
|
||||||
|
{...provided.dragHandleProps}
|
||||||
|
direction="row"
|
||||||
|
alignItems="center"
|
||||||
|
spacing={theme.spacing(LAYOUT.XS)}
|
||||||
|
padding={theme.spacing(LAYOUT.XS)}
|
||||||
|
marginTop={theme.spacing(SPACING.LG)}
|
||||||
|
borderRadius={1}
|
||||||
|
sx={{
|
||||||
|
border: `1px solid ${theme.palette.divider}`,
|
||||||
|
cursor: "grab",
|
||||||
|
"&:active": { cursor: "grabbing" },
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<GripVertical size={20} />
|
||||||
|
<Typography
|
||||||
|
flexGrow={1}
|
||||||
|
>{`${monitor.name} (${getMonitorTypeLabel(monitor.type, t)})`}</Typography>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={() => {
|
||||||
|
field.onChange(
|
||||||
|
field.value.filter(
|
||||||
|
(id: string) => id !== monitor.id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
aria-label="Remove monitor"
|
||||||
|
>
|
||||||
|
<Trash2 size={16} />
|
||||||
|
</IconButton>
|
||||||
|
</Stack>
|
||||||
|
)}
|
||||||
|
</Draggable>
|
||||||
|
))}
|
||||||
|
{provided.placeholder}
|
||||||
|
</Stack>
|
||||||
|
)}
|
||||||
|
</Droppable>
|
||||||
|
</DragDropContext>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(0) && (
|
||||||
|
<ConfigBox
|
||||||
|
title={t("pages.statusPages.form.timezone.title")}
|
||||||
|
subtitle={t("pages.statusPages.form.timezone.description")}
|
||||||
|
rightContent={
|
||||||
<Controller
|
<Controller
|
||||||
name="showInfrastructure"
|
name="timezone"
|
||||||
control={control}
|
control={control}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormControlLabel
|
<Autocomplete
|
||||||
control={
|
options={timezones}
|
||||||
<Checkbox
|
getOptionLabel={(option: TimezoneOption) => option.name}
|
||||||
checked={field.value}
|
value={
|
||||||
onChange={field.onChange}
|
timezones.find((tz: TimezoneOption) => tz._id === field.value) ?? null
|
||||||
/>
|
|
||||||
}
|
}
|
||||||
label={t(
|
onChange={(_, newValue: TimezoneOption | null) => {
|
||||||
"pages.statusPages.form.features.option.showInfrastructure.label"
|
field.onChange(newValue?._id ?? "");
|
||||||
|
}}
|
||||||
|
fieldLabel={t("pages.statusPages.form.timezone.option.timezone.label")}
|
||||||
|
renderInput={(params) => (
|
||||||
|
<TextField
|
||||||
|
{...params}
|
||||||
|
placeholder={t(
|
||||||
|
"pages.statusPages.form.timezone.option.timezone.placeholder"
|
||||||
|
)}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
{/* <Controller
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(1) && (
|
||||||
|
<ConfigBox
|
||||||
|
title={t("pages.statusPages.form.appearance.title")}
|
||||||
|
subtitle={t("pages.statusPages.form.appearance.description")}
|
||||||
|
rightContent={
|
||||||
|
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
||||||
|
<Stack alignItems={"center"}>
|
||||||
|
<Controller
|
||||||
|
name="logo"
|
||||||
|
control={control}
|
||||||
|
render={({ field }) => (
|
||||||
|
<ImageUpload
|
||||||
|
src={field.value?.data}
|
||||||
|
onChange={(file) => {
|
||||||
|
if (file) {
|
||||||
|
field.onChange({
|
||||||
|
data: file.src,
|
||||||
|
contentType: file.file.type,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
field.onChange(null);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
<Controller
|
||||||
|
name="color"
|
||||||
|
control={control}
|
||||||
|
render={({ field }) => (
|
||||||
|
<ColorInput
|
||||||
|
format="hex"
|
||||||
|
value={field.value}
|
||||||
|
onChange={field.onChange}
|
||||||
|
fieldLabel={t("pages.statusPages.form.appearance.option.color.label")}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</Stack>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(1) && (
|
||||||
|
<ConfigBox
|
||||||
|
title={t("pages.statusPages.form.theme.title")}
|
||||||
|
subtitle={t("pages.statusPages.form.theme.description")}
|
||||||
|
rightContent={
|
||||||
|
<Controller
|
||||||
|
name="theme"
|
||||||
|
control={control}
|
||||||
|
render={({ field: themeField }) => (
|
||||||
|
<Controller
|
||||||
|
name="themeMode"
|
||||||
|
control={control}
|
||||||
|
render={({ field: modeField }) => (
|
||||||
|
<ThemePicker
|
||||||
|
theme={themeField.value ?? "refined"}
|
||||||
|
themeMode={modeField.value ?? "auto"}
|
||||||
|
onThemeChange={themeField.onChange}
|
||||||
|
onThemeModeChange={modeField.onChange}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{showStep(1) && (
|
||||||
|
<ConfigBox
|
||||||
|
title={t("pages.statusPages.form.features.title")}
|
||||||
|
subtitle={t("pages.statusPages.form.features.description")}
|
||||||
|
rightContent={
|
||||||
|
<Stack spacing={theme.spacing(LAYOUT.MD)}>
|
||||||
|
<Controller
|
||||||
|
name="showCharts"
|
||||||
|
control={control}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormControlLabel
|
||||||
|
control={
|
||||||
|
<Checkbox
|
||||||
|
checked={field.value}
|
||||||
|
onChange={field.onChange}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
label={t("pages.statusPages.form.features.option.showCharts.label")}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
<Controller
|
||||||
|
name="showInfrastructure"
|
||||||
|
control={control}
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormControlLabel
|
||||||
|
control={
|
||||||
|
<Checkbox
|
||||||
|
checked={field.value}
|
||||||
|
onChange={field.onChange}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
label={t(
|
||||||
|
"pages.statusPages.form.features.option.showInfrastructure.label"
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
{/* <Controller
|
||||||
name="showUptimePercentage"
|
name="showUptimePercentage"
|
||||||
control={control}
|
control={control}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
@@ -542,21 +576,46 @@ const CreateStatusPage = () => {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/> */}
|
/> */}
|
||||||
</Stack>
|
</Stack>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
<Stack
|
<Stack
|
||||||
direction="row"
|
direction="row"
|
||||||
justifyContent="flex-end"
|
justifyContent={isCreate ? "space-between" : "flex-end"}
|
||||||
>
|
>
|
||||||
<Button
|
{isCreate && (
|
||||||
loading={isSubmitting}
|
<Button
|
||||||
type="submit"
|
type="button"
|
||||||
variant="contained"
|
variant="outlined"
|
||||||
color="primary"
|
color="secondary"
|
||||||
>
|
disabled={currentStep === 0}
|
||||||
{t("common.buttons.save")}
|
onClick={handleBack}
|
||||||
</Button>
|
>
|
||||||
|
{t("common.buttons.back")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{isCreate && currentStep < totalSteps - 1 ? (
|
||||||
|
<Button
|
||||||
|
key="wizard-next"
|
||||||
|
type="button"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
onClick={handleNext}
|
||||||
|
>
|
||||||
|
{t("common.buttons.next")}
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
key="wizard-save"
|
||||||
|
loading={isSubmitting}
|
||||||
|
type="submit"
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
>
|
||||||
|
{t("common.buttons.save")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
<Dialog
|
<Dialog
|
||||||
open={isDeleteDialogOpen}
|
open={isDeleteDialogOpen}
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
import type { MonitorType } from "./Monitor";
|
import type { MonitorType } from "./Monitor";
|
||||||
|
|
||||||
|
export const QueueModes = ["primary", "worker"] as const;
|
||||||
|
export type QueueMode = (typeof QueueModes)[number];
|
||||||
|
|
||||||
|
export type QueueWorker = {
|
||||||
|
workerId: string; // hostname:pid:uuid
|
||||||
|
mode: QueueMode;
|
||||||
|
lastSeenAt: number; // epoch ms of the last heartbeat
|
||||||
|
};
|
||||||
|
|
||||||
export interface QueueJobFailure {
|
export interface QueueJobFailure {
|
||||||
monitorId: string | number;
|
monitorId: string | number;
|
||||||
monitorUrl: string | null;
|
monitorUrl: string | null;
|
||||||
@@ -9,23 +18,26 @@ export interface QueueJobFailure {
|
|||||||
failReason: string | null;
|
failReason: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueueMetrics {
|
export type QueueMetrics = {
|
||||||
jobs: number;
|
jobs: number;
|
||||||
activeJobs: number;
|
activeJobs: number;
|
||||||
failingJobs: number;
|
failingJobs: number;
|
||||||
jobsWithFailures: QueueJobFailure[];
|
jobsWithFailures: QueueJobFailure[];
|
||||||
totalRuns: number;
|
totalRuns: number;
|
||||||
totalFailures: number;
|
totalFailures: number;
|
||||||
}
|
workers: QueueWorker[];
|
||||||
|
};
|
||||||
|
|
||||||
export interface QueueJobSummary {
|
export type QueueJobSummary = {
|
||||||
monitorId: string | number;
|
monitorId: string | number;
|
||||||
monitorUrl: string | null;
|
monitorUrl: string | null;
|
||||||
monitorType: MonitorType | null;
|
monitorType: string | null;
|
||||||
monitorInterval: number | null;
|
monitorInterval: number | null;
|
||||||
monitorGeoInterval: number | null;
|
monitorGeoInterval: number | null;
|
||||||
monitorActive: boolean | null;
|
monitorActive: boolean | null;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
|
lockedBy: string | null;
|
||||||
|
lockedUntil: number | null;
|
||||||
lockedAt: number | null;
|
lockedAt: number | null;
|
||||||
runCount: number;
|
runCount: number;
|
||||||
failCount: number;
|
failCount: number;
|
||||||
@@ -35,9 +47,10 @@ export interface QueueJobSummary {
|
|||||||
lastRunTook: number | null;
|
lastRunTook: number | null;
|
||||||
lastFailedAt: number | null;
|
lastFailedAt: number | null;
|
||||||
repeat: number | null;
|
repeat: number | null;
|
||||||
}
|
};
|
||||||
|
|
||||||
export interface QueueData {
|
export interface QueueData {
|
||||||
jobs: QueueJobSummary[];
|
jobs: QueueJobSummary[];
|
||||||
|
count: number;
|
||||||
metrics: QueueMetrics;
|
metrics: QueueMetrics;
|
||||||
}
|
}
|
||||||
@@ -128,19 +128,23 @@ const hardwareSchema = baseSchema.extend({
|
|||||||
cpuAlertThreshold: z
|
cpuAlertThreshold: z
|
||||||
.number()
|
.number()
|
||||||
.min(0, "CPU threshold must be at least 0")
|
.min(0, "CPU threshold must be at least 0")
|
||||||
.max(100, "CPU threshold must be at most 100"),
|
.max(100, "CPU threshold must be at most 100")
|
||||||
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
memoryAlertThreshold: z
|
memoryAlertThreshold: z
|
||||||
.number()
|
.number()
|
||||||
.min(0, "Memory threshold must be at least 0")
|
.min(0, "Memory threshold must be at least 0")
|
||||||
.max(100, "Memory threshold must be at most 100"),
|
.max(100, "Memory threshold must be at most 100")
|
||||||
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
diskAlertThreshold: z
|
diskAlertThreshold: z
|
||||||
.number()
|
.number()
|
||||||
.min(0, "Disk threshold must be at least 0")
|
.min(0, "Disk threshold must be at least 0")
|
||||||
.max(100, "Disk threshold must be at most 100"),
|
.max(100, "Disk threshold must be at most 100")
|
||||||
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
tempAlertThreshold: z
|
tempAlertThreshold: z
|
||||||
.number()
|
.number()
|
||||||
.min(0, "Temperature threshold must be at least 0")
|
.min(0, "Temperature threshold must be at least 0")
|
||||||
.max(150, "Temperature threshold must be at most 150"),
|
.max(150, "Temperature threshold must be at most 150")
|
||||||
|
.register(monitorStepRegistry, { step: 1 }),
|
||||||
selectedDisks: z.array(z.string()),
|
selectedDisks: z.array(z.string()),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import type { FieldPath } from "react-hook-form";
|
||||||
import { STATUS_PAGE_THEMES, STATUS_PAGE_THEME_MODES } from "@/Types/StatusPage";
|
import { STATUS_PAGE_THEMES, STATUS_PAGE_THEME_MODES } from "@/Types/StatusPage";
|
||||||
|
|
||||||
|
// Wizard step a field is validated on. Attached inline to each field below so
|
||||||
|
// the grouping lives next to the field definition; unannotated fields default
|
||||||
|
// to step 0. Read via `statusPageStepFieldsFor`. Mirrors the monitor schema's
|
||||||
|
// `monitorStepRegistry` so the create-status-page wizard can't drift from the
|
||||||
|
// field definitions.
|
||||||
|
export const statusPageStepRegistry = z.registry<{ step: number }>();
|
||||||
|
|
||||||
export const statusPageSchema = z.object({
|
export const statusPageSchema = z.object({
|
||||||
companyName: z
|
companyName: z
|
||||||
.string()
|
.string()
|
||||||
@@ -18,23 +26,51 @@ export const statusPageSchema = z.object({
|
|||||||
type: z
|
type: z
|
||||||
.array(z.enum(["uptime", "infrastructure"]))
|
.array(z.enum(["uptime", "infrastructure"]))
|
||||||
.min(1, "At least one type is required"),
|
.min(1, "At least one type is required"),
|
||||||
color: z.string().min(1, "Color is required"),
|
color: z.string().min(1, "Color is required").register(statusPageStepRegistry, {
|
||||||
|
step: 1,
|
||||||
|
}),
|
||||||
monitors: z.array(z.string()).min(1, "At least one monitor is required"),
|
monitors: z.array(z.string()).min(1, "At least one monitor is required"),
|
||||||
isPublished: z.boolean(),
|
isPublished: z.boolean(),
|
||||||
showCharts: z.boolean(),
|
showCharts: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
showUptimePercentage: z.boolean(),
|
showUptimePercentage: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
showAdminLoginLink: z.boolean(),
|
showAdminLoginLink: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
showInfrastructure: z.boolean(),
|
showInfrastructure: z.boolean().register(statusPageStepRegistry, { step: 1 }),
|
||||||
customCSS: z.string().optional(),
|
customCSS: z.string().optional().register(statusPageStepRegistry, { step: 1 }),
|
||||||
theme: z.enum(STATUS_PAGE_THEMES).optional(),
|
theme: z
|
||||||
themeMode: z.enum(STATUS_PAGE_THEME_MODES).optional(),
|
.enum(STATUS_PAGE_THEMES)
|
||||||
|
.optional()
|
||||||
|
.register(statusPageStepRegistry, { step: 1 }),
|
||||||
|
themeMode: z
|
||||||
|
.enum(STATUS_PAGE_THEME_MODES)
|
||||||
|
.optional()
|
||||||
|
.register(statusPageStepRegistry, { step: 1 }),
|
||||||
logo: z
|
logo: z
|
||||||
.object({
|
.object({
|
||||||
data: z.string(),
|
data: z.string(),
|
||||||
contentType: z.string(),
|
contentType: z.string(),
|
||||||
})
|
})
|
||||||
.nullable()
|
.nullable()
|
||||||
.optional(),
|
.optional()
|
||||||
|
.register(statusPageStepRegistry, { step: 1 }),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type StatusPageFormData = z.infer<typeof statusPageSchema>;
|
export type StatusPageFormData = z.infer<typeof statusPageSchema>;
|
||||||
|
|
||||||
|
// The step each field is validated on, from the per-field
|
||||||
|
// `statusPageStepRegistry` metadata (unannotated fields default to step 0).
|
||||||
|
const fieldSteps = (): [name: FieldPath<StatusPageFormData>, step: number][] =>
|
||||||
|
Object.entries(statusPageSchema.shape).map(([name, field]) => [
|
||||||
|
name as FieldPath<StatusPageFormData>,
|
||||||
|
statusPageStepRegistry.get(field)?.step ?? 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// The fields that belong to a given wizard step. Derived from the schema so it
|
||||||
|
// can't drift from the field definitions.
|
||||||
|
export const statusPageStepFieldsFor = (step: number): FieldPath<StatusPageFormData>[] =>
|
||||||
|
fieldSteps()
|
||||||
|
.filter(([, fieldStep]) => fieldStep === step)
|
||||||
|
.map(([name]) => name);
|
||||||
|
|
||||||
|
// Number of wizard steps, derived from the highest step its fields declare.
|
||||||
|
export const statusPageStepCount = (): number =>
|
||||||
|
Math.max(0, ...fieldSteps().map(([, step]) => step)) + 1;
|
||||||
@@ -933,6 +933,8 @@
|
|||||||
"logLevelSelect": {
|
"logLevelSelect": {
|
||||||
"label": "Log level"
|
"label": "Log level"
|
||||||
},
|
},
|
||||||
|
"workers": "Workers",
|
||||||
|
"workersExplainers": "Shows worker processes. Worker IDs are truncated, hover for full ID",
|
||||||
"jobQueue": "Job queue",
|
"jobQueue": "Job queue",
|
||||||
"jobQueueExplainer": "<highlight>Highlighted rows</highlight> are jobs currently being processed by a worker.",
|
"jobQueueExplainer": "<highlight>Highlighted rows</highlight> are jobs currently being processed by a worker.",
|
||||||
"failedJobs": "Failed jobs",
|
"failedJobs": "Failed jobs",
|
||||||
@@ -1000,6 +1002,11 @@
|
|||||||
},
|
},
|
||||||
"table": {
|
"table": {
|
||||||
"headers": {
|
"headers": {
|
||||||
|
"host": "Host",
|
||||||
|
"processId": "Process ID",
|
||||||
|
"workerId": "Worker ID",
|
||||||
|
"workerMode": "Worker mode",
|
||||||
|
"lastSeenAt": "Last seen at",
|
||||||
"state": "State",
|
"state": "State",
|
||||||
"timestamp": "Timestamp",
|
"timestamp": "Timestamp",
|
||||||
"level": "Level",
|
"level": "Level",
|
||||||
@@ -1010,6 +1017,7 @@
|
|||||||
"failCount": "Count",
|
"failCount": "Count",
|
||||||
"lastRunAt": "Last run at",
|
"lastRunAt": "Last run at",
|
||||||
"lockedAt": "Locked at",
|
"lockedAt": "Locked at",
|
||||||
|
"lockedBy": "Locked by",
|
||||||
"lastFinishedAt": "Last finished at",
|
"lastFinishedAt": "Last finished at",
|
||||||
"lastRunTook": "Last run took",
|
"lastRunTook": "Last run took",
|
||||||
"lastFailedAt": "Last failed",
|
"lastFailedAt": "Last failed",
|
||||||
|
|||||||
Generated
+4
-4
@@ -31,7 +31,7 @@
|
|||||||
"jmespath": "^0.16.0",
|
"jmespath": "^0.16.0",
|
||||||
"jsdom": "^26.1.0",
|
"jsdom": "^26.1.0",
|
||||||
"jsonwebtoken": "9.0.2",
|
"jsonwebtoken": "9.0.2",
|
||||||
"less-simple-scheduler": "2.2.0",
|
"less-simple-scheduler": "2.6.0",
|
||||||
"mailersend": "^2.2.0",
|
"mailersend": "^2.2.0",
|
||||||
"mjml": "^5.0.0-alpha.4",
|
"mjml": "^5.0.0-alpha.4",
|
||||||
"mongoose": "^8.3.3",
|
"mongoose": "^8.3.3",
|
||||||
@@ -10417,9 +10417,9 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/less-simple-scheduler": {
|
"node_modules/less-simple-scheduler": {
|
||||||
"version": "2.2.0",
|
"version": "2.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/less-simple-scheduler/-/less-simple-scheduler-2.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/less-simple-scheduler/-/less-simple-scheduler-2.6.0.tgz",
|
||||||
"integrity": "sha512-0bbFohlkcWZY2wfkGfDiYEbDxkshj0RuskkL0RLUDMn1Y3WjoPqy29PAnKNtnHG+DQqn0hMjZ0e3hVFJ4k76oA==",
|
"integrity": "sha512-Vi8ouiodLqe2fyh+DqCmxE5cCJZDI3xOyUAH4jcj9sf7GunLUzH71PBcYJy2P8lxTK3LxEkHtr1reymiogs6JQ==",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mongodb": "7.2.0"
|
"mongodb": "7.2.0"
|
||||||
|
|||||||
+4
-2
@@ -9,8 +9,10 @@
|
|||||||
"test:unit": "NODE_OPTIONS=--experimental-vm-modules jest --coverage --collectCoverageFrom='src/**/*.ts' test/unit",
|
"test:unit": "NODE_OPTIONS=--experimental-vm-modules jest --coverage --collectCoverageFrom='src/**/*.ts' test/unit",
|
||||||
"test:unit:services": "NODE_OPTIONS=--experimental-vm-modules jest --coverage --collectCoverageFrom='src/service/**/*.ts' test/unit/services test/unit/providers/network test/unit/providers/notifications",
|
"test:unit:services": "NODE_OPTIONS=--experimental-vm-modules jest --coverage --collectCoverageFrom='src/service/**/*.ts' test/unit/services test/unit/providers/network test/unit/providers/notifications",
|
||||||
"test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --no-coverage test/integration",
|
"test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --no-coverage test/integration",
|
||||||
"dev": "nodemon --exec tsx src/index.js",
|
|
||||||
"start": "node --watch ./dist/index.js",
|
"start": "node --watch ./dist/index.js",
|
||||||
|
"dev": "nodemon --exec tsx src/index.js",
|
||||||
|
"worker": "QUEUE_MODE=worker node ./dist/index.js",
|
||||||
|
"worker:dev": "QUEUE_MODE=worker nodemon --exec tsx src/index.js",
|
||||||
"clean": "rm -rf dist",
|
"clean": "rm -rf dist",
|
||||||
"build": "npm run clean && npm run build:openapi && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json && cp -r src/templates dist/templates",
|
"build": "npm run clean && npm run build:openapi && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json && cp -r src/templates dist/templates",
|
||||||
"build:openapi": "tsc --noEmit && tsx scripts/buildOpenApi.ts",
|
"build:openapi": "tsc --noEmit && tsx scripts/buildOpenApi.ts",
|
||||||
@@ -51,7 +53,7 @@
|
|||||||
"jmespath": "^0.16.0",
|
"jmespath": "^0.16.0",
|
||||||
"jsdom": "^26.1.0",
|
"jsdom": "^26.1.0",
|
||||||
"jsonwebtoken": "9.0.2",
|
"jsonwebtoken": "9.0.2",
|
||||||
"less-simple-scheduler": "2.2.0",
|
"less-simple-scheduler": "2.6.0",
|
||||||
"mailersend": "^2.2.0",
|
"mailersend": "^2.2.0",
|
||||||
"mjml": "^5.0.0-alpha.4",
|
"mjml": "^5.0.0-alpha.4",
|
||||||
"mongoose": "^8.3.3",
|
"mongoose": "^8.3.3",
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ import {
|
|||||||
MongoTeamsRepository,
|
MongoTeamsRepository,
|
||||||
MongoMaintenanceWindowsRepository,
|
MongoMaintenanceWindowsRepository,
|
||||||
MongoSettingsRepository,
|
MongoSettingsRepository,
|
||||||
|
MongoQueueWorkersRepository,
|
||||||
IMonitorsRepository,
|
IMonitorsRepository,
|
||||||
IChecksRepository,
|
IChecksRepository,
|
||||||
IGeoChecksRepository,
|
IGeoChecksRepository,
|
||||||
@@ -123,7 +124,8 @@ import {
|
|||||||
IMaintenanceWindowsRepository,
|
IMaintenanceWindowsRepository,
|
||||||
} from "@/repositories/index.js";
|
} from "@/repositories/index.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
// import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { type QueueMode } from "@/types/settings.js";
|
||||||
|
|
||||||
export type InitializedServices = {
|
export type InitializedServices = {
|
||||||
settingsService: ISettingsService;
|
settingsService: ISettingsService;
|
||||||
@@ -168,10 +170,12 @@ export const initializeServices = async ({
|
|||||||
logger,
|
logger,
|
||||||
envSettings,
|
envSettings,
|
||||||
settingsService,
|
settingsService,
|
||||||
|
queueMode = "primary",
|
||||||
}: {
|
}: {
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
envSettings: EnvConfig;
|
envSettings: EnvConfig;
|
||||||
settingsService: ISettingsService;
|
settingsService: ISettingsService;
|
||||||
|
queueMode?: QueueMode;
|
||||||
}): Promise<InitializedServices> => {
|
}): Promise<InitializedServices> => {
|
||||||
// Create DB
|
// Create DB
|
||||||
|
|
||||||
@@ -245,6 +249,7 @@ export const initializeServices = async ({
|
|||||||
const incidentsRepository = new MongoIncidentsRepository();
|
const incidentsRepository = new MongoIncidentsRepository();
|
||||||
const teamsRepository = new MongoTeamsRepository();
|
const teamsRepository = new MongoTeamsRepository();
|
||||||
const maintenanceWindowsRepository = new MongoMaintenanceWindowsRepository();
|
const maintenanceWindowsRepository = new MongoMaintenanceWindowsRepository();
|
||||||
|
const queueWorkersRepository = new MongoQueueWorkersRepository();
|
||||||
|
|
||||||
// Inject settings repository into settings service (now that DB is connected)
|
// Inject settings repository into settings service (now that DB is connected)
|
||||||
(settingsService as SettingsService).setRepository(settingsRepository);
|
(settingsService as SettingsService).setRepository(settingsRepository);
|
||||||
@@ -345,10 +350,19 @@ export const initializeServices = async ({
|
|||||||
geoChecksRepository
|
geoChecksRepository
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (queueMode === "worker" && envSettings.queueType !== "lessSimpleQueue") {
|
||||||
|
throw new AppError({
|
||||||
|
message: `Worker mode requires QUEUE_TYPE="lessSimpleQueue" got ${envSettings.queueType}`,
|
||||||
|
status: 500,
|
||||||
|
service: "config services",
|
||||||
|
method: "initializeServices",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let jobQueue: IJobQueue;
|
let jobQueue: IJobQueue;
|
||||||
switch (envSettings.queueType) {
|
switch (envSettings.queueType) {
|
||||||
case "lessSimpleQueue":
|
case "lessSimpleQueue":
|
||||||
jobQueue = await LessSimpleQueue.create(logger, queueHelper, monitorsRepository, envSettings);
|
jobQueue = await LessSimpleQueue.create(logger, queueHelper, monitorsRepository, queueWorkersRepository, envSettings, queueMode);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
jobQueue = await SuperSimpleQueue.create(logger, queueHelper, monitorsRepository);
|
jobQueue = await SuperSimpleQueue.create(logger, queueHelper, monitorsRepository);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { ISuperSimpleQueue } from "@/service/index.js";
|
import { IJobQueue } from "@/service/index.js";
|
||||||
|
import { getQueueJobsQueryValidation } from "@/validation/queueValidation.js";
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
const SERVICE_NAME = "JobQueueController";
|
const SERVICE_NAME = "JobQueueController";
|
||||||
@@ -12,8 +13,8 @@ export interface IJobQueueController {
|
|||||||
|
|
||||||
class JobQueueController implements IJobQueueController {
|
class JobQueueController implements IJobQueueController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
private jobQueue: ISuperSimpleQueue;
|
private jobQueue: IJobQueue;
|
||||||
constructor(jobQueue: ISuperSimpleQueue) {
|
constructor(jobQueue: IJobQueue) {
|
||||||
this.jobQueue = jobQueue;
|
this.jobQueue = jobQueue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,11 +37,12 @@ class JobQueueController implements IJobQueueController {
|
|||||||
|
|
||||||
getJobs = async (req: Request, res: Response, next: NextFunction) => {
|
getJobs = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const jobs = await this.jobQueue.getJobs();
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
||||||
|
const { jobs, count } = await this.jobQueue.getJobs(pagination);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Queue jobs fetched successfully",
|
msg: "Queue jobs fetched successfully",
|
||||||
data: jobs,
|
data: { jobs, count },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
@@ -49,12 +51,13 @@ class JobQueueController implements IJobQueueController {
|
|||||||
|
|
||||||
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const jobs = await this.jobQueue.getJobs();
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
||||||
|
const { jobs, count } = await this.jobQueue.getJobs(pagination);
|
||||||
const metrics = await this.jobQueue.getMetrics();
|
const metrics = await this.jobQueue.getMetrics();
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Queue metrics fetched successfully",
|
msg: "Queue metrics fetched successfully",
|
||||||
data: { jobs, metrics },
|
data: { jobs, count, metrics },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { Schema, model } from "mongoose";
|
||||||
|
import { QueueModes, type QueueMode } from "@/types/settings.js";
|
||||||
|
|
||||||
|
// Heartbeat presence record for a scheduler instance. _id is the scheduler's
|
||||||
|
// workerId (hostname:pid:uuid). The TTL index on lastSeenAt garbage-collects
|
||||||
|
// records for instances that have stopped heartbeating.
|
||||||
|
interface QueueWorkerDocument {
|
||||||
|
_id: string;
|
||||||
|
mode: QueueMode;
|
||||||
|
lastSeenAt: Date;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QueueWorkerSchema = new Schema<QueueWorkerDocument>(
|
||||||
|
{
|
||||||
|
_id: { type: String, required: true },
|
||||||
|
mode: { type: String, enum: QueueModes, required: true },
|
||||||
|
lastSeenAt: { type: Date, default: Date.now, expires: 30 },
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
const QueueWorkerModel = model<QueueWorkerDocument>("QueueWorker", QueueWorkerSchema);
|
||||||
|
|
||||||
|
export type { QueueWorkerDocument };
|
||||||
|
export { QueueWorkerModel };
|
||||||
|
export default QueueWorkerModel;
|
||||||
@@ -41,3 +41,6 @@ export * from "@/db/models/DLQItem.js";
|
|||||||
export { default as DLQItemModel } from "@/db/models/DLQItem.js";
|
export { default as DLQItemModel } from "@/db/models/DLQItem.js";
|
||||||
export * from "@/db/models/Tag.js";
|
export * from "@/db/models/Tag.js";
|
||||||
export { default as TagModel } from "@/db/models/Tag.js";
|
export { default as TagModel } from "@/db/models/Tag.js";
|
||||||
|
|
||||||
|
export * from "@/db/models/QueueWorker.js";
|
||||||
|
export { default as QueueWorkerModel } from "@/db/models/QueueWorker.js";
|
||||||
+19
-10
@@ -17,22 +17,31 @@ const startApp = async () => {
|
|||||||
// Validate environment variables first
|
// Validate environment variables first
|
||||||
const env = validateEnv();
|
const env = validateEnv();
|
||||||
|
|
||||||
|
// Create settings service (env only — DB repository injected after connect)
|
||||||
|
const settingsService = new SettingsService(env);
|
||||||
|
const envSettings = settingsService.loadSettings();
|
||||||
|
// Create logger
|
||||||
|
logger = new Logger({ envSettings });
|
||||||
|
|
||||||
|
// Initialize services (connects DB, creates repositories, injects settingsRepository)
|
||||||
|
const services = await initializeServices({ logger, envSettings, settingsService, queueMode: envSettings.queueMode });
|
||||||
|
|
||||||
|
// If this is a worker instance, we're done. No need for express
|
||||||
|
if (envSettings.queueMode === "worker") {
|
||||||
|
logger.info({
|
||||||
|
message: "Worker instance started. API will not be started",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
});
|
||||||
|
initShutdownListener(null, services);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// FE path
|
// FE path
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = path.dirname(__filename);
|
const __dirname = path.dirname(__filename);
|
||||||
const openApiSpec = JSON.parse(fs.readFileSync(path.join(__dirname, "../openapi.json"), "utf8"));
|
const openApiSpec = JSON.parse(fs.readFileSync(path.join(__dirname, "../openapi.json"), "utf8"));
|
||||||
const frontendPath = path.join(__dirname, "..", "public");
|
const frontendPath = path.join(__dirname, "..", "public");
|
||||||
|
|
||||||
// Create settings service (env only — DB repository injected after connect)
|
|
||||||
const settingsService = new SettingsService(env);
|
|
||||||
const envSettings = settingsService.loadSettings();
|
|
||||||
|
|
||||||
// Create logger
|
|
||||||
logger = new Logger({ envSettings });
|
|
||||||
|
|
||||||
// Initialize services (connects DB, creates repositories, injects settingsRepository)
|
|
||||||
const services = await initializeServices({ logger, envSettings, settingsService });
|
|
||||||
|
|
||||||
// Initialize controllers
|
// Initialize controllers
|
||||||
const controllers = initializeControllers(services);
|
const controllers = initializeControllers(services);
|
||||||
|
|
||||||
|
|||||||
@@ -42,3 +42,6 @@ export { default as MongoGeoChecksRepository } from "@/repositories/geo-checks/M
|
|||||||
|
|
||||||
export * from "@/repositories/dlq/IDLQRepository.js";
|
export * from "@/repositories/dlq/IDLQRepository.js";
|
||||||
export { default as MongoDLQRepository } from "@/repositories/dlq/MongoDLQRepository.js";
|
export { default as MongoDLQRepository } from "@/repositories/dlq/MongoDLQRepository.js";
|
||||||
|
|
||||||
|
export * from "@/repositories/queue-workers/IQueueWorkersRepository.js";
|
||||||
|
export { default as MongoQueueWorkersRepository } from "@/repositories/queue-workers/MongoQueueWorkersRepository.js";
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import type { QueueMode, QueueWorker } from "@/types/settings.js";
|
||||||
|
|
||||||
|
export interface IQueueWorkersRepository {
|
||||||
|
upsert(workerId: string, mode: QueueMode): Promise<void>;
|
||||||
|
findRecent(maxAgeMs: number): Promise<QueueWorker[]>;
|
||||||
|
deleteById(workerId: string): Promise<void>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import type { QueueMode, QueueWorker } from "@/types/settings.js";
|
||||||
|
import type { IQueueWorkersRepository } from "./IQueueWorkersRepository.js";
|
||||||
|
import type { QueueWorkerDocument } from "@/db/models/QueueWorker.js";
|
||||||
|
import { QueueWorkerModel } from "@/db/models/QueueWorker.js";
|
||||||
|
const SERVICE_NAME = "MongoQueueWorkersRepository";
|
||||||
|
|
||||||
|
class MongoQueueWorkersRepository implements IQueueWorkersRepository {
|
||||||
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
|
protected toEntity = (doc: QueueWorkerDocument): QueueWorker => {
|
||||||
|
return {
|
||||||
|
workerId: doc._id,
|
||||||
|
mode: doc.mode,
|
||||||
|
lastSeenAt: doc.lastSeenAt.getTime(),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
upsert = async (workerId: string, mode: QueueMode): Promise<void> => {
|
||||||
|
await QueueWorkerModel.updateOne({ _id: workerId }, { $set: { mode, lastSeenAt: new Date() } }, { upsert: true });
|
||||||
|
};
|
||||||
|
|
||||||
|
findRecent = async (maxAgeMs: number): Promise<QueueWorker[]> => {
|
||||||
|
const cutoff = new Date(Date.now() - maxAgeMs);
|
||||||
|
const docs = await QueueWorkerModel.find({ lastSeenAt: { $gte: cutoff } });
|
||||||
|
return docs.map(this.toEntity);
|
||||||
|
};
|
||||||
|
|
||||||
|
deleteById = async (workerId: string): Promise<void> => {
|
||||||
|
await QueueWorkerModel.deleteOne({ _id: workerId });
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MongoQueueWorkersRepository;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Monitor } from "@/types/monitor.js";
|
import { Monitor } from "@/types/monitor.js";
|
||||||
|
import { QueueWorker } from "@/types/settings.js";
|
||||||
|
|
||||||
export type QueueJobFailure = {
|
export type QueueJobFailure = {
|
||||||
monitorId: string | number;
|
monitorId: string | number;
|
||||||
@@ -16,6 +17,7 @@ export type QueueMetrics = {
|
|||||||
jobsWithFailures: QueueJobFailure[];
|
jobsWithFailures: QueueJobFailure[];
|
||||||
totalRuns: number;
|
totalRuns: number;
|
||||||
totalFailures: number;
|
totalFailures: number;
|
||||||
|
workers: QueueWorker[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QueueJobSummary = {
|
export type QueueJobSummary = {
|
||||||
@@ -26,6 +28,8 @@ export type QueueJobSummary = {
|
|||||||
monitorGeoInterval: number | null;
|
monitorGeoInterval: number | null;
|
||||||
monitorActive: boolean | null;
|
monitorActive: boolean | null;
|
||||||
active: boolean;
|
active: boolean;
|
||||||
|
lockedBy: string | null;
|
||||||
|
lockedUntil: number | null;
|
||||||
lockedAt: number | null;
|
lockedAt: number | null;
|
||||||
runCount: number;
|
runCount: number;
|
||||||
failCount: number;
|
failCount: number;
|
||||||
@@ -37,6 +41,16 @@ export type QueueJobSummary = {
|
|||||||
repeat: number | null;
|
repeat: number | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type QueueJobsPagination = {
|
||||||
|
page?: number;
|
||||||
|
rowsPerPage?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type QueueJobsPage = {
|
||||||
|
jobs: QueueJobSummary[];
|
||||||
|
count: number;
|
||||||
|
};
|
||||||
|
|
||||||
export interface IJobQueue {
|
export interface IJobQueue {
|
||||||
readonly serviceName: string;
|
readonly serviceName: string;
|
||||||
init(): Promise<boolean>;
|
init(): Promise<boolean>;
|
||||||
@@ -47,6 +61,6 @@ export interface IJobQueue {
|
|||||||
updateJob(monitor: Monitor): Promise<void>;
|
updateJob(monitor: Monitor): Promise<void>;
|
||||||
shutdown(): Promise<void>;
|
shutdown(): Promise<void>;
|
||||||
getMetrics(): Promise<QueueMetrics>;
|
getMetrics(): Promise<QueueMetrics>;
|
||||||
getJobs(): Promise<QueueJobSummary[]>;
|
getJobs(pagination: QueueJobsPagination): Promise<QueueJobsPage>;
|
||||||
flushQueues(): Promise<{ success: boolean }>;
|
flushQueues(): Promise<{ success: boolean }>;
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
import { IMonitorsRepository } from "@/repositories/index.js";
|
import { IMonitorsRepository, IQueueWorkersRepository } from "@/repositories/index.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import { MongoStore, Scheduler, IJob } from "less-simple-scheduler";
|
import { MongoStore, Scheduler, IJob, AddJobInput } from "less-simple-scheduler";
|
||||||
import { IQueueHelper } from "@/service/infrastructure/JobQueues/QueueHelper.js";
|
import { IQueueHelper } from "@/service/infrastructure/JobQueues/QueueHelper.js";
|
||||||
import { Monitor, supportsGeoCheck } from "@/types/monitor.js";
|
import { Monitor, supportsGeoCheck } from "@/types/monitor.js";
|
||||||
import { type QueueMetrics, IJobQueue } from "@/service/infrastructure/JobQueues/IJobQueue.js";
|
import { type QueueMetrics, type QueueJobsPagination, type QueueJobsPage, IJobQueue } from "@/service/infrastructure/JobQueues/IJobQueue.js";
|
||||||
|
import { type QueueMode } from "@/types/settings.js";
|
||||||
import { EnvConfig } from "@/service/system/settingsService.js";
|
import { EnvConfig } from "@/service/system/settingsService.js";
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
|
|
||||||
|
// How long after its last heartbeat a worker is still considered alive. Must be longer than the scheduler's heartbeat
|
||||||
|
const WORKER_TTL_MS = 30_000;
|
||||||
|
|
||||||
// For discriminating job types
|
// For discriminating job types
|
||||||
type MonitorJob = Omit<IJob, "data"> & { data: Monitor };
|
type MonitorJob = Omit<IJob, "data"> & { data: Monitor };
|
||||||
const MONITOR_TEMPLATES = new Set<string>(["monitor-job", "geo-check-job"]);
|
const MONITOR_TEMPLATES = new Set<string>(["monitor-job", "geo-check-job"]);
|
||||||
@@ -18,13 +22,25 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private helper: IQueueHelper;
|
private helper: IQueueHelper;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
|
private workersRepository: IQueueWorkersRepository;
|
||||||
private readonly scheduler: Scheduler;
|
private readonly scheduler: Scheduler;
|
||||||
|
private queueMode: QueueMode;
|
||||||
|
private heartbeatListener: ((workerId: string) => void) | null = null;
|
||||||
|
|
||||||
constructor(logger: ILogger, helper: IQueueHelper, monitorsRepository: IMonitorsRepository, scheduler: Scheduler) {
|
constructor(
|
||||||
|
logger: ILogger,
|
||||||
|
helper: IQueueHelper,
|
||||||
|
monitorsRepository: IMonitorsRepository,
|
||||||
|
workersRepository: IQueueWorkersRepository,
|
||||||
|
scheduler: Scheduler,
|
||||||
|
queueMode: QueueMode
|
||||||
|
) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.helper = helper;
|
this.helper = helper;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
|
this.workersRepository = workersRepository;
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
|
this.queueMode = queueMode;
|
||||||
this.registerListeners();
|
this.registerListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +65,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
|
|
||||||
this.scheduler.on("scheduler:error", (workerId, error) => {
|
this.scheduler.on("scheduler:error", (workerId, error) => {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: `w${workerId} error: ${error instanceof Error ? error.message : String(error)}`,
|
message: `${workerId} error: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
});
|
});
|
||||||
@@ -57,35 +73,35 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
|
|
||||||
this.scheduler.on("job:locked", (workerId, job) => {
|
this.scheduler.on("job:locked", (workerId, job) => {
|
||||||
this.logger.debug({
|
this.logger.debug({
|
||||||
message: `w${workerId} ${job.id} locked`,
|
message: `${workerId} ${job.id} locked`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.scheduler.on("job:abort", (workerId, job, reason) => {
|
this.scheduler.on("job:abort", (workerId, job, reason) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `w${workerId} ${job.id} aborted: ${reason}`,
|
message: `${workerId} ${job.id} aborted: ${reason}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.scheduler.on("job:attempt", (workerId, job, attempt) => {
|
this.scheduler.on("job:attempt", (workerId, job, attempt) => {
|
||||||
this.logger.debug({
|
this.logger.debug({
|
||||||
message: `w${workerId} ${job.id} attempt ${attempt}`,
|
message: `${workerId} ${job.id} attempt ${attempt}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.scheduler.on("job:complete", (workerId, job) => {
|
this.scheduler.on("job:complete", (workerId, job) => {
|
||||||
this.logger.debug({
|
this.logger.debug({
|
||||||
message: `w${workerId} ${job.id} completed successfully`,
|
message: `${workerId} ${job.id} completed successfully`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
this.scheduler.on("job:exhausted", (workerId, job, error) => {
|
this.scheduler.on("job:exhausted", (workerId, job, error) => {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
message: `w${workerId} ${job.id} exhausted all retries: ${error instanceof Error ? error.message : String(error)}`,
|
message: `${workerId} ${job.id} exhausted all retries: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
});
|
});
|
||||||
@@ -93,7 +109,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
|
|
||||||
this.scheduler.on("job:fail", (workerId, job, error, attempt) => {
|
this.scheduler.on("job:fail", (workerId, job, error, attempt) => {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `w${workerId} ${job.id} failed on attempt ${attempt}: ${error instanceof Error ? error.message : String(error)}`,
|
message: `${workerId} ${job.id} failed on attempt ${attempt}: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
});
|
});
|
||||||
@@ -101,41 +117,80 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
|
|
||||||
this.scheduler.on("job:start", (workerId, job) => {
|
this.scheduler.on("job:start", (workerId, job) => {
|
||||||
this.logger.debug({
|
this.logger.debug({
|
||||||
message: `w${workerId} ${job.id} started`,
|
message: `${workerId} ${job.id} started`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
static async create(logger: ILogger, helper: IQueueHelper, monitorsRepository: IMonitorsRepository, envSettings: EnvConfig) {
|
static async create(
|
||||||
|
logger: ILogger,
|
||||||
|
helper: IQueueHelper,
|
||||||
|
monitorsRepository: IMonitorsRepository,
|
||||||
|
workersRepository: IQueueWorkersRepository,
|
||||||
|
envSettings: EnvConfig,
|
||||||
|
queueMode: QueueMode
|
||||||
|
) {
|
||||||
const store = new MongoStore({
|
const store = new MongoStore({
|
||||||
url: envSettings.dbConnectionString ?? "mongodb://localhost:27017/uptime_db",
|
url: envSettings.dbConnectionString ?? "mongodb://localhost:27017/uptime_db",
|
||||||
});
|
});
|
||||||
const scheduler = new Scheduler(store, {
|
const scheduler = new Scheduler(store, {
|
||||||
concurrency: 50,
|
concurrency: envSettings.queueMode === "primary" ? 0 : 50,
|
||||||
|
processEvery: 100,
|
||||||
});
|
});
|
||||||
const instance = new LessSimpleQueue(logger, helper, monitorsRepository, scheduler);
|
const instance = new LessSimpleQueue(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode);
|
||||||
await instance.init();
|
await instance.init(queueMode);
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
init = async () => {
|
private startHeartbeat = (queueMode: QueueMode) => {
|
||||||
|
const beat = (
|
||||||
|
workerId: string // This is the heartbeat function that registers workers
|
||||||
|
) =>
|
||||||
|
this.workersRepository.upsert(workerId, queueMode).catch((error: unknown) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: `Worker heartbeat failed: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "startHeartbeat",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
beat(this.scheduler.workerId); // Beat immediately to register
|
||||||
|
if (this.heartbeatListener) {
|
||||||
|
this.scheduler.off("scheduler:heartbeat", this.heartbeatListener); // remove old listeners
|
||||||
|
}
|
||||||
|
this.heartbeatListener = beat; /// Keep a reference to the listener
|
||||||
|
this.scheduler.on("scheduler:heartbeat", beat); // beat on every heartbeat
|
||||||
|
};
|
||||||
|
|
||||||
|
init = async (queueMode: QueueMode = "primary") => {
|
||||||
try {
|
try {
|
||||||
await this.scheduler.start();
|
await this.scheduler.start();
|
||||||
this.scheduler.addTemplate("monitor-job", this.helper.getHeartbeatJob());
|
this.scheduler.addTemplate("monitor-job", this.helper.getHeartbeatJob());
|
||||||
this.scheduler.addTemplate("geo-check-job", this.helper.getHeartbeatGeoJob());
|
this.scheduler.addTemplate("geo-check-job", this.helper.getHeartbeatGeoJob());
|
||||||
this.scheduler.addTemplate("cleanup-orphaned", this.helper.getCleanupOrphanedJob());
|
this.scheduler.addTemplate("cleanup-orphaned", this.helper.getCleanupOrphanedJob());
|
||||||
this.scheduler.addTemplate("cleanup-retention-job", this.helper.getCleanupRetentionJob());
|
this.scheduler.addTemplate("cleanup-retention-job", this.helper.getCleanupRetentionJob());
|
||||||
|
|
||||||
|
// Register in the worker registry
|
||||||
|
this.startHeartbeat(queueMode);
|
||||||
|
|
||||||
|
// If this is a worker, return early - primary will populate queue
|
||||||
|
if (queueMode === "worker") {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Worker queue: ${this.scheduler.workerId} initialized`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "init",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const monitors = await this.monitorsRepository.findAll();
|
const monitors = await this.monitorsRepository.findAll();
|
||||||
if (!monitors) {
|
if (!monitors) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (const monitor of monitors) {
|
const jobInputs = monitors.flatMap((monitor) => this.buildJobInputs(monitor));
|
||||||
const randomOffset = Math.floor(Math.random() * 100);
|
if (jobInputs.length > 0) {
|
||||||
setTimeout(() => {
|
await this.scheduler.addJobs(jobInputs);
|
||||||
this.addJob(monitor.id, monitor);
|
|
||||||
}, randomOffset);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scheduler.addJob({ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true, upsert: true });
|
this.scheduler.addJob({ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true, upsert: true });
|
||||||
@@ -152,32 +207,38 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
addJob = async (monitorId: string, monitor: Monitor) => {
|
// Builds the scheduler inputs for a monitor: always a monitor-job, plus a
|
||||||
this.scheduler.addJob({
|
// geo-check job when the monitor supports and enables geo checks.
|
||||||
id: monitorId,
|
private buildJobInputs = (monitor: Monitor): AddJobInput[] => {
|
||||||
template: "monitor-job",
|
const inputs: AddJobInput[] = [
|
||||||
repeat: monitor.interval,
|
{
|
||||||
active: monitor.isActive,
|
id: monitor.id,
|
||||||
data: monitor,
|
template: "monitor-job",
|
||||||
upsert: true,
|
repeat: monitor.interval,
|
||||||
});
|
active: monitor.isActive,
|
||||||
|
data: monitor,
|
||||||
|
upsert: true,
|
||||||
|
jitter: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
// Return early if we don't need geo checks
|
if (supportsGeoCheck(monitor.type) && monitor.geoCheckEnabled) {
|
||||||
if (!supportsGeoCheck(monitor.type)) {
|
inputs.push({
|
||||||
return;
|
id: `${monitor.id}-geo`,
|
||||||
}
|
|
||||||
|
|
||||||
// Add geo check job if enabled for HTTP monitors
|
|
||||||
if (monitor.geoCheckEnabled) {
|
|
||||||
this.scheduler.addJob({
|
|
||||||
id: `${monitorId}-geo`,
|
|
||||||
template: "geo-check-job",
|
template: "geo-check-job",
|
||||||
repeat: monitor.geoCheckInterval,
|
repeat: monitor.geoCheckInterval,
|
||||||
active: monitor.isActive,
|
active: monitor.isActive,
|
||||||
data: monitor,
|
data: monitor,
|
||||||
upsert: true,
|
upsert: true,
|
||||||
|
jitter: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return inputs;
|
||||||
|
};
|
||||||
|
|
||||||
|
addJob = async (_monitorId: string, monitor: Monitor) => {
|
||||||
|
await this.scheduler.addJobs(this.buildJobInputs(monitor));
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteJob = async (monitor: Monitor) => {
|
deleteJob = async (monitor: Monitor) => {
|
||||||
@@ -272,56 +333,55 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
};
|
};
|
||||||
|
|
||||||
shutdown = async () => {
|
shutdown = async () => {
|
||||||
|
// Stop beating before we remove this worker, so a heartbeat firing between
|
||||||
|
// the remove and the scheduler stopping can't re-insert it.
|
||||||
|
if (this.heartbeatListener) {
|
||||||
|
this.scheduler.off("scheduler:heartbeat", this.heartbeatListener);
|
||||||
|
this.heartbeatListener = null;
|
||||||
|
}
|
||||||
|
await this.workersRepository.deleteById(this.scheduler.workerId).catch((error: unknown) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: `Failed to deregister worker on shutdown: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "shutdown",
|
||||||
|
});
|
||||||
|
});
|
||||||
this.scheduler.stop();
|
this.scheduler.stop();
|
||||||
};
|
};
|
||||||
|
|
||||||
getMetrics = async () => {
|
getMetrics = async (): Promise<QueueMetrics> => {
|
||||||
const jobs = await this.scheduler.getJobs();
|
const stats = await this.scheduler.getStats();
|
||||||
const metrics = jobs.reduce<QueueMetrics>(
|
|
||||||
(acc, job) => {
|
|
||||||
const runCount = job.runCount ?? 0;
|
|
||||||
const failCount = job.failCount ?? 0;
|
|
||||||
const lastFailedAt = job.lastFailedAt ?? 0;
|
|
||||||
const lastFinishedAt = job.lastFinishedAt ?? 0;
|
|
||||||
acc.totalRuns += runCount;
|
|
||||||
acc.totalFailures += failCount;
|
|
||||||
acc.jobs++;
|
|
||||||
if (failCount > 0 && lastFailedAt >= lastFinishedAt) {
|
|
||||||
acc.failingJobs++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (job.lockedAt) {
|
const jobsWithFailures = stats.jobsWithFailures.map((failure) => {
|
||||||
acc.activeJobs++;
|
// `data` is untyped at the store level: for monitor jobs it's the Monitor,
|
||||||
}
|
// for system jobs (e.g. cleanup) it has no url/type => null
|
||||||
|
const monitor = failure.data as Monitor | undefined;
|
||||||
|
return {
|
||||||
|
monitorId: failure.id,
|
||||||
|
monitorUrl: monitor?.url ?? null,
|
||||||
|
monitorType: monitor?.type ?? null,
|
||||||
|
failedAt: failure.failedAt,
|
||||||
|
failCount: failure.failCount,
|
||||||
|
failReason: failure.failReason,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
if (failCount > 0) {
|
const workers = await this.workersRepository.findRecent(WORKER_TTL_MS);
|
||||||
const monitor = isMonitorJob(job) ? job.data : undefined;
|
return {
|
||||||
acc.jobsWithFailures.push({
|
jobs: stats.jobs,
|
||||||
monitorId: job.id,
|
activeJobs: stats.activeJobs,
|
||||||
monitorUrl: monitor?.url || null,
|
failingJobs: stats.failingJobs,
|
||||||
monitorType: monitor?.type || null,
|
totalRuns: stats.totalRuns,
|
||||||
failedAt: job.lastFailedAt ?? null,
|
totalFailures: stats.totalFailures,
|
||||||
failCount,
|
jobsWithFailures,
|
||||||
failReason: job.lastError ?? null,
|
workers,
|
||||||
});
|
};
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
},
|
|
||||||
{
|
|
||||||
jobs: 0,
|
|
||||||
activeJobs: 0,
|
|
||||||
failingJobs: 0,
|
|
||||||
jobsWithFailures: [],
|
|
||||||
totalRuns: 0,
|
|
||||||
totalFailures: 0,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
return metrics;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
getJobs = async () => {
|
getJobs = async ({ page = 0, rowsPerPage = 0 }: QueueJobsPagination): Promise<QueueJobsPage> => {
|
||||||
const jobs = await this.scheduler.getJobs();
|
const listOptions = rowsPerPage > 0 ? { skip: Math.max(page, 0) * rowsPerPage, limit: rowsPerPage } : undefined;
|
||||||
return jobs.map((job) => {
|
const [jobs, count] = await Promise.all([this.scheduler.getJobs(listOptions), this.scheduler.countJobs()]);
|
||||||
|
const mapped = jobs.map((job) => {
|
||||||
const monitor = isMonitorJob(job) ? job.data : undefined;
|
const monitor = isMonitorJob(job) ? job.data : undefined;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -333,6 +393,8 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
monitorActive: monitor?.isActive ?? null,
|
monitorActive: monitor?.isActive ?? null,
|
||||||
active: job.active,
|
active: job.active,
|
||||||
repeat: job.repeat ?? null,
|
repeat: job.repeat ?? null,
|
||||||
|
lockedBy: job.lockedBy ?? null,
|
||||||
|
lockedUntil: job.lockedUntil ?? null,
|
||||||
lockedAt: job.lockedAt ?? null,
|
lockedAt: job.lockedAt ?? null,
|
||||||
runCount: job.runCount ?? 0,
|
runCount: job.runCount ?? 0,
|
||||||
failCount: job.failCount ?? 0,
|
failCount: job.failCount ?? 0,
|
||||||
@@ -343,11 +405,19 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
lastFailedAt: job.lastFailedAt ?? null,
|
lastFailedAt: job.lastFailedAt ?? null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return { jobs: mapped, count };
|
||||||
};
|
};
|
||||||
|
|
||||||
flushQueues = async () => {
|
flushQueues = async () => {
|
||||||
const stopRes = await this.scheduler.stop();
|
if (this.queueMode !== "primary") {
|
||||||
const flushRes = await this.scheduler.flushJobs();
|
// Don't allow workers to flush, though this isn't currently reachable since workers have no API endpoints
|
||||||
|
this.logger.warn({ message: "Ignoring flush on non-primary worker", service: SERVICE_NAME, method: "flushQueues" });
|
||||||
|
return { success: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
const flushRes = await this.scheduler.flushJobs(); // Less Simple Queue must flush before stopping as it needs DB
|
||||||
|
const stopRes = await this.scheduler.stop(); // Destructive, other workers will abort their jobs
|
||||||
const initRes = await this.init();
|
const initRes = await this.init();
|
||||||
return {
|
return {
|
||||||
success: Boolean(stopRes && flushRes && initRes),
|
success: Boolean(stopRes && flushRes && initRes),
|
||||||
|
|||||||
@@ -3,64 +3,19 @@ import { ILogger } from "@/utils/logger.js";
|
|||||||
import Scheduler from "super-simple-scheduler";
|
import Scheduler from "super-simple-scheduler";
|
||||||
import { IQueueHelper } from "@/service/infrastructure/JobQueues/QueueHelper.js";
|
import { IQueueHelper } from "@/service/infrastructure/JobQueues/QueueHelper.js";
|
||||||
import { Monitor, MonitorType, supportsGeoCheck } from "@/types/monitor.js";
|
import { Monitor, MonitorType, supportsGeoCheck } from "@/types/monitor.js";
|
||||||
|
import { hostname } from "node:os";
|
||||||
|
import { randomUUID } from "node:crypto";
|
||||||
|
import { QueueMode } from "@/types/settings.js";
|
||||||
|
import { IJobQueue, type QueueJobSummary } from "@/service/infrastructure/JobQueues/IJobQueue.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
|
|
||||||
type QueueJobFailure = {
|
export class SuperSimpleQueue implements IJobQueue {
|
||||||
monitorId: string | number;
|
|
||||||
monitorUrl: string | null;
|
|
||||||
monitorType: string | null;
|
|
||||||
failedAt: number | null;
|
|
||||||
failCount: number;
|
|
||||||
failReason: string | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
type QueueMetrics = {
|
|
||||||
jobs: number;
|
|
||||||
activeJobs: number;
|
|
||||||
failingJobs: number;
|
|
||||||
jobsWithFailures: QueueJobFailure[];
|
|
||||||
totalRuns: number;
|
|
||||||
totalFailures: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
type QueueJobSummary = {
|
|
||||||
monitorId: string | number;
|
|
||||||
monitorUrl: string | null;
|
|
||||||
monitorType: string | null;
|
|
||||||
monitorInterval: number | null;
|
|
||||||
monitorGeoInterval: number | null;
|
|
||||||
monitorActive: boolean | null;
|
|
||||||
active: boolean;
|
|
||||||
lockedAt: number | null;
|
|
||||||
runCount: number;
|
|
||||||
failCount: number;
|
|
||||||
failReason: string | null;
|
|
||||||
lastRunAt: number | null;
|
|
||||||
lastFinishedAt: number | null;
|
|
||||||
lastRunTook: number | null;
|
|
||||||
lastFailedAt: number | null;
|
|
||||||
repeat: number | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface ISuperSimpleQueue {
|
|
||||||
readonly serviceName: string;
|
|
||||||
init(): Promise<boolean>;
|
|
||||||
addJob(monitorId: string, monitor: Monitor): Promise<void>;
|
|
||||||
deleteJob(monitor: Monitor): Promise<void>;
|
|
||||||
pauseJob(monitor: Monitor): Promise<void>;
|
|
||||||
resumeJob(monitor: Monitor): Promise<void>;
|
|
||||||
updateJob(monitor: Monitor): Promise<void>;
|
|
||||||
shutdown(): Promise<void>;
|
|
||||||
getMetrics(): Promise<QueueMetrics>;
|
|
||||||
getJobs(): Promise<QueueJobSummary[]>;
|
|
||||||
flushQueues(): Promise<{ success: boolean }>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class SuperSimpleQueue implements ISuperSimpleQueue {
|
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private helper: IQueueHelper;
|
private helper: IQueueHelper;
|
||||||
|
private workerId: string;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private readonly scheduler: Scheduler;
|
private readonly scheduler: Scheduler;
|
||||||
|
|
||||||
@@ -70,6 +25,7 @@ export class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.registerListeners();
|
this.registerListeners();
|
||||||
|
this.workerId = `${hostname()}:${process.pid}:${randomUUID()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
@@ -311,7 +267,26 @@ export class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
|
|
||||||
getMetrics = async () => {
|
getMetrics = async () => {
|
||||||
const jobs = await this.scheduler.getJobs();
|
const jobs = await this.scheduler.getJobs();
|
||||||
const metrics = jobs.reduce(
|
const metrics: {
|
||||||
|
jobs: number;
|
||||||
|
activeJobs: number;
|
||||||
|
failingJobs: number;
|
||||||
|
jobsWithFailures: Array<{
|
||||||
|
monitorId: string | number;
|
||||||
|
monitorUrl: string;
|
||||||
|
monitorType: MonitorType;
|
||||||
|
failedAt: number | null;
|
||||||
|
failCount: number;
|
||||||
|
failReason: string | null;
|
||||||
|
}>;
|
||||||
|
totalRuns: number;
|
||||||
|
totalFailures: number;
|
||||||
|
workers: Array<{
|
||||||
|
workerId: string;
|
||||||
|
mode: QueueMode;
|
||||||
|
lastSeenAt: number;
|
||||||
|
}>;
|
||||||
|
} = jobs.reduce(
|
||||||
(acc, job) => {
|
(acc, job) => {
|
||||||
const runCount = job.runCount ?? 0;
|
const runCount = job.runCount ?? 0;
|
||||||
const failCount = job.failCount ?? 0;
|
const failCount = job.failCount ?? 0;
|
||||||
@@ -354,14 +329,21 @@ export class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
}>,
|
}>,
|
||||||
totalRuns: 0,
|
totalRuns: 0,
|
||||||
totalFailures: 0,
|
totalFailures: 0,
|
||||||
|
workers: [
|
||||||
|
{
|
||||||
|
workerId: this.workerId,
|
||||||
|
mode: "primary",
|
||||||
|
lastSeenAt: Date.now(),
|
||||||
|
},
|
||||||
|
],
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return metrics;
|
return metrics;
|
||||||
};
|
};
|
||||||
|
|
||||||
getJobs = async () => {
|
getJobs = async ({ page = 0, rowsPerPage = 0 }: { page?: number; rowsPerPage?: number }): Promise<{ jobs: QueueJobSummary[]; count: number }> => {
|
||||||
const jobs = await this.scheduler.getJobs();
|
const jobs = await this.scheduler.getJobs();
|
||||||
return jobs.map((job) => {
|
const mapped = jobs.map((job) => {
|
||||||
return {
|
return {
|
||||||
monitorId: job.id,
|
monitorId: job.id,
|
||||||
monitorUrl: job.data?.url || null,
|
monitorUrl: job.data?.url || null,
|
||||||
@@ -371,6 +353,8 @@ export class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
monitorActive: job.data?.isActive ?? null,
|
monitorActive: job.data?.isActive ?? null,
|
||||||
active: job.active,
|
active: job.active,
|
||||||
repeat: job.repeat ?? null,
|
repeat: job.repeat ?? null,
|
||||||
|
lockedBy: job.lockedAt ? this.workerId : null,
|
||||||
|
lockedUntil: null,
|
||||||
lockedAt: job.lockedAt ?? null,
|
lockedAt: job.lockedAt ?? null,
|
||||||
runCount: job.runCount ?? 0,
|
runCount: job.runCount ?? 0,
|
||||||
failCount: job.failCount ?? 0,
|
failCount: job.failCount ?? 0,
|
||||||
@@ -381,6 +365,12 @@ export class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
lastFailedAt: job.lastFailedAt ?? null,
|
lastFailedAt: job.lastFailedAt ?? null,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const start = Math.max(page, 0) * rowsPerPage;
|
||||||
|
return {
|
||||||
|
jobs: rowsPerPage > 0 ? mapped.slice(start, start + rowsPerPage) : mapped,
|
||||||
|
count: mapped.length,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
flushQueues = async () => {
|
flushQueues = async () => {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export class BufferService implements IBufferService {
|
|||||||
private geoChecksService: IGeoChecksService;
|
private geoChecksService: IGeoChecksService;
|
||||||
|
|
||||||
constructor(logger: ILogger, checkService: ICheckService, geoChecksService: IGeoChecksService, settingsService: ISettingsService) {
|
constructor(logger: ILogger, checkService: ICheckService, geoChecksService: IGeoChecksService, settingsService: ISettingsService) {
|
||||||
this.BUFFER_TIMEOUT = settingsService.getSettings().nodeEnv === "development" ? 10 : 1000 * 60 * 1; // 1 minute
|
this.BUFFER_TIMEOUT = settingsService.getSettings().nodeEnv === "development" ? 1000 : 1000 * 60 * 1; // 1 minute
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.checksService = checkService;
|
this.checksService = checkService;
|
||||||
this.geoChecksService = geoChecksService;
|
this.geoChecksService = geoChecksService;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { IStatusProvider } from "@/service/infrastructure/network/IStatusProvide
|
|||||||
import { HttpStatusPayload } from "@/types/network.js";
|
import { HttpStatusPayload } from "@/types/network.js";
|
||||||
import { MonitorStatusResponse } from "@/types/network.js";
|
import { MonitorStatusResponse } from "@/types/network.js";
|
||||||
import { Agent as HttpsAgent } from "https";
|
import { Agent as HttpsAgent } from "https";
|
||||||
|
import { Agent as HttpAgent } from "http";
|
||||||
import { Monitor, MonitorType } from "@/types/monitor.js";
|
import { Monitor, MonitorType } from "@/types/monitor.js";
|
||||||
import { NETWORK_ERROR, isStatusUp } from "@/service/infrastructure/network/utils.js";
|
import { NETWORK_ERROR, isStatusUp } from "@/service/infrastructure/network/utils.js";
|
||||||
import CacheableLookup from "cacheable-lookup";
|
import CacheableLookup from "cacheable-lookup";
|
||||||
@@ -11,6 +12,11 @@ import CacheableLookup from "cacheable-lookup";
|
|||||||
export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
||||||
readonly type = "http";
|
readonly type = "http";
|
||||||
|
|
||||||
|
// Shared, pooled agents reused across every check
|
||||||
|
private readonly httpAgent: HttpAgent;
|
||||||
|
private readonly httpsAgent: HttpsAgent;
|
||||||
|
private readonly httpsAgentInsecure: HttpsAgent;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private got: Got,
|
private got: Got,
|
||||||
private advancedMatcher: IAdvancedMatcher
|
private advancedMatcher: IAdvancedMatcher
|
||||||
@@ -23,6 +29,11 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
},
|
},
|
||||||
retry: { limit: 1 },
|
retry: { limit: 1 },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const agentOptions = { keepAlive: true, maxSockets: 256, maxFreeSockets: 256 };
|
||||||
|
this.httpAgent = new HttpAgent(agentOptions);
|
||||||
|
this.httpsAgent = new HttpsAgent({ ...agentOptions, rejectUnauthorized: true });
|
||||||
|
this.httpsAgentInsecure = new HttpsAgent({ ...agentOptions, rejectUnauthorized: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
supports(type: MonitorType) {
|
supports(type: MonitorType) {
|
||||||
@@ -33,7 +44,7 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
if (error instanceof HTTPError || error instanceof RequestError) {
|
if (error instanceof HTTPError || error instanceof RequestError) {
|
||||||
const statusCode = error.response?.statusCode;
|
const statusCode = error.response?.statusCode;
|
||||||
const statusUp = isStatusUp(statusCode, monitor.customUpCodes);
|
const statusUp = isStatusUp(statusCode, monitor.customUpCodes);
|
||||||
const responseTime = error.timings?.phases?.total ?? 0;
|
const responseTime = error.timings?.phases?.firstByte ?? error.timings?.phases?.total ?? 0;
|
||||||
|
|
||||||
if (!statusUp) {
|
if (!statusUp) {
|
||||||
return {
|
return {
|
||||||
@@ -119,7 +130,8 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
options.agent = {
|
options.agent = {
|
||||||
https: new HttpsAgent({ rejectUnauthorized: !ignoreTlsErrors }),
|
http: this.httpAgent,
|
||||||
|
https: ignoreTlsErrors ? this.httpsAgentInsecure : this.httpsAgent,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -135,7 +147,7 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
status: false,
|
status: false,
|
||||||
code: response.statusCode,
|
code: response.statusCode,
|
||||||
message: "Response is not JSON",
|
message: "Response is not JSON",
|
||||||
responseTime: response.timings.phases.total ?? 0,
|
responseTime: response.timings.phases.firstByte ?? 0,
|
||||||
timings: response.timings,
|
timings: response.timings,
|
||||||
payload: response.body as unknown as T,
|
payload: response.body as unknown as T,
|
||||||
};
|
};
|
||||||
@@ -161,7 +173,7 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
status: statusUp && matchResult.ok,
|
status: statusUp && matchResult.ok,
|
||||||
code: response.statusCode,
|
code: response.statusCode,
|
||||||
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
||||||
responseTime: response.timings.phases.total ?? 0,
|
responseTime: response.timings.phases.firstByte ?? 0,
|
||||||
timings: response.timings,
|
timings: response.timings,
|
||||||
payload,
|
payload,
|
||||||
extracted: matchResult.extracted,
|
extracted: matchResult.extracted,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ISettingsRepository } from "@/repositories/index.js";
|
import { ISettingsRepository } from "@/repositories/index.js";
|
||||||
import { type DbType, type QueueType, Settings, SettingsUpdate } from "@/types/index.js";
|
import { type DbType, type QueueType, type QueueMode, Settings, SettingsUpdate } from "@/types/index.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { ValidatedEnv } from "@/validation/envValidation.js";
|
import { ValidatedEnv } from "@/validation/envValidation.js";
|
||||||
import type { StringValue } from "ms";
|
import type { StringValue } from "ms";
|
||||||
@@ -14,6 +14,7 @@ export type EnvConfig = {
|
|||||||
dbConnectionString: string;
|
dbConnectionString: string;
|
||||||
dbType: DbType;
|
dbType: DbType;
|
||||||
queueType: QueueType;
|
queueType: QueueType;
|
||||||
|
queueMode: QueueMode;
|
||||||
statusPageThemesEnabled: boolean;
|
statusPageThemesEnabled: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -41,6 +42,7 @@ export class SettingsService implements ISettingsService {
|
|||||||
dbConnectionString: env.DB_CONNECTION_STRING,
|
dbConnectionString: env.DB_CONNECTION_STRING,
|
||||||
dbType: env.DB_TYPE,
|
dbType: env.DB_TYPE,
|
||||||
queueType: env.QUEUE_TYPE,
|
queueType: env.QUEUE_TYPE,
|
||||||
|
queueMode: env.QUEUE_MODE,
|
||||||
statusPageThemesEnabled: env.STATUS_PAGE_THEMES_ENABLED,
|
statusPageThemesEnabled: env.STATUS_PAGE_THEMES_ENABLED,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { InitializedServices } from "./config/services.js";
|
|||||||
import { logger } from "./utils/logger.js";
|
import { logger } from "./utils/logger.js";
|
||||||
import type { Server } from "http";
|
import type { Server } from "http";
|
||||||
|
|
||||||
export const initShutdownListener = (server: Server, services: InitializedServices) => {
|
export const initShutdownListener = (server: Server | null, services: InitializedServices) => {
|
||||||
const SERVICE_NAME = "Server";
|
const SERVICE_NAME = "Server";
|
||||||
|
|
||||||
let isShuttingDown = false;
|
let isShuttingDown = false;
|
||||||
@@ -15,7 +15,7 @@ export const initShutdownListener = (server: Server, services: InitializedServic
|
|||||||
logger.info({ message: "Attempting graceful shutdown" });
|
logger.info({ message: "Attempting graceful shutdown" });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
server.close();
|
server?.close();
|
||||||
await services.jobQueue.shutdown();
|
await services.jobQueue.shutdown();
|
||||||
await services.db.disconnect();
|
await services.db.disconnect();
|
||||||
logger.info({ message: "Graceful shutdown complete" });
|
logger.info({ message: "Graceful shutdown complete" });
|
||||||
|
|||||||
@@ -4,6 +4,15 @@ export type DbType = (typeof DbTypes)[number];
|
|||||||
export const QueueTypes = ["lessSimpleQueue", "superSimpleQueue"] as const;
|
export const QueueTypes = ["lessSimpleQueue", "superSimpleQueue"] as const;
|
||||||
export type QueueType = (typeof QueueTypes)[number];
|
export type QueueType = (typeof QueueTypes)[number];
|
||||||
|
|
||||||
|
export const QueueModes = ["primary", "worker"] as const;
|
||||||
|
export type QueueMode = (typeof QueueModes)[number];
|
||||||
|
|
||||||
|
export type QueueWorker = {
|
||||||
|
workerId: string; // hostname:pid:uuid
|
||||||
|
mode: QueueMode;
|
||||||
|
lastSeenAt: number; // epoch ms of the last heartbeat
|
||||||
|
};
|
||||||
|
|
||||||
export interface SettingsThresholds {
|
export interface SettingsThresholds {
|
||||||
cpu?: number;
|
cpu?: number;
|
||||||
memory?: number;
|
memory?: number;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { DbTypes, QueueTypes } from "@/types/settings.js";
|
import { DbTypes, QueueTypes, QueueModes } from "@/types/settings.js";
|
||||||
import { booleanCoercion } from "./shared.js";
|
import { booleanCoercion } from "./shared.js";
|
||||||
|
|
||||||
const envSchema = z.object({
|
const envSchema = z.object({
|
||||||
@@ -11,7 +11,9 @@ const envSchema = z.object({
|
|||||||
// Database
|
// Database
|
||||||
DB_CONNECTION_STRING: z.string().min(1, "Database connection string is required"),
|
DB_CONNECTION_STRING: z.string().min(1, "Database connection string is required"),
|
||||||
DB_TYPE: z.enum(DbTypes).default("mongodb"),
|
DB_TYPE: z.enum(DbTypes).default("mongodb"),
|
||||||
|
|
||||||
QUEUE_TYPE: z.enum(QueueTypes).default("superSimpleQueue"),
|
QUEUE_TYPE: z.enum(QueueTypes).default("superSimpleQueue"),
|
||||||
|
QUEUE_MODE: z.enum(QueueModes).default("primary"),
|
||||||
|
|
||||||
// JWT Authentication
|
// JWT Authentication
|
||||||
JWT_SECRET: z.string().min(1, "JWT_SECRET is required"),
|
JWT_SECRET: z.string().min(1, "JWT_SECRET is required"),
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
export const getQueueJobsQueryValidation = z.object({
|
||||||
|
page: z.coerce.number().int().min(0).optional(),
|
||||||
|
rowsPerPage: z.coerce.number().int().min(1).max(100).optional(),
|
||||||
|
});
|
||||||
@@ -70,7 +70,7 @@ const makeGotResponse = (overrides?: Record<string, any>) => ({
|
|||||||
statusMessage: "OK",
|
statusMessage: "OK",
|
||||||
headers: { "content-type": "text/html" },
|
headers: { "content-type": "text/html" },
|
||||||
body: "<html></html>",
|
body: "<html></html>",
|
||||||
timings: { phases: { total: 100 } },
|
timings: { phases: { firstByte: 100, total: 120 } },
|
||||||
...overrides,
|
...overrides,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -182,8 +182,8 @@ describe("HttpProvider", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("defaults responseTime to 0 when timings.phases.total is undefined", async () => {
|
it("defaults responseTime to 0 when timings.phases.firstByte is undefined", async () => {
|
||||||
mockGot.mockResolvedValue(makeGotResponse({ timings: { phases: { total: undefined } } }));
|
mockGot.mockResolvedValue(makeGotResponse({ timings: { phases: { firstByte: undefined } } }));
|
||||||
const { provider } = createProvider();
|
const { provider } = createProvider();
|
||||||
|
|
||||||
const result = await provider.handle(makeMonitor());
|
const result = await provider.handle(makeMonitor());
|
||||||
@@ -224,11 +224,11 @@ describe("HttpProvider", () => {
|
|||||||
expect(result.message).toBe("Response is not JSON");
|
expect(result.message).toBe("Response is not JSON");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("defaults responseTime to 0 in non-JSON jsonPath response when total is undefined", async () => {
|
it("defaults responseTime to 0 in non-JSON jsonPath response when firstByte is undefined", async () => {
|
||||||
mockGot.mockResolvedValue(
|
mockGot.mockResolvedValue(
|
||||||
makeGotResponse({
|
makeGotResponse({
|
||||||
headers: { "content-type": "text/html" },
|
headers: { "content-type": "text/html" },
|
||||||
timings: { phases: { total: undefined } },
|
timings: { phases: { firstByte: undefined } },
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
const { provider } = createProvider();
|
const { provider } = createProvider();
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ describe("BufferService", () => {
|
|||||||
const { logger } = createService("development");
|
const { logger } = createService("development");
|
||||||
expect(logger.info).toHaveBeenCalledWith(
|
expect(logger.info).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
message: expect.stringContaining("0.01s"),
|
message: expect.stringContaining("1s"),
|
||||||
service: "BufferService",
|
service: "BufferService",
|
||||||
method: "constructor",
|
method: "constructor",
|
||||||
})
|
})
|
||||||
@@ -231,7 +231,7 @@ describe("BufferService", () => {
|
|||||||
service.addToBuffer(makeCheck());
|
service.addToBuffer(makeCheck());
|
||||||
service.addGeoCheckToBuffer(makeGeoCheck());
|
service.addGeoCheckToBuffer(makeGeoCheck());
|
||||||
|
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
expect(checkService.createChecks).toHaveBeenCalled();
|
expect(checkService.createChecks).toHaveBeenCalled();
|
||||||
expect(geoChecksService.createGeoChecks).toHaveBeenCalled();
|
expect(geoChecksService.createGeoChecks).toHaveBeenCalled();
|
||||||
@@ -242,11 +242,11 @@ describe("BufferService", () => {
|
|||||||
(checkService.createChecks as jest.Mock).mockResolvedValue([]);
|
(checkService.createChecks as jest.Mock).mockResolvedValue([]);
|
||||||
|
|
||||||
service.addToBuffer(makeCheck());
|
service.addToBuffer(makeCheck());
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
// Add another check and advance again to confirm rescheduling
|
// Add another check and advance again to confirm rescheduling
|
||||||
service.addToBuffer(makeCheck({ id: "c2" }));
|
service.addToBuffer(makeCheck({ id: "c2" }));
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
expect(checkService.createChecks).toHaveBeenCalledTimes(2);
|
expect(checkService.createChecks).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
@@ -256,7 +256,7 @@ describe("BufferService", () => {
|
|||||||
(checkService.createChecks as jest.Mock).mockRejectedValueOnce(new Error("DB down"));
|
(checkService.createChecks as jest.Mock).mockRejectedValueOnce(new Error("DB down"));
|
||||||
service.addToBuffer(makeCheck());
|
service.addToBuffer(makeCheck());
|
||||||
|
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
expect(logger.error).toHaveBeenCalledWith(
|
expect(logger.error).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@@ -268,7 +268,7 @@ describe("BufferService", () => {
|
|||||||
// Should still reschedule — add another check and flush
|
// Should still reschedule — add another check and flush
|
||||||
(checkService.createChecks as jest.Mock).mockResolvedValue([]);
|
(checkService.createChecks as jest.Mock).mockResolvedValue([]);
|
||||||
service.addToBuffer(makeCheck({ id: "c2" }));
|
service.addToBuffer(makeCheck({ id: "c2" }));
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
expect(checkService.createChecks).toHaveBeenCalledTimes(2);
|
expect(checkService.createChecks).toHaveBeenCalledTimes(2);
|
||||||
});
|
});
|
||||||
@@ -278,7 +278,7 @@ describe("BufferService", () => {
|
|||||||
// Override flushBuffer to throw past its own try/catch
|
// Override flushBuffer to throw past its own try/catch
|
||||||
service.flushBuffer = jest.fn<() => Promise<void>>().mockRejectedValueOnce(new Error("unexpected"));
|
service.flushBuffer = jest.fn<() => Promise<void>>().mockRejectedValueOnce(new Error("unexpected"));
|
||||||
|
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
expect(logger.error).toHaveBeenCalledWith(
|
expect(logger.error).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@@ -292,7 +292,7 @@ describe("BufferService", () => {
|
|||||||
const { service, logger } = createService();
|
const { service, logger } = createService();
|
||||||
service.flushBuffer = jest.fn<() => Promise<void>>().mockRejectedValueOnce("string error");
|
service.flushBuffer = jest.fn<() => Promise<void>>().mockRejectedValueOnce("string error");
|
||||||
|
|
||||||
await jest.advanceTimersByTimeAsync(10);
|
await jest.advanceTimersByTimeAsync(1000);
|
||||||
|
|
||||||
expect(logger.error).toHaveBeenCalledWith(
|
expect(logger.error).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
|
|||||||
@@ -0,0 +1,791 @@
|
|||||||
|
import { describe, expect, it, jest } from "@jest/globals";
|
||||||
|
import { createMockLogger } from "../../helpers/createMockLogger.ts";
|
||||||
|
import type { Monitor } from "../../../src/types/monitor.ts";
|
||||||
|
|
||||||
|
// ── Mock Scheduler / MongoStore ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
const createScheduler = () => {
|
||||||
|
const listeners: Record<string, Function[]> = {};
|
||||||
|
return {
|
||||||
|
workerId: "host-1:1234:abcd",
|
||||||
|
on: jest.fn((event: string, cb: Function) => {
|
||||||
|
listeners[event] = listeners[event] || [];
|
||||||
|
listeners[event].push(cb);
|
||||||
|
}),
|
||||||
|
off: jest.fn((event: string, cb: Function) => {
|
||||||
|
listeners[event] = (listeners[event] || []).filter((fn) => fn !== cb);
|
||||||
|
}),
|
||||||
|
emit: (event: string, ...args: unknown[]) => {
|
||||||
|
(listeners[event] || []).forEach((cb) => cb(...args));
|
||||||
|
},
|
||||||
|
start: jest.fn().mockResolvedValue(true),
|
||||||
|
stop: jest.fn().mockResolvedValue(true),
|
||||||
|
addTemplate: jest.fn(),
|
||||||
|
addJob: jest.fn(),
|
||||||
|
addJobs: jest.fn().mockResolvedValue({ inserted: 0, upserted: 0, modified: 0, failures: [] }),
|
||||||
|
removeJob: jest.fn(),
|
||||||
|
getJob: jest.fn().mockResolvedValue(null),
|
||||||
|
getJobs: jest.fn().mockResolvedValue([]),
|
||||||
|
countJobs: jest.fn().mockResolvedValue(0),
|
||||||
|
getStats: jest.fn().mockResolvedValue({ jobs: 0, activeJobs: 0, failingJobs: 0, totalRuns: 0, totalFailures: 0, jobsWithFailures: [] }),
|
||||||
|
pauseJob: jest.fn().mockResolvedValue(true),
|
||||||
|
resumeJob: jest.fn().mockResolvedValue(true),
|
||||||
|
updateJob: jest.fn(),
|
||||||
|
flushJobs: jest.fn().mockResolvedValue(true),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockSchedulerInstance = createScheduler();
|
||||||
|
const MockScheduler = jest.fn().mockReturnValue(mockSchedulerInstance);
|
||||||
|
const MockMongoStore = jest.fn();
|
||||||
|
|
||||||
|
jest.unstable_mockModule("less-simple-scheduler", () => ({
|
||||||
|
Scheduler: MockScheduler,
|
||||||
|
MongoStore: MockMongoStore,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { LessSimpleQueue } = await import("../../../src/service/infrastructure/JobQueues/LessSimpleQueue.ts");
|
||||||
|
|
||||||
|
const createQueueHelper = () => ({
|
||||||
|
getHeartbeatJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
||||||
|
getHeartbeatGeoJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
||||||
|
getCleanupOrphanedJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
||||||
|
getCleanupRetentionJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createMonitorsRepo = () => ({
|
||||||
|
findAll: jest.fn().mockResolvedValue([]),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createWorkersRepo = () => ({
|
||||||
|
upsert: jest.fn().mockResolvedValue(undefined),
|
||||||
|
findRecent: jest.fn().mockResolvedValue([]),
|
||||||
|
deleteById: jest.fn().mockResolvedValue(undefined),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createQueue = (overrides?: Record<string, unknown>) => {
|
||||||
|
const logger = createMockLogger();
|
||||||
|
const helper = createQueueHelper();
|
||||||
|
const monitorsRepository = createMonitorsRepo();
|
||||||
|
const workersRepository = createWorkersRepo();
|
||||||
|
const scheduler = createScheduler();
|
||||||
|
|
||||||
|
const defaults = { logger, helper, monitorsRepository, workersRepository, scheduler, queueMode: "primary", ...overrides };
|
||||||
|
|
||||||
|
const queue = new LessSimpleQueue(
|
||||||
|
defaults.logger as any,
|
||||||
|
defaults.helper as any,
|
||||||
|
defaults.monitorsRepository as any,
|
||||||
|
defaults.workersRepository as any,
|
||||||
|
defaults.scheduler as any,
|
||||||
|
defaults.queueMode as any
|
||||||
|
);
|
||||||
|
return { queue, ...defaults };
|
||||||
|
};
|
||||||
|
|
||||||
|
const makeMonitor = (overrides?: Partial<Monitor>): Monitor =>
|
||||||
|
({
|
||||||
|
id: "mon-1",
|
||||||
|
teamId: "team-1",
|
||||||
|
type: "http",
|
||||||
|
interval: 60000,
|
||||||
|
isActive: true,
|
||||||
|
geoCheckEnabled: false,
|
||||||
|
geoCheckInterval: 300000,
|
||||||
|
geoCheckLocations: [],
|
||||||
|
...overrides,
|
||||||
|
}) as Monitor;
|
||||||
|
|
||||||
|
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("LessSimpleQueue", () => {
|
||||||
|
describe("serviceName", () => {
|
||||||
|
it("returns JobQueue", () => {
|
||||||
|
const { queue } = createQueue();
|
||||||
|
expect(queue.serviceName).toBe("JobQueue");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── init (primary) ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("init - primary mode", () => {
|
||||||
|
it("starts scheduler, registers all templates, seeds monitors and system jobs", async () => {
|
||||||
|
const { queue, scheduler, monitorsRepository } = createQueue();
|
||||||
|
|
||||||
|
const result = await queue.init();
|
||||||
|
|
||||||
|
expect(result).toBe(true);
|
||||||
|
expect(scheduler.start).toHaveBeenCalled();
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("monitor-job", expect.any(Function));
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("geo-check-job", expect.any(Function));
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("cleanup-orphaned", expect.any(Function));
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("cleanup-retention-job", expect.any(Function));
|
||||||
|
expect(monitorsRepository.findAll).toHaveBeenCalled();
|
||||||
|
expect(scheduler.addJob).toHaveBeenCalledWith(expect.objectContaining({ id: "cleanup-orphaned" }));
|
||||||
|
expect(scheduler.addJob).toHaveBeenCalledWith(expect.objectContaining({ id: "cleanup-retention" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("registers existing monitors in a single bulk write", async () => {
|
||||||
|
const monitors = [makeMonitor({ id: "m1" }), makeMonitor({ id: "m2" })];
|
||||||
|
const { queue, scheduler, monitorsRepository } = createQueue();
|
||||||
|
(monitorsRepository.findAll as jest.Mock).mockResolvedValue(monitors);
|
||||||
|
|
||||||
|
await queue.init();
|
||||||
|
|
||||||
|
expect(scheduler.addJobs).toHaveBeenCalledTimes(1);
|
||||||
|
expect(scheduler.addJobs).toHaveBeenCalledWith(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({ id: "m1", template: "monitor-job", jitter: true }),
|
||||||
|
expect.objectContaining({ id: "m2", template: "monitor-job", jitter: true }),
|
||||||
|
])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns true and skips system jobs when findAll returns null", async () => {
|
||||||
|
const { queue, monitorsRepository, scheduler } = createQueue();
|
||||||
|
(monitorsRepository.findAll as jest.Mock).mockResolvedValue(null);
|
||||||
|
|
||||||
|
const result = await queue.init();
|
||||||
|
|
||||||
|
expect(result).toBe(true);
|
||||||
|
expect(scheduler.addJob).not.toHaveBeenCalledWith(expect.objectContaining({ id: "cleanup-orphaned" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false and logs error on failure", async () => {
|
||||||
|
const { queue, logger, monitorsRepository } = createQueue();
|
||||||
|
(monitorsRepository.findAll as jest.Mock).mockRejectedValue(new Error("db down"));
|
||||||
|
|
||||||
|
const result = await queue.init();
|
||||||
|
|
||||||
|
expect(result).toBe(false);
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("db down") }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs error with String(error) for non-Error exceptions", async () => {
|
||||||
|
const { queue, logger, monitorsRepository } = createQueue();
|
||||||
|
(monitorsRepository.findAll as jest.Mock).mockRejectedValue("string error");
|
||||||
|
|
||||||
|
const result = await queue.init();
|
||||||
|
|
||||||
|
expect(result).toBe(false);
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("string error") }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── init (worker) ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("init - worker mode", () => {
|
||||||
|
it("starts scheduler and registers all templates", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
|
||||||
|
const result = await queue.init("worker");
|
||||||
|
|
||||||
|
expect(result).toBe(true);
|
||||||
|
expect(scheduler.start).toHaveBeenCalled();
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("monitor-job", expect.any(Function));
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("geo-check-job", expect.any(Function));
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("cleanup-orphaned", expect.any(Function));
|
||||||
|
expect(scheduler.addTemplate).toHaveBeenCalledWith("cleanup-retention-job", expect.any(Function));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does NOT seed monitor jobs (no findAll, primary populates the queue)", async () => {
|
||||||
|
const { queue, monitorsRepository } = createQueue();
|
||||||
|
|
||||||
|
await queue.init("worker");
|
||||||
|
|
||||||
|
expect(monitorsRepository.findAll).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does NOT seed cleanup system jobs", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
|
||||||
|
await queue.init("worker");
|
||||||
|
|
||||||
|
expect(scheduler.addJob).not.toHaveBeenCalledWith(expect.objectContaining({ id: "cleanup-orphaned" }));
|
||||||
|
expect(scheduler.addJob).not.toHaveBeenCalledWith(expect.objectContaining({ id: "cleanup-retention" }));
|
||||||
|
expect(scheduler.addJob).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs the initialized worker with its workerId", async () => {
|
||||||
|
const { queue, logger } = createQueue();
|
||||||
|
|
||||||
|
await queue.init("worker");
|
||||||
|
|
||||||
|
expect(logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: "Worker queue: host-1:1234:abcd initialized" }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── registerListeners ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("registerListeners (via constructor)", () => {
|
||||||
|
it("logs on scheduler:start", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("scheduler:start", "host-1:1234:abcd");
|
||||||
|
expect(logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: "Scheduler started" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on scheduler:stop", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("scheduler:stop", "host-1:1234:abcd");
|
||||||
|
expect(logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: "Scheduler stopped" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on scheduler:error with Error", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("scheduler:error", "w1", new Error("boom"));
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 error: boom" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on scheduler:error with non-Error", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("scheduler:error", "w1", "string error");
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 error: string error", stack: undefined }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:locked", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:locked", "w1", { id: "m1" });
|
||||||
|
expect(logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 locked" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:abort", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:abort", "w1", { id: "m1" }, "timeout");
|
||||||
|
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 aborted: timeout" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:attempt", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:attempt", "w1", { id: "m1" }, 2);
|
||||||
|
expect(logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 attempt 2" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:complete", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:complete", "w1", { id: "m1" });
|
||||||
|
expect(logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 completed successfully" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:exhausted with Error", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:exhausted", "w1", { id: "m1" }, new Error("gave up"));
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 exhausted all retries: gave up" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:exhausted with non-Error", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:exhausted", "w1", { id: "m1" }, "gave up");
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 exhausted all retries: gave up", stack: undefined }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:fail with Error", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:fail", "w1", { id: "m1" }, new Error("oops"), 1);
|
||||||
|
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 failed on attempt 1: oops" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:fail with non-Error", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:fail", "w1", { id: "m1" }, "oops", 1);
|
||||||
|
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 failed on attempt 1: oops", stack: undefined }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs on job:start", () => {
|
||||||
|
const { scheduler, logger } = createQueue();
|
||||||
|
scheduler.emit("job:start", "w1", { id: "m1" });
|
||||||
|
expect(logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: "w1 m1 started" }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── addJob ─────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("addJob", () => {
|
||||||
|
it("adds monitor job to scheduler", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.addJob("mon-1", makeMonitor());
|
||||||
|
expect(scheduler.addJobs).toHaveBeenCalledWith(
|
||||||
|
expect.arrayContaining([expect.objectContaining({ id: "mon-1", template: "monitor-job", repeat: 60000, active: true })])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds geo check job when geoCheckEnabled and type supports it", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.addJob("mon-1", makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckInterval: 300000 }));
|
||||||
|
expect(scheduler.addJobs).toHaveBeenCalledWith(
|
||||||
|
expect.arrayContaining([expect.objectContaining({ id: "mon-1-geo", template: "geo-check-job", repeat: 300000 })])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips geo job when type does not support geo checks", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.addJob("mon-1", makeMonitor({ geoCheckEnabled: true, type: "hardware" }));
|
||||||
|
expect(scheduler.addJobs).toHaveBeenCalledWith(expect.not.arrayContaining([expect.objectContaining({ id: "mon-1-geo" })]));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips geo job when geoCheckEnabled is false", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.addJob("mon-1", makeMonitor({ geoCheckEnabled: false, type: "http" }));
|
||||||
|
expect(scheduler.addJobs).toHaveBeenCalledWith(expect.not.arrayContaining([expect.objectContaining({ id: "mon-1-geo" })]));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── deleteJob ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("deleteJob", () => {
|
||||||
|
it("removes monitor job", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.deleteJob(makeMonitor());
|
||||||
|
expect(scheduler.removeJob).toHaveBeenCalledWith("mon-1");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes geo job if it exists", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
await queue.deleteJob(makeMonitor());
|
||||||
|
expect(scheduler.removeJob).toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not remove geo job if it does not exist", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.deleteJob(makeMonitor());
|
||||||
|
expect(scheduler.removeJob).not.toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── pauseJob ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("pauseJob", () => {
|
||||||
|
it("pauses monitor and geo job when geo exists", async () => {
|
||||||
|
const { queue, scheduler, logger } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
await queue.pauseJob(makeMonitor());
|
||||||
|
|
||||||
|
expect(scheduler.pauseJob).toHaveBeenCalledWith("mon-1");
|
||||||
|
expect(scheduler.pauseJob).toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
expect(logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: "Paused monitor mon-1" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not pause geo job when it does not exist", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.pauseJob(makeMonitor());
|
||||||
|
expect(scheduler.pauseJob).toHaveBeenCalledWith("mon-1");
|
||||||
|
expect(scheduler.pauseJob).not.toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws when scheduler.pauseJob returns false for the main job", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.pauseJob as jest.Mock).mockResolvedValue(false);
|
||||||
|
await expect(queue.pauseJob(makeMonitor())).rejects.toThrow("Failed to pause monitor");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs error but does not throw when geo pauseJob returns false", async () => {
|
||||||
|
const { queue, scheduler, logger } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
(scheduler.pauseJob as jest.Mock).mockImplementation(async (id: string) => id !== "mon-1-geo");
|
||||||
|
|
||||||
|
await expect(queue.pauseJob(makeMonitor())).resolves.toBeUndefined();
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "Failed to pause geo check job for monitor mon-1" }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── resumeJob ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("resumeJob", () => {
|
||||||
|
it("resumes monitor and geo job when geo exists", async () => {
|
||||||
|
const { queue, scheduler, logger } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
await queue.resumeJob(makeMonitor());
|
||||||
|
|
||||||
|
expect(scheduler.resumeJob).toHaveBeenCalledWith("mon-1");
|
||||||
|
expect(scheduler.resumeJob).toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
expect(logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: "Resumed monitor mon-1" }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not resume geo job when it does not exist", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.resumeJob(makeMonitor());
|
||||||
|
expect(scheduler.resumeJob).toHaveBeenCalledWith("mon-1");
|
||||||
|
expect(scheduler.resumeJob).not.toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws when scheduler.resumeJob returns false for the main job", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.resumeJob as jest.Mock).mockResolvedValue(false);
|
||||||
|
await expect(queue.resumeJob(makeMonitor())).rejects.toThrow("Failed to resume monitor");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("logs error but does not throw when geo resumeJob returns false", async () => {
|
||||||
|
const { queue, scheduler, logger } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
(scheduler.resumeJob as jest.Mock).mockImplementation(async (id: string) => id !== "mon-1-geo");
|
||||||
|
|
||||||
|
await expect(queue.resumeJob(makeMonitor())).resolves.toBeUndefined();
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "Failed to resume geo check job for monitor mon-1" }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── updateJob / syncGeoJob ─────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("updateJob", () => {
|
||||||
|
it("updates monitor job with new interval", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
const monitor = makeMonitor({ interval: 120000 });
|
||||||
|
await queue.updateJob(monitor);
|
||||||
|
expect(scheduler.updateJob).toHaveBeenCalledWith("mon-1", expect.objectContaining({ repeat: 120000, data: monitor }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes existing geo job when geoCheckEnabled is false", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
await queue.updateJob(makeMonitor({ geoCheckEnabled: false, type: "http" }));
|
||||||
|
expect(scheduler.removeJob).toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes existing geo job when type does not support geo checks", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
await queue.updateJob(makeMonitor({ geoCheckEnabled: true, type: "hardware" }));
|
||||||
|
expect(scheduler.removeJob).toHaveBeenCalledWith("mon-1-geo");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates existing geo job when geoCheckEnabled", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJob as jest.Mock).mockResolvedValue({ id: "mon-1-geo" });
|
||||||
|
const monitor = makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckInterval: 300000 });
|
||||||
|
await queue.updateJob(monitor);
|
||||||
|
expect(scheduler.updateJob).toHaveBeenCalledWith("mon-1-geo", expect.objectContaining({ repeat: 300000 }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates new geo job when enabled but no existing job", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
const monitor = makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckInterval: 300000 });
|
||||||
|
await queue.updateJob(monitor);
|
||||||
|
expect(scheduler.addJob).toHaveBeenCalledWith(expect.objectContaining({ id: "mon-1-geo", template: "geo-check-job" }));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── shutdown ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("shutdown", () => {
|
||||||
|
it("stops the scheduler", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
await queue.shutdown();
|
||||||
|
expect(scheduler.stop).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── getMetrics ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("getMetrics", () => {
|
||||||
|
it("returns empty metrics when no jobs", async () => {
|
||||||
|
const { queue } = createQueue();
|
||||||
|
const metrics = await queue.getMetrics();
|
||||||
|
expect(metrics).toEqual({ jobs: 0, activeJobs: 0, failingJobs: 0, jobsWithFailures: [], totalRuns: 0, totalFailures: 0, workers: [] });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("maps the store's aggregate counters through", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getStats as jest.Mock).mockResolvedValue({
|
||||||
|
jobs: 2,
|
||||||
|
activeJobs: 1,
|
||||||
|
failingJobs: 1,
|
||||||
|
totalRuns: 15,
|
||||||
|
totalFailures: 2,
|
||||||
|
jobsWithFailures: [],
|
||||||
|
});
|
||||||
|
const metrics = await queue.getMetrics();
|
||||||
|
expect(metrics).toMatchObject({ jobs: 2, activeJobs: 1, failingJobs: 1, totalRuns: 15, totalFailures: 2 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("derives monitor fields for failing monitor jobs from job data", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getStats as jest.Mock).mockResolvedValue({
|
||||||
|
jobs: 1,
|
||||||
|
activeJobs: 0,
|
||||||
|
failingJobs: 1,
|
||||||
|
totalRuns: 10,
|
||||||
|
totalFailures: 2,
|
||||||
|
jobsWithFailures: [{ id: "m1", data: { url: "http://a.com", type: "http" }, failedAt: 200, failCount: 2, failReason: "timeout" }],
|
||||||
|
});
|
||||||
|
const metrics = await queue.getMetrics();
|
||||||
|
expect(metrics.jobsWithFailures).toEqual([
|
||||||
|
{ monitorId: "m1", monitorUrl: "http://a.com", monitorType: "http", failedAt: 200, failCount: 2, failReason: "timeout" },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves monitor fields null for non-monitor (cleanup) failures", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getStats as jest.Mock).mockResolvedValue({
|
||||||
|
jobs: 1,
|
||||||
|
activeJobs: 0,
|
||||||
|
failingJobs: 1,
|
||||||
|
totalRuns: 1,
|
||||||
|
totalFailures: 1,
|
||||||
|
jobsWithFailures: [{ id: "cleanup-orphaned", data: undefined, failedAt: 100, failCount: 1, failReason: "boom" }],
|
||||||
|
});
|
||||||
|
const metrics = await queue.getMetrics();
|
||||||
|
expect(metrics.jobsWithFailures[0]).toEqual({
|
||||||
|
monitorId: "cleanup-orphaned",
|
||||||
|
monitorUrl: null,
|
||||||
|
monitorType: null,
|
||||||
|
failedAt: 100,
|
||||||
|
failCount: 1,
|
||||||
|
failReason: "boom",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── getMetrics: worker registry ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("getMetrics - worker registry", () => {
|
||||||
|
it("returns the alive workers from the registry", async () => {
|
||||||
|
const { queue, workersRepository } = createQueue();
|
||||||
|
const alive = [
|
||||||
|
{ workerId: "host:1:aaa", mode: "primary", lastSeenAt: 1000 },
|
||||||
|
{ workerId: "host:2:bbb", mode: "worker", lastSeenAt: 2000 },
|
||||||
|
];
|
||||||
|
(workersRepository.findRecent as jest.Mock).mockResolvedValue(alive);
|
||||||
|
|
||||||
|
const metrics = await queue.getMetrics();
|
||||||
|
|
||||||
|
expect(workersRepository.findRecent).toHaveBeenCalledWith(30000);
|
||||||
|
expect(metrics.workers).toEqual(alive);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── getJobs ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("getJobs", () => {
|
||||||
|
it("maps scheduler jobs to summaries", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
||||||
|
{
|
||||||
|
id: "m1",
|
||||||
|
template: "monitor-job",
|
||||||
|
active: true,
|
||||||
|
repeat: 60000,
|
||||||
|
lockedAt: null,
|
||||||
|
runCount: 5,
|
||||||
|
failCount: 1,
|
||||||
|
lastError: "timeout",
|
||||||
|
lastStartedAt: 1000,
|
||||||
|
lastFinishedAt: 1100,
|
||||||
|
lastFailedAt: 900,
|
||||||
|
data: { url: "http://a.com", type: "http", interval: 60000, geoCheckInterval: 300000, isActive: true },
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
const { jobs } = await queue.getJobs({});
|
||||||
|
|
||||||
|
expect(jobs).toHaveLength(1);
|
||||||
|
expect(jobs[0]).toEqual({
|
||||||
|
monitorId: "m1",
|
||||||
|
monitorUrl: "http://a.com",
|
||||||
|
monitorType: "http",
|
||||||
|
monitorInterval: 60000,
|
||||||
|
monitorGeoInterval: 300000,
|
||||||
|
monitorActive: true,
|
||||||
|
active: true,
|
||||||
|
repeat: 60000,
|
||||||
|
lockedBy: null,
|
||||||
|
lockedUntil: null,
|
||||||
|
lockedAt: null,
|
||||||
|
runCount: 5,
|
||||||
|
failCount: 1,
|
||||||
|
failReason: "timeout",
|
||||||
|
lastRunAt: 1000,
|
||||||
|
lastFinishedAt: 1100,
|
||||||
|
lastRunTook: 100,
|
||||||
|
lastFailedAt: 900,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null lastRunTook when job is locked", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
||||||
|
{
|
||||||
|
id: "m1",
|
||||||
|
template: "monitor-job",
|
||||||
|
active: true,
|
||||||
|
lockedAt: 999,
|
||||||
|
runCount: 1,
|
||||||
|
lastStartedAt: 1000,
|
||||||
|
lastFinishedAt: 1100,
|
||||||
|
data: { url: "a", type: "http", interval: 60000 },
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
const { jobs } = await queue.getJobs({});
|
||||||
|
expect(jobs[0].lastRunTook).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves monitor fields null for non-monitor jobs", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([{ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true, data: undefined }]);
|
||||||
|
const { jobs } = await queue.getJobs({});
|
||||||
|
expect(jobs[0].monitorUrl).toBeNull();
|
||||||
|
expect(jobs[0].monitorType).toBeNull();
|
||||||
|
expect(jobs[0].monitorInterval).toBeNull();
|
||||||
|
expect(jobs[0].monitorGeoInterval).toBeNull();
|
||||||
|
expect(jobs[0].monitorActive).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("pushes pagination to the scheduler and returns its total count", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
||||||
|
{ id: "m1", template: "monitor-job", active: true, data: { url: "a", type: "http", interval: 60000 } },
|
||||||
|
]);
|
||||||
|
(scheduler.countJobs as jest.Mock).mockResolvedValue(42);
|
||||||
|
|
||||||
|
const { jobs, count } = await queue.getJobs({ page: 2, rowsPerPage: 10 });
|
||||||
|
|
||||||
|
expect(scheduler.getJobs).toHaveBeenCalledWith({ skip: 20, limit: 10 });
|
||||||
|
expect(count).toBe(42);
|
||||||
|
expect(jobs).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("requests all jobs when rowsPerPage is omitted", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.countJobs as jest.Mock).mockResolvedValue(7);
|
||||||
|
|
||||||
|
const { count } = await queue.getJobs({});
|
||||||
|
|
||||||
|
expect(scheduler.getJobs).toHaveBeenCalledWith(undefined);
|
||||||
|
expect(count).toBe(7);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── flushQueues ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("flushQueues", () => {
|
||||||
|
it("stops, flushes, and reinitializes", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
const result = await queue.flushQueues();
|
||||||
|
expect(scheduler.stop).toHaveBeenCalled();
|
||||||
|
expect(scheduler.flushJobs).toHaveBeenCalled();
|
||||||
|
expect(result.success).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("flushes before stopping, so the store is still connected for removeAll", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
const order: string[] = [];
|
||||||
|
(scheduler.flushJobs as jest.Mock).mockImplementation(async () => {
|
||||||
|
order.push("flush");
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
(scheduler.stop as jest.Mock).mockImplementation(async () => {
|
||||||
|
order.push("stop");
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
await queue.flushQueues();
|
||||||
|
expect(order).toEqual(["flush", "stop"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses to flush on a non-primary worker without touching the scheduler", async () => {
|
||||||
|
const { queue, scheduler } = createQueue({ queueMode: "worker" });
|
||||||
|
const result = await queue.flushQueues();
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
expect(scheduler.flushJobs).not.toHaveBeenCalled();
|
||||||
|
expect(scheduler.stop).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false when flush fails", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.flushJobs as jest.Mock).mockResolvedValue(false);
|
||||||
|
const result = await queue.flushQueues();
|
||||||
|
expect(result.success).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── static create ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("static create", () => {
|
||||||
|
const envSettings = { dbConnectionString: "mongodb://localhost:27017/test_db" } as any;
|
||||||
|
|
||||||
|
it("creates a MongoStore + Scheduler and inits in primary mode", async () => {
|
||||||
|
const logger = createMockLogger();
|
||||||
|
const helper = createQueueHelper();
|
||||||
|
const monitorsRepository = createMonitorsRepo();
|
||||||
|
const workersRepository = createWorkersRepo();
|
||||||
|
|
||||||
|
const instance = await LessSimpleQueue.create(
|
||||||
|
logger as any,
|
||||||
|
helper as any,
|
||||||
|
monitorsRepository as any,
|
||||||
|
workersRepository as any,
|
||||||
|
envSettings,
|
||||||
|
"primary"
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(MockMongoStore).toHaveBeenCalledWith(expect.objectContaining({ url: "mongodb://localhost:27017/test_db" }));
|
||||||
|
expect(MockScheduler).toHaveBeenCalled();
|
||||||
|
expect(instance).toBeInstanceOf(LessSimpleQueue);
|
||||||
|
expect(mockSchedulerInstance.start).toHaveBeenCalled();
|
||||||
|
expect(monitorsRepository.findAll).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("inits in worker mode without seeding monitors", async () => {
|
||||||
|
const logger = createMockLogger();
|
||||||
|
const helper = createQueueHelper();
|
||||||
|
const monitorsRepository = createMonitorsRepo();
|
||||||
|
const workersRepository = createWorkersRepo();
|
||||||
|
|
||||||
|
const instance = await LessSimpleQueue.create(
|
||||||
|
logger as any,
|
||||||
|
helper as any,
|
||||||
|
monitorsRepository as any,
|
||||||
|
workersRepository as any,
|
||||||
|
envSettings,
|
||||||
|
"worker"
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(instance).toBeInstanceOf(LessSimpleQueue);
|
||||||
|
expect(monitorsRepository.findAll).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── worker heartbeat / deregistration ────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("worker heartbeat", () => {
|
||||||
|
it("writes a heartbeat on init with the workerId and mode (primary)", async () => {
|
||||||
|
const { queue, workersRepository } = createQueue();
|
||||||
|
await queue.init("primary");
|
||||||
|
expect(workersRepository.upsert).toHaveBeenCalledWith("host-1:1234:abcd", "primary");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("writes a heartbeat on init with the workerId and mode (worker)", async () => {
|
||||||
|
const { queue, workersRepository } = createQueue();
|
||||||
|
await queue.init("worker");
|
||||||
|
expect(workersRepository.upsert).toHaveBeenCalledWith("host-1:1234:abcd", "worker");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("beats again on each scheduler:heartbeat event", async () => {
|
||||||
|
const { queue, scheduler, workersRepository } = createQueue();
|
||||||
|
await queue.init("worker");
|
||||||
|
(workersRepository.upsert as jest.Mock).mockClear();
|
||||||
|
|
||||||
|
scheduler.emit("scheduler:heartbeat", "host-1:1234:abcd");
|
||||||
|
scheduler.emit("scheduler:heartbeat", "host-1:1234:abcd");
|
||||||
|
|
||||||
|
expect(workersRepository.upsert).toHaveBeenCalledTimes(2);
|
||||||
|
expect(workersRepository.upsert).toHaveBeenCalledWith("host-1:1234:abcd", "worker");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stops beating after shutdown", async () => {
|
||||||
|
const { queue, scheduler, workersRepository } = createQueue();
|
||||||
|
await queue.init("worker");
|
||||||
|
await queue.shutdown();
|
||||||
|
(workersRepository.upsert as jest.Mock).mockClear();
|
||||||
|
|
||||||
|
scheduler.emit("scheduler:heartbeat", "host-1:1234:abcd");
|
||||||
|
|
||||||
|
expect(workersRepository.upsert).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("deregisters the worker and stops the scheduler on shutdown", async () => {
|
||||||
|
const { queue, scheduler, workersRepository } = createQueue();
|
||||||
|
await queue.init("worker");
|
||||||
|
await queue.shutdown();
|
||||||
|
expect(workersRepository.deleteById).toHaveBeenCalledWith("host-1:1234:abcd");
|
||||||
|
expect(scheduler.stop).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -486,10 +486,25 @@ describe("SuperSimpleQueue", () => {
|
|||||||
// ── getMetrics ───────────────────────────────────────────────────────────
|
// ── getMetrics ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
describe("getMetrics", () => {
|
describe("getMetrics", () => {
|
||||||
it("returns empty metrics when no jobs", async () => {
|
it("returns empty job metrics plus this process as the sole worker when no jobs", async () => {
|
||||||
const { queue } = createQueue();
|
const { queue } = createQueue();
|
||||||
const metrics = await queue.getMetrics();
|
const metrics = await queue.getMetrics();
|
||||||
expect(metrics).toEqual({ jobs: 0, activeJobs: 0, failingJobs: 0, jobsWithFailures: [], totalRuns: 0, totalFailures: 0 });
|
expect(metrics).toEqual({
|
||||||
|
jobs: 0,
|
||||||
|
activeJobs: 0,
|
||||||
|
failingJobs: 0,
|
||||||
|
jobsWithFailures: [],
|
||||||
|
totalRuns: 0,
|
||||||
|
totalFailures: 0,
|
||||||
|
workers: [
|
||||||
|
{
|
||||||
|
// hostname:pid:uuid, matching less-simple-scheduler's workerId format
|
||||||
|
workerId: expect.stringMatching(/^.+:\d+:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/),
|
||||||
|
mode: "primary",
|
||||||
|
lastSeenAt: expect.any(Number),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("aggregates metrics from jobs", async () => {
|
it("aggregates metrics from jobs", async () => {
|
||||||
@@ -568,7 +583,7 @@ describe("SuperSimpleQueue", () => {
|
|||||||
data: { url: "http://a.com", type: "http", interval: 60000, geoCheckInterval: 300000, isActive: true },
|
data: { url: "http://a.com", type: "http", interval: 60000, geoCheckInterval: 300000, isActive: true },
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
const jobs = await queue.getJobs();
|
const { jobs } = await queue.getJobs({});
|
||||||
|
|
||||||
expect(jobs).toHaveLength(1);
|
expect(jobs).toHaveLength(1);
|
||||||
expect(jobs[0]).toEqual({
|
expect(jobs[0]).toEqual({
|
||||||
@@ -580,6 +595,8 @@ describe("SuperSimpleQueue", () => {
|
|||||||
monitorActive: true,
|
monitorActive: true,
|
||||||
active: true,
|
active: true,
|
||||||
repeat: 60000,
|
repeat: 60000,
|
||||||
|
lockedBy: null,
|
||||||
|
lockedUntil: null,
|
||||||
lockedAt: null,
|
lockedAt: null,
|
||||||
runCount: 5,
|
runCount: 5,
|
||||||
failCount: 1,
|
failCount: 1,
|
||||||
@@ -596,14 +613,14 @@ describe("SuperSimpleQueue", () => {
|
|||||||
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
||||||
{ id: "m1", active: true, lockedAt: 999, runCount: 1, data: { url: "a", type: "http", interval: 60000 } },
|
{ id: "m1", active: true, lockedAt: 999, runCount: 1, data: { url: "a", type: "http", interval: 60000 } },
|
||||||
]);
|
]);
|
||||||
const jobs = await queue.getJobs();
|
const { jobs } = await queue.getJobs({});
|
||||||
expect(jobs[0].lastRunTook).toBeNull();
|
expect(jobs[0].lastRunTook).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles jobs with missing data", async () => {
|
it("handles jobs with missing data", async () => {
|
||||||
const { queue, scheduler } = createQueue();
|
const { queue, scheduler } = createQueue();
|
||||||
(scheduler.getJobs as jest.Mock).mockResolvedValue([{ id: "m1", active: true, data: null }]);
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([{ id: "m1", active: true, data: null }]);
|
||||||
const jobs = await queue.getJobs();
|
const { jobs } = await queue.getJobs({});
|
||||||
expect(jobs[0].monitorUrl).toBeNull();
|
expect(jobs[0].monitorUrl).toBeNull();
|
||||||
expect(jobs[0].monitorType).toBeNull();
|
expect(jobs[0].monitorType).toBeNull();
|
||||||
expect(jobs[0].monitorInterval).toBeNull();
|
expect(jobs[0].monitorInterval).toBeNull();
|
||||||
@@ -618,7 +635,7 @@ describe("SuperSimpleQueue", () => {
|
|||||||
{ id: "m1", active: true, data: { url: "a", type: "http", interval: 60000, isActive: true } },
|
{ id: "m1", active: true, data: { url: "a", type: "http", interval: 60000, isActive: true } },
|
||||||
{ id: "m2", active: false, data: { url: "b", type: "http", interval: 60000, isActive: false } },
|
{ id: "m2", active: false, data: { url: "b", type: "http", interval: 60000, isActive: false } },
|
||||||
]);
|
]);
|
||||||
const jobs = await queue.getJobs();
|
const { jobs } = await queue.getJobs({});
|
||||||
expect(jobs[0].monitorActive).toBe(true);
|
expect(jobs[0].monitorActive).toBe(true);
|
||||||
expect(jobs[1].monitorActive).toBe(false);
|
expect(jobs[1].monitorActive).toBe(false);
|
||||||
});
|
});
|
||||||
@@ -628,10 +645,33 @@ describe("SuperSimpleQueue", () => {
|
|||||||
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
(scheduler.getJobs as jest.Mock).mockResolvedValue([
|
||||||
{ id: "m1", active: true, repeat: 120000, data: { url: "a", type: "http", interval: 60000, geoCheckInterval: 600000 } },
|
{ id: "m1", active: true, repeat: 120000, data: { url: "a", type: "http", interval: 60000, geoCheckInterval: 600000 } },
|
||||||
]);
|
]);
|
||||||
const jobs = await queue.getJobs();
|
const { jobs } = await queue.getJobs({});
|
||||||
expect(jobs[0].repeat).toBe(120000);
|
expect(jobs[0].repeat).toBe(120000);
|
||||||
expect(jobs[0].monitorGeoInterval).toBe(600000);
|
expect(jobs[0].monitorGeoInterval).toBe(600000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("paginates jobs and returns the total count", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJobs as jest.Mock).mockResolvedValue(
|
||||||
|
Array.from({ length: 25 }, (_, i) => ({ id: `m${i}`, active: true, data: { url: `a${i}`, type: "http", interval: 60000 } }))
|
||||||
|
);
|
||||||
|
|
||||||
|
const { jobs, count } = await queue.getJobs({ page: 1, rowsPerPage: 10 });
|
||||||
|
expect(count).toBe(25);
|
||||||
|
expect(jobs).toHaveLength(10);
|
||||||
|
expect(jobs[0].monitorId).toBe("m10");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns all jobs when rowsPerPage is omitted", async () => {
|
||||||
|
const { queue, scheduler } = createQueue();
|
||||||
|
(scheduler.getJobs as jest.Mock).mockResolvedValue(
|
||||||
|
Array.from({ length: 25 }, (_, i) => ({ id: `m${i}`, active: true, data: { url: `a${i}`, type: "http", interval: 60000 } }))
|
||||||
|
);
|
||||||
|
|
||||||
|
const { jobs, count } = await queue.getJobs({});
|
||||||
|
expect(count).toBe(25);
|
||||||
|
expect(jobs).toHaveLength(25);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── flushQueues ──────────────────────────────────────────────────────────
|
// ── flushQueues ──────────────────────────────────────────────────────────
|
||||||
|
|||||||
Reference in new issue
Block a user