Adds additional campaign generation logic (#646)

This commit is contained in:
Chris Anderson 2025-03-08 20:36:12 -06:00 committed by GitHub
parent a7c855e8db
commit d368613818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import PushJob from '../providers/push/PushJob'
import WebhookJob from '../providers/webhook/WebhookJob'
import TextJob from '../providers/text/TextJob'
import EmailJob from '../providers/email/EmailJob'
import { logger } from '../config/logger'
import { User } from '../users/User'
import { UserEvent } from '../users/UserEvent'
import Campaign, { CampaignCreateParams, CampaignDelivery, CampaignParams, CampaignPopulationProgress, CampaignProgress, CampaignSend, CampaignSendParams, CampaignSendReferenceType, CampaignSendState, SentCampaign } from './Campaign'
@ -288,6 +289,7 @@ export const generateSendList = async (campaign: SentCampaign) => {
// Clear any aborted sends
await clearCampaign(campaign)
const now = Date.now()
const cacheKey = CacheKeys.populationProgress(campaign)
const stream = UserList.query()
@ -295,11 +297,14 @@ export const generateSendList = async (campaign: SentCampaign) => {
.whereIn('list_id', campaign.list_ids ?? [])
.stream()
logger.debug({ campaign: campaign.id, elapsed: Date.now() - now }, 'campaign:generate:progress:started')
let count = 0
const limit = 10_000
for await (const user of stream) {
if (count % limit === 0) {
logger.debug({ count, campaign: campaign.id, elapsed: Date.now() - now }, 'campaign:generate:progress')
await recipientPartialQuery({
campaign,
project,
@ -317,6 +322,8 @@ export const generateSendList = async (campaign: SentCampaign) => {
count++
}
logger.debug({ count, campaign: campaign.id, elapsed: Date.now() - now }, 'campaign:generate:progress:finished')
await Campaign.update(qb => qb.where('id', campaign.id), { state: 'scheduled' })
}

View file

@ -11,7 +11,7 @@ export default class CampaignStateJob extends Job {
// Fetch anything that is currently running, has finished
// within the last two days or has activity since last run
const openedCampaignIds = await App.main.redis.smembers(CacheKeys.pendingStats).then(ids => ids.map(parseInt))
const openedCampaignIds = await App.main.redis.smembers(CacheKeys.pendingStats).then(ids => ids.map(parseInt).filter(x => x))
const campaigns = await Campaign.query()
.whereIn('state', ['scheduled', 'running'])
.orWhere(function(qb) {