📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
fix: limit trial lifecycle email concurrency
Blue Mouse8fe9b78d1d81
diff --git a/backend/apps/cloud/src/common/templates/en/customer_sign-up.html b/backend/apps/cloud/src/common/templates/en/customer_sign-up.htmlindex 3a914c22a..33dea8456 100644--- a/backend/apps/cloud/src/common/templates/en/customer_sign-up.html+++ b/backend/apps/cloud/src/common/templates/en/customer_sign-up.html@@ -10,10 +10,7 @@ details. You won't be charged today. Here's what happens next:<br /><b>Before the trial starts</b>: Choose a plan and add payment details. Nocharge today.</li>- <li><b>Days 1-14</b>: Use all Swetrix Cloud features.</li>- <li><b>Day 3</b>: We'll point you to the first traffic patterns to check.</li>- <li><b>Day 7</b>: We'll show you useful features, including GA4 import.</li>- <li><b>Days 11 and 13</b>: We'll remind you before billing starts.</li>+ <li><b>Days 11-13</b>: We'll remind you before the trial ends.</li><li><b>Day 14</b>: Your subscription starts automatically unless you cancelfirst.diff --git a/backend/apps/cloud/src/mailer/mailer.service.ts b/backend/apps/cloud/src/mailer/mailer.service.tsindex 8f1114586..8a66f4c91 100644--- a/backend/apps/cloud/src/mailer/mailer.service.ts+++ b/backend/apps/cloud/src/mailer/mailer.service.ts@@ -253,7 +253,8 @@ export class MailerService {await this.nodeMailerService.sendMail(message)}} catch (reason) {- this.logger.error(reason, 'sendEmail', true)+ this.logger.error({ reason, email, templateName }, 'sendEmail')+ throw reason}}}diff --git a/backend/apps/cloud/src/task-manager/task-manager.service.ts b/backend/apps/cloud/src/task-manager/task-manager.service.tsindex d82b16421..a04fe8189 100644--- a/backend/apps/cloud/src/task-manager/task-manager.service.ts+++ b/backend/apps/cloud/src/task-manager/task-manager.service.ts@@ -127,6 +127,8 @@ const checkQueryCondition = (const CHUNK_SIZE = 5000const REPORTS_USERS_CONCURRENCY = 3const REPORTS_PROJECTS_CONCURRENCY = 5+const TRIAL_LIFECYCLE_STEPS_CONCURRENCY = 1+const TRIAL_LIFECYCLE_EMAILS_CONCURRENCY = 3const NO_EVENTS_REMINDER_DELAY_DAYS = 2const TRIAL_LIFECYCLE_EMAIL_URLS = {dashboardUrl: 'https://swetrix.com/dashboard',@@ -1575,19 +1577,19 @@ export class TaskManagerService {@Cron(CronExpression.EVERY_4_HOURS)async trialReminder() {const now = dayjs.utc()- const promises = _map(TRIAL_LIFECYCLE_EMAIL_STEPS, (step) =>- this.sendTrialLifecycleEmails(step, now),+ await mapLimit(+ TRIAL_LIFECYCLE_EMAIL_STEPS,+ TRIAL_LIFECYCLE_STEPS_CONCURRENCY,+ async (step) => {+ try {+ await this.sendTrialLifecycleEmails(step, now)+ } catch (reason) {+ this.logger.error(+ `[CRON WORKER](trialReminder) Error occured: ${reason}`,+ )+ }+ },)-- const results = await Promise.allSettled(promises)-- results.forEach((result) => {- if (result.status === 'rejected') {- this.logger.error(- `[CRON WORKER](trialReminder) Error occured: ${result.reason}`,- )- }- })}private async sendTrialLifecycleEmails(@@ -1615,42 +1617,38 @@ export class TaskManagerService {],})- const promises = _map(users, async (user) => {- const { id, email, cancellationEffectiveDate } = user- const sent = user.trialLifecycleEmailsSent || {}-- if (sent[step.key]) {- return- }-- await this.userService.update(id, {- trialLifecycleEmailsSent: {- ...sent,- [step.key]: now.toISOString(),- },- ...(step.key === TrialLifecycleEmailKey.EndsTomorrow- ? { trialReminderSent: true }- : {}),- })+ await mapLimit(users, TRIAL_LIFECYCLE_EMAILS_CONCURRENCY, async (user) => {+ try {+ const { id, email, cancellationEffectiveDate } = user+ const sent = user.trialLifecycleEmailsSent || {}- const template =- cancellationEffectiveDate && step.cancelledTemplate- ? step.cancelledTemplate- : step.template+ if (sent[step.key]) {+ return+ }- await this.mailerService.sendEmail(- email,- template,- TRIAL_LIFECYCLE_EMAIL_URLS,- )- })+ const template =+ cancellationEffectiveDate && step.cancelledTemplate+ ? step.cancelledTemplate+ : step.template- const results = await Promise.allSettled(promises)+ await this.mailerService.sendEmail(+ email,+ template,+ TRIAL_LIFECYCLE_EMAIL_URLS,+ )- results.forEach((result) => {- if (result.status === 'rejected') {+ await this.userService.update(id, {+ trialLifecycleEmailsSent: {+ ...sent,+ [step.key]: now.toISOString(),+ },+ ...(step.key === TrialLifecycleEmailKey.EndsTomorrow+ ? { trialReminderSent: true }+ : {}),+ })+ } catch (reason) {this.logger.error(- `[CRON WORKER](trialReminder:${step.key}) Error occured: ${result.reason}`,+ `[CRON WORKER](trialReminder:${step.key}) Error occured: ${reason}`,)}})