Skip to content

Commit 5a0ff8e

Browse files
committed
Batch bug fix
1 parent 1b5ec03 commit 5a0ff8e

6 files changed

Lines changed: 17 additions & 11 deletions

File tree

RELEASE_NOTES.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
### ✨ Features
22

3-
- Uses cron job to process queued campaigns, processes messages in batches, much more efficient for large campaigns.
4-
- Theme toggle updated.
5-
- Loading state for API keys.
63
- New features and improvements.
74

85
### 🐛 Bug Fixes
96

10-
- Recipient count on campaign page was incorrect.
7+
- Fixed the issue of campaigns not sending because of incorrect status.
8+
- Fixed the batch size for processing campaigns.
119
- Various bug fixes and optimizations.
1210

1311
### 📚 Docs
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterEnum
2+
ALTER TYPE "CampaignStatus" ADD VALUE 'CREATING';

apps/backend/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ model SubscriberMetadata {
9696
enum CampaignStatus {
9797
DRAFT
9898
SCHEDULED
99+
CREATING
99100
SENDING
100101
COMPLETED
101102
CANCELLED

apps/backend/src/campaign/mutation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export const startCampaign = authProcedure
375375
const status =
376376
campaign.scheduledAt && campaign.scheduledAt > new Date()
377377
? "SCHEDULED"
378-
: "SENDING"
378+
: "CREATING"
379379

380380
const updatedCampaign = await prisma.campaign.update({
381381
where: { id: campaign.id },

apps/backend/src/cron/processQueuedCampaigns.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async function getSubscribersForCampaign(
1818
where: {
1919
campaignId,
2020
},
21+
take: BATCH_SIZE,
2122
include: {
2223
List: {
2324
include: {
@@ -86,7 +87,7 @@ export const processQueuedCampaigns = cronJob(
8687
async () => {
8788
const queuedCampaigns = await prisma.campaign.findMany({
8889
where: {
89-
status: "SENDING",
90+
status: "CREATING",
9091
},
9192
include: {
9293
Organization: {
@@ -154,17 +155,15 @@ export const processQueuedCampaigns = cronJob(
154155
allSubscribersMap.values()
155156
).filter((sub) => !subscribersWithMessage.has(sub.id))
156157

157-
const batchSubscribers = subscribersToProcess.slice(0, BATCH_SIZE)
158-
159-
if (batchSubscribers.length === 0) {
158+
if (subscribersToProcess.length === 0) {
160159
continue
161160
}
162161

163162
await prisma.$transaction(async (tx) => {
164163
const linkTracker = new LinkTracker(tx)
165164
const messagesToCreate: Prisma.MessageCreateManyInput[] = []
166165

167-
for (const subscriber of batchSubscribers) {
166+
for (const subscriber of subscribersToProcess) {
168167
const messageId = uuidV4()
169168
if (!campaign.content) {
170169
oneTimeLogger(
@@ -245,6 +244,12 @@ export const processQueuedCampaigns = cronJob(
245244
await tx.message.createMany({
246245
data: messagesToCreate,
247246
})
247+
248+
await tx.campaign.update({
249+
where: { id: campaign.id },
250+
data: { status: "SENDING" },
251+
})
252+
248253
console.log(
249254
`Cron job: Created ${messagesToCreate.length} messages for campaign ${campaign.id}.`
250255
)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.5.0",
2+
"version": "0.5.1",
33
"name": "letterspace",
44
"private": true,
55
"scripts": {

0 commit comments

Comments
 (0)