Skip to content

Commit d6124aa

Browse files
committed
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.
1 parent f404b23 commit d6124aa

7 files changed

Lines changed: 59 additions & 44 deletions

File tree

apps/dokploy/components/dashboard/settings/billing/show-billing.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ export const ShowBilling = () => {
200200
</div>
201201
<Switch
202202
id="invoice-notifications"
203-
checked={
204-
admin?.user.sendInvoiceNotifications ?? false
205-
}
203+
checked={admin?.user.sendInvoiceNotifications ?? false}
206204
onCheckedChange={async (checked) => {
207205
await updateInvoiceNotifications({
208206
enabled: checked,

apps/dokploy/components/dashboard/settings/servers/welcome-stripe/welcome-subscription.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ export const WelcomeSubscription = () => {
7373
setIsOpen(open);
7474
if (!open) {
7575
const { success, ...rest } = router.query;
76-
router.replace({ pathname: router.pathname, query: rest }, undefined, {
77-
shallow: true,
78-
});
76+
router.replace(
77+
{ pathname: router.pathname, query: rest },
78+
undefined,
79+
{
80+
shallow: true,
81+
},
82+
);
7983
}
8084
}}
8185
>

apps/dokploy/server/api/routers/stripe.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ export const stripeRouter = createTRPCRouter({
205205
mode: "subscription",
206206
line_items: items,
207207
...(stripeCustomerId
208-
? { customer: stripeCustomerId, customer_update: { name: "auto", address: "auto" } }
208+
? {
209+
customer: stripeCustomerId,
210+
customer_update: { name: "auto", address: "auto" },
211+
}
209212
: { customer_email: owner.email }),
210213
metadata: {
211214
adminId: owner.id,

apps/dokploy/server/utils/stripe-notifications.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ export const sendInvoiceEmail = async (
3232
if (!invoice.hosted_invoice_url) return;
3333

3434
try {
35-
const amountFormatted = formatAmount(
36-
invoice.amount_paid,
37-
invoice.currency,
38-
);
35+
const amountFormatted = formatAmount(invoice.amount_paid, invoice.currency);
3936

4037
const htmlContent = await renderAsync(
4138
InvoiceNotificationEmail({
@@ -85,10 +82,7 @@ export const sendPaymentFailedEmail = async (
8582
if (!invoice.hosted_invoice_url) return;
8683

8784
try {
88-
const amountFormatted = formatAmount(
89-
invoice.amount_due,
90-
invoice.currency,
91-
);
85+
const amountFormatted = formatAmount(invoice.amount_due, invoice.currency);
9286

9387
const htmlContent = await renderAsync(
9488
PaymentFailedEmail({

apps/schedules/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ app.use(async (c, next) => {
3333

3434
app.post("/create-backup", zValidator("json", jobQueueSchema), async (c) => {
3535
const data = c.req.valid("json");
36-
scheduleJob(data);
36+
await scheduleJob(data);
3737
logger.info({ data }, `[${data.type}] created successfully`);
3838
return c.json({ message: `[${data.type}] created successfully` });
3939
});
@@ -70,7 +70,7 @@ app.post("/update-backup", zValidator("json", jobQueueSchema), async (c) => {
7070
}
7171
logger.info({ result }, "Job removed");
7272
}
73-
scheduleJob(data);
73+
await scheduleJob(data);
7474
logger.info("Backup updated successfully");
7575

7676
return c.json({ message: "Backup updated successfully" });
@@ -103,8 +103,8 @@ process.on("uncaughtException", (err) => {
103103
logger.error(err, "Uncaught exception");
104104
});
105105

106-
process.on("unhandledRejection", (reason, promise) => {
107-
logger.error({ promise, reason }, "Unhandled Rejection at: Promise");
106+
process.on("unhandledRejection", (reason, _promise) => {
107+
logger.error(reason instanceof Error ? reason : { reason: String(reason) }, "Unhandled Rejection at: Promise");
108108
});
109109

110110
const port = Number.parseInt(process.env.PORT || "3000");

apps/schedules/src/queue.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ export const cleanQueue = async () => {
2121
}
2222
};
2323

24-
export const scheduleJob = (job: QueueJob) => {
24+
export const scheduleJob = async (job: QueueJob) => {
2525
if (job.type === "backup") {
26-
jobQueue.add(job.backupId, job, {
26+
await jobQueue.add(job.backupId, job, {
2727
repeat: {
2828
pattern: job.cronSchedule,
2929
},
3030
});
3131
} else if (job.type === "server") {
32-
jobQueue.add(`${job.serverId}-cleanup`, job, {
32+
await jobQueue.add(`${job.serverId}-cleanup`, job, {
3333
repeat: {
3434
pattern: job.cronSchedule,
3535
},
3636
});
3737
} else if (job.type === "schedule") {
38-
jobQueue.add(job.scheduleId, job, {
38+
await jobQueue.add(job.scheduleId, job, {
3939
repeat: {
4040
pattern: job.cronSchedule,
4141
tz: job.timezone || "UTC",
4242
},
4343
});
4444
} else if (job.type === "volume-backup") {
45-
jobQueue.add(job.volumeBackupId, job, {
45+
await jobQueue.add(job.volumeBackupId, job, {
4646
repeat: {
4747
pattern: job.cronSchedule,
4848
},

apps/schedules/src/utils.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ export const initializeJobs = async () => {
135135

136136
for (const server of servers) {
137137
const { serverId } = server;
138-
scheduleJob({
139-
serverId,
140-
type: "server",
141-
cronSchedule: CLEANUP_CRON_JOB,
142-
});
138+
try {
139+
await scheduleJob({
140+
serverId,
141+
type: "server",
142+
cronSchedule: CLEANUP_CRON_JOB,
143+
});
144+
} catch (error) {
145+
logger.error(error, `Failed to schedule cleanup job for server ${serverId}`);
146+
}
143147
}
144148

145149
logger.info({ Quantity: servers.length }, "Active Servers Initialized");
@@ -157,11 +161,15 @@ export const initializeJobs = async () => {
157161
});
158162

159163
for (const backup of backupsResult) {
160-
scheduleJob({
161-
backupId: backup.backupId,
162-
type: "backup",
163-
cronSchedule: backup.schedule,
164-
});
164+
try {
165+
await scheduleJob({
166+
backupId: backup.backupId,
167+
type: "backup",
168+
cronSchedule: backup.schedule,
169+
});
170+
} catch (error) {
171+
logger.error(error, `Failed to schedule backup ${backup.backupId}`);
172+
}
165173
}
166174
logger.info({ Quantity: backupsResult.length }, "Backups Initialized");
167175

@@ -197,11 +205,15 @@ export const initializeJobs = async () => {
197205
);
198206

199207
for (const schedule of filteredSchedulesBasedOnServerStatus) {
200-
scheduleJob({
201-
scheduleId: schedule.scheduleId,
202-
type: "schedule",
203-
cronSchedule: schedule.cronExpression,
204-
});
208+
try {
209+
await scheduleJob({
210+
scheduleId: schedule.scheduleId,
211+
type: "schedule",
212+
cronSchedule: schedule.cronExpression,
213+
});
214+
} catch (error) {
215+
logger.error(error, `Failed to schedule ${schedule.scheduleId}`);
216+
}
205217
}
206218
logger.info(
207219
{ Quantity: filteredSchedulesBasedOnServerStatus.length },
@@ -236,11 +248,15 @@ export const initializeJobs = async () => {
236248
);
237249

238250
for (const volumeBackup of filteredVolumeBackupsBasedOnServerStatus) {
239-
scheduleJob({
240-
volumeBackupId: volumeBackup.volumeBackupId,
241-
type: "volume-backup",
242-
cronSchedule: volumeBackup.cronExpression,
243-
});
251+
try {
252+
await scheduleJob({
253+
volumeBackupId: volumeBackup.volumeBackupId,
254+
type: "volume-backup",
255+
cronSchedule: volumeBackup.cronExpression,
256+
});
257+
} catch (error) {
258+
logger.error(error, `Failed to schedule volume backup ${volumeBackup.volumeBackupId}`);
259+
}
244260
}
245261

246262
logger.info(

0 commit comments

Comments
 (0)