Update various configuration files, components, and assets; enhance notification system and API endpoints; improve documentation and styles across the application.
This commit is contained in:
84
server/api/notifications/queue/retry/[id].post.js
Normal file
84
server/api/notifications/queue/retry/[id].post.js
Normal file
@@ -0,0 +1,84 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get job ID from params
|
||||
const id = event.context.params.id;
|
||||
|
||||
if (!id) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Job ID is required",
|
||||
});
|
||||
}
|
||||
|
||||
// Check if job exists and is in a failed state
|
||||
const job = await prisma.notification_queue.findUnique({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!job) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: "Job not found",
|
||||
});
|
||||
}
|
||||
|
||||
if (job.status !== "failed") {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: "Only failed jobs can be retried",
|
||||
});
|
||||
}
|
||||
|
||||
// Reset job for retry
|
||||
await prisma.notification_queue.update({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
data: {
|
||||
status: "queued",
|
||||
attempts: job.attempts, // Keep the previous attempts count for tracking
|
||||
last_attempt_at: null,
|
||||
error_message: null,
|
||||
updated_at: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// Log the retry action
|
||||
await prisma.notification_logs.create({
|
||||
data: {
|
||||
notification_id: job.notification_id,
|
||||
action: "Job Retry",
|
||||
status: "Queued",
|
||||
details: `Job ${id} requeued for retry after ${job.attempts} previous attempts`,
|
||||
created_at: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
message: "Job queued for retry",
|
||||
jobId: id,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error retrying job:", error);
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Failed to retry job",
|
||||
data: {
|
||||
error: error.message,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
73
server/api/notifications/queue/retry/all.post.js
Normal file
73
server/api/notifications/queue/retry/all.post.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Find all failed jobs
|
||||
const failedJobs = await prisma.notification_queue.findMany({
|
||||
where: {
|
||||
status: "failed",
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
notification_id: true,
|
||||
attempts: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (failedJobs.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
message: "No failed jobs to retry",
|
||||
count: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Update all failed jobs to queued status
|
||||
await prisma.notification_queue.updateMany({
|
||||
where: {
|
||||
status: "failed",
|
||||
},
|
||||
data: {
|
||||
status: "queued",
|
||||
last_attempt_at: null,
|
||||
error_message: null,
|
||||
updated_at: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// Log the batch retry action
|
||||
await prisma.notification_logs.create({
|
||||
data: {
|
||||
action: "Batch Job Retry",
|
||||
status: "Processed",
|
||||
details: `${failedJobs.length} failed jobs requeued for retry`,
|
||||
created_at: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
message: `${failedJobs.length} jobs queued for retry`,
|
||||
count: failedJobs.length,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error retrying all failed jobs:", error);
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Failed to retry all jobs",
|
||||
data: {
|
||||
error: error.message,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
118
server/api/notifications/queue/retry/jobs.get.js
Normal file
118
server/api/notifications/queue/retry/jobs.get.js
Normal file
@@ -0,0 +1,118 @@
|
||||
import { z } from "zod";
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
// Query parameter validation schema
|
||||
const jobsQuerySchema = z.object({
|
||||
page: z
|
||||
.string()
|
||||
.transform((val) => parseInt(val) || 1)
|
||||
.optional(),
|
||||
limit: z
|
||||
.string()
|
||||
.transform((val) => parseInt(val) || 10)
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Parse and validate query parameters
|
||||
const queryParams = getQuery(event) || {};
|
||||
const params = jobsQuerySchema.parse(queryParams);
|
||||
|
||||
// Build where clause for filtering failed jobs
|
||||
const where = {
|
||||
status: "failed",
|
||||
};
|
||||
|
||||
// Get total count for pagination
|
||||
const total = await prisma.notification_queue.count({ where });
|
||||
|
||||
// Calculate pagination metadata
|
||||
const totalPages = Math.ceil(total / params.limit);
|
||||
|
||||
// Fetch failed jobs with relations
|
||||
const jobs = await prisma.notification_queue.findMany({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
scheduled_for: true,
|
||||
attempts: true,
|
||||
max_attempts: true,
|
||||
last_attempt_at: true,
|
||||
error_message: true,
|
||||
created_at: true,
|
||||
notifications: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
type: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
last_attempt_at: "desc",
|
||||
},
|
||||
skip: (params.page - 1) * params.limit,
|
||||
take: params.limit,
|
||||
});
|
||||
|
||||
// Format jobs for response
|
||||
const formattedJobs = jobs.map((job) => {
|
||||
return {
|
||||
id: job.id,
|
||||
type: job.notifications?.type || "unknown",
|
||||
description: job.notifications?.title || "Failed Notification",
|
||||
status: job.status,
|
||||
attempts: job.attempts,
|
||||
maxAttempts: job.max_attempts,
|
||||
errorType: job.error_message ? getErrorType(job.error_message) : "Unknown Error",
|
||||
errorMessage: job.error_message || "No error message provided",
|
||||
failedAt: job.last_attempt_at ? new Date(job.last_attempt_at).toISOString() : null,
|
||||
nextRetry: "Manual retry required",
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
jobs: formattedJobs,
|
||||
pagination: {
|
||||
page: params.page,
|
||||
totalPages,
|
||||
totalItems: total,
|
||||
hasMore: params.page < totalPages,
|
||||
},
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching failed jobs:", error);
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Failed to fetch failed jobs",
|
||||
data: {
|
||||
error: error.message,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
|
||||
// Helper function to extract error type from error message
|
||||
function getErrorType(errorMessage) {
|
||||
if (!errorMessage) return "Unknown Error";
|
||||
|
||||
if (errorMessage.includes("timeout")) return "Timeout Error";
|
||||
if (errorMessage.includes("connect")) return "Connection Error";
|
||||
if (errorMessage.includes("authentication")) return "Authentication Error";
|
||||
if (errorMessage.includes("rate limit")) return "Rate Limit Error";
|
||||
if (errorMessage.includes("validation")) return "Validation Error";
|
||||
if (errorMessage.includes("template")) return "Template Error";
|
||||
|
||||
return "Processing Error";
|
||||
}
|
||||
73
server/api/notifications/queue/retry/stats.get.js
Normal file
73
server/api/notifications/queue/retry/stats.get.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import prisma from "~/server/utils/prisma";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Get current date and set to start of day
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
// Get yesterday
|
||||
const yesterday = new Date(today);
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
|
||||
// Query counts for different job statuses
|
||||
const [failed, retrying, recovered, deadLetter] = await Promise.all([
|
||||
// Failed jobs count
|
||||
prisma.notification_queue.count({
|
||||
where: {
|
||||
status: "failed",
|
||||
},
|
||||
}),
|
||||
// Jobs currently being retried (if your system tracks this state)
|
||||
prisma.notification_queue.count({
|
||||
where: {
|
||||
status: "processing",
|
||||
attempts: {
|
||||
gt: 1,
|
||||
},
|
||||
},
|
||||
}),
|
||||
// Recovered jobs (failed but then succeeded)
|
||||
prisma.notification_queue.count({
|
||||
where: {
|
||||
status: "completed",
|
||||
attempts: {
|
||||
gt: 1,
|
||||
},
|
||||
updated_at: {
|
||||
gte: yesterday,
|
||||
},
|
||||
},
|
||||
}),
|
||||
// Dead letter queue (failed and max attempts reached)
|
||||
prisma.notification_queue.count({
|
||||
where: {
|
||||
status: "failed",
|
||||
attempts: {
|
||||
equals: prisma.notification_queue.fields.max_attempts,
|
||||
},
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
failed,
|
||||
retrying,
|
||||
recovered,
|
||||
deadLetter,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error fetching retry stats:", error);
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Failed to fetch retry statistics",
|
||||
data: {
|
||||
error: error.message,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user