refactor: clean up code formatting and improve error handling in job scheduling

- Simplified code formatting for better readability in various components.
- Updated job scheduling functions to handle errors gracefully, ensuring that failures in scheduling do not disrupt the overall process.
- Enhanced logging for better traceability of job scheduling issues.

These changes improve code maintainability and user experience by providing clearer error messages and more organized code structure.
This commit is contained in:
Mauricio Siu
2026-04-11 10:04:29 -06:00
parent f404b231a6
commit d6124aae81
7 changed files with 59 additions and 44 deletions
@@ -200,9 +200,7 @@ export const ShowBilling = () => {
</div>
<Switch
id="invoice-notifications"
checked={
admin?.user.sendInvoiceNotifications ?? false
}
checked={admin?.user.sendInvoiceNotifications ?? false}
onCheckedChange={async (checked) => {
await updateInvoiceNotifications({
enabled: checked,
@@ -73,9 +73,13 @@ export const WelcomeSubscription = () => {
setIsOpen(open);
if (!open) {
const { success, ...rest } = router.query;
router.replace({ pathname: router.pathname, query: rest }, undefined, {
shallow: true,
});
router.replace(
{ pathname: router.pathname, query: rest },
undefined,
{
shallow: true,
},
);
}
}}
>
+4 -1
View File
@@ -205,7 +205,10 @@ export const stripeRouter = createTRPCRouter({
mode: "subscription",
line_items: items,
...(stripeCustomerId
? { customer: stripeCustomerId, customer_update: { name: "auto", address: "auto" } }
? {
customer: stripeCustomerId,
customer_update: { name: "auto", address: "auto" },
}
: { customer_email: owner.email }),
metadata: {
adminId: owner.id,
@@ -32,10 +32,7 @@ export const sendInvoiceEmail = async (
if (!invoice.hosted_invoice_url) return;
try {
const amountFormatted = formatAmount(
invoice.amount_paid,
invoice.currency,
);
const amountFormatted = formatAmount(invoice.amount_paid, invoice.currency);
const htmlContent = await renderAsync(
InvoiceNotificationEmail({
@@ -85,10 +82,7 @@ export const sendPaymentFailedEmail = async (
if (!invoice.hosted_invoice_url) return;
try {
const amountFormatted = formatAmount(
invoice.amount_due,
invoice.currency,
);
const amountFormatted = formatAmount(invoice.amount_due, invoice.currency);
const htmlContent = await renderAsync(
PaymentFailedEmail({
+4 -4
View File
@@ -33,7 +33,7 @@ app.use(async (c, next) => {
app.post("/create-backup", zValidator("json", jobQueueSchema), async (c) => {
const data = c.req.valid("json");
scheduleJob(data);
await scheduleJob(data);
logger.info({ data }, `[${data.type}] created successfully`);
return c.json({ message: `[${data.type}] created successfully` });
});
@@ -70,7 +70,7 @@ app.post("/update-backup", zValidator("json", jobQueueSchema), async (c) => {
}
logger.info({ result }, "Job removed");
}
scheduleJob(data);
await scheduleJob(data);
logger.info("Backup updated successfully");
return c.json({ message: "Backup updated successfully" });
@@ -103,8 +103,8 @@ process.on("uncaughtException", (err) => {
logger.error(err, "Uncaught exception");
});
process.on("unhandledRejection", (reason, promise) => {
logger.error({ promise, reason }, "Unhandled Rejection at: Promise");
process.on("unhandledRejection", (reason, _promise) => {
logger.error(reason instanceof Error ? reason : { reason: String(reason) }, "Unhandled Rejection at: Promise");
});
const port = Number.parseInt(process.env.PORT || "3000");
+5 -5
View File
@@ -21,28 +21,28 @@ export const cleanQueue = async () => {
}
};
export const scheduleJob = (job: QueueJob) => {
export const scheduleJob = async (job: QueueJob) => {
if (job.type === "backup") {
jobQueue.add(job.backupId, job, {
await jobQueue.add(job.backupId, job, {
repeat: {
pattern: job.cronSchedule,
},
});
} else if (job.type === "server") {
jobQueue.add(`${job.serverId}-cleanup`, job, {
await jobQueue.add(`${job.serverId}-cleanup`, job, {
repeat: {
pattern: job.cronSchedule,
},
});
} else if (job.type === "schedule") {
jobQueue.add(job.scheduleId, job, {
await jobQueue.add(job.scheduleId, job, {
repeat: {
pattern: job.cronSchedule,
tz: job.timezone || "UTC",
},
});
} else if (job.type === "volume-backup") {
jobQueue.add(job.volumeBackupId, job, {
await jobQueue.add(job.volumeBackupId, job, {
repeat: {
pattern: job.cronSchedule,
},
+36 -20
View File
@@ -135,11 +135,15 @@ export const initializeJobs = async () => {
for (const server of servers) {
const { serverId } = server;
scheduleJob({
serverId,
type: "server",
cronSchedule: CLEANUP_CRON_JOB,
});
try {
await scheduleJob({
serverId,
type: "server",
cronSchedule: CLEANUP_CRON_JOB,
});
} catch (error) {
logger.error(error, `Failed to schedule cleanup job for server ${serverId}`);
}
}
logger.info({ Quantity: servers.length }, "Active Servers Initialized");
@@ -157,11 +161,15 @@ export const initializeJobs = async () => {
});
for (const backup of backupsResult) {
scheduleJob({
backupId: backup.backupId,
type: "backup",
cronSchedule: backup.schedule,
});
try {
await scheduleJob({
backupId: backup.backupId,
type: "backup",
cronSchedule: backup.schedule,
});
} catch (error) {
logger.error(error, `Failed to schedule backup ${backup.backupId}`);
}
}
logger.info({ Quantity: backupsResult.length }, "Backups Initialized");
@@ -197,11 +205,15 @@ export const initializeJobs = async () => {
);
for (const schedule of filteredSchedulesBasedOnServerStatus) {
scheduleJob({
scheduleId: schedule.scheduleId,
type: "schedule",
cronSchedule: schedule.cronExpression,
});
try {
await scheduleJob({
scheduleId: schedule.scheduleId,
type: "schedule",
cronSchedule: schedule.cronExpression,
});
} catch (error) {
logger.error(error, `Failed to schedule ${schedule.scheduleId}`);
}
}
logger.info(
{ Quantity: filteredSchedulesBasedOnServerStatus.length },
@@ -236,11 +248,15 @@ export const initializeJobs = async () => {
);
for (const volumeBackup of filteredVolumeBackupsBasedOnServerStatus) {
scheduleJob({
volumeBackupId: volumeBackup.volumeBackupId,
type: "volume-backup",
cronSchedule: volumeBackup.cronExpression,
});
try {
await scheduleJob({
volumeBackupId: volumeBackup.volumeBackupId,
type: "volume-backup",
cronSchedule: volumeBackup.cronExpression,
});
} catch (error) {
logger.error(error, `Failed to schedule volume backup ${volumeBackup.volumeBackupId}`);
}
}
logger.info(