Skip to content

Commit 0372304

Browse files
committed
fix(billing): one reminder at 1 day, guarded by an advisory lock
Cloud runs two frontend replicas and node-schedule fires on each, so both would read the same unset marker and every trial user would get the email twice. A Postgres advisory lock lets one replica run and the other skip; it lives in the connection rather than the schema, so no migration. Drops the 3-day reminder, leaving the single notice before billing starts.
1 parent 14e0597 commit 0372304

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

‎apps/dokploy/server/utils/trial-notifications.ts‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
import { IS_CLOUD } from "@dokploy/server";
2-
import { db } from "@dokploy/server/db";
2+
import { db, dbUrl } from "@dokploy/server/db";
33
import TrialExpiringEmail from "@dokploy/server/emails/emails/trial-expiring";
44
import { sendEmail } from "@dokploy/server/verification/send-verification-email";
55
import { render } from "@react-email/components";
66
import { format } from "date-fns";
77
import { eq } from "drizzle-orm";
88
import { scheduleJob } from "node-schedule";
9+
import postgres from "postgres";
910
import type Stripe from "stripe";
1011
import { user } from "@/server/db/schema";
1112
import { getStripeClient, planFromPriceIds } from "@/server/utils/billing";
1213
import { WEBSITE_URL } from "@/server/utils/stripe";
1314

1415
const REMINDER_THRESHOLDS = [
15-
{ days: 3, metadataKey: "trialReminder3Sent" },
1616
{ days: 1, metadataKey: "trialReminder1Sent" },
1717
] as const;
1818

19+
// Cloud runs several replicas and node-schedule fires on each one, so without
20+
// this every trial user would get one email per replica. Advisory locks live
21+
// in the connection rather than the schema, so no migration is needed.
22+
const ADVISORY_LOCK_KEY = 4820260918;
23+
24+
const withAdvisoryLock = async <T>(run: () => Promise<T>) => {
25+
const sql = postgres(dbUrl, { max: 1 });
26+
try {
27+
const [row] =
28+
await sql`select pg_try_advisory_lock(${ADVISORY_LOCK_KEY}) as locked`;
29+
if (!row?.locked) return null;
30+
try {
31+
return await run();
32+
} finally {
33+
await sql`select pg_advisory_unlock(${ADVISORY_LOCK_KEY})`;
34+
}
35+
} finally {
36+
await sql.end({ timeout: 5 });
37+
}
38+
};
39+
1940
const PLAN_LABELS: Record<string, string> = {
2041
hobby: "Hobby",
2142
startup: "Startup",
@@ -148,7 +169,14 @@ export const initTrialNotificationsCronJob = () => {
148169

149170
scheduleJob("trial-expiration-reminders", "0 14 * * *", async () => {
150171
try {
151-
await processTrialExpirations();
172+
const result = await withAdvisoryLock(processTrialExpirations);
173+
if (!result) {
174+
console.log("Trial reminders: another replica holds the lock");
175+
return;
176+
}
177+
console.log(
178+
`Trial reminders: scanned ${result.scanned}, sent ${result.sent}`,
179+
);
152180
} catch (error) {
153181
console.error(
154182
"Trial reminder job failed:",

0 commit comments

Comments
 (0)