From 89eabea0de07f42404e0e35a48ca8300e702b6b5 Mon Sep 17 00:00:00 2001 From: Tomas Bures Date: Tue, 11 Sep 2018 10:07:00 +0200 Subject: [PATCH] Fixes in selection of subscribers --- client/src/campaigns/Status.js | 2 +- models/campaigns.js | 38 +++++++++++++++++++--------------- services/sender-master.js | 4 ++-- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/client/src/campaigns/Status.js b/client/src/campaigns/Status.js index 379c6399..d299d644 100644 --- a/client/src/campaigns/Status.js +++ b/client/src/campaigns/Status.js @@ -318,7 +318,7 @@ export default class Status extends Component { // The periodic task runs all the time, so that we don't have to worry about starting/stopping it as a reaction to the buttons. await this.refreshEntity(); if (this.refreshTimeoutHandler) { // For some reason the task gets rescheduled if server is restarted while the page is shown. That why we have this check here. - this.refreshTimeoutId = setTimeout(this.refreshTimeoutHandler, 2000); + this.refreshTimeoutId = setTimeout(this.refreshTimeoutHandler, 10000); } } diff --git a/models/campaigns.js b/models/campaigns.js index ca588e7d..dfc9c764 100644 --- a/models/campaigns.js +++ b/models/campaigns.js @@ -101,11 +101,9 @@ async function listTestUsersDTAjax(context, campaignId, params) { This is supposed to produce queries like this: select * from ( - (select `subscription__1`.`email`, 2 AS campaign_list_id, 1 AS list, NULL AS segment from `subscription__1` left join `campaign_messages` on - `campaign_messages`.`subscription` = `subscription__1`.`id` where `subscription__1`.`status` = 1 and `subscription__1`.`is_test` = true) + (select `subscription__1`.`email`, 2 AS campaign_list_id, 1 AS list, NULL AS segment from `subscription__1` where `subscription__1`.`status` = 1 and `subscription__1`.`is_test` = true) UNION ALL - (select `subscription__2`.`email`, 4 AS campaign_list_id, 2 AS list, NULL AS segment from `subscription__2` left join `campaign_messages` on - `campaign_messages`.`subscription` = `subscription__2`.`id` where `subscription__2`.`status` = 1 and `subscription__2`.`is_test` = true) + (select `subscription__2`.`email`, 4 AS campaign_list_id, 2 AS list, NULL AS segment from `subscription__2` where `subscription__2`.`status` = 1 and `subscription__2`.`is_test` = true) ) as `test_subscriptions` inner join `lists` on `test_subscriptions`.`list` = `lists`.`id` inner join `segments` on `test_subscriptions`.`segment` = `segments`.`id` inner join `namespaces` on `lists`.`namespace` = `namespaces`.`id` @@ -573,16 +571,17 @@ async function getSubscribersQueryGeneratorTx(tx, campaignId, onlyUnsent, batchS /* This is supposed to produce queries like this: - select count(*) as `subscriptionsToSend` from `campaign_lists` inner join ( + select ... from `campaign_lists` inner join ( select `email`, min(`campaign_list_id`) as `campaign_list_id`, max(`sent`) as `sent` from ( - (select `subscription__1`.`email`, 2 AS campaign_list_id, campaign_messages.id IS NOT NULL AS sent from `subscription__1` left join `campaign_messages` on - `campaign_messages`.`subscription` = `subscription__1`.`id` where `campaign_messages`.`campaign` = 1 and `campaign_messages`.`list` = 1 and `subscription__1`.`status` = 1) - UNION ALL - (select `subscription__2`.`email`, 4 AS campaign_list_id, campaign_messages.id IS NOT NULL AS sent from `subscription__2` left join `campaign_messages` on - `campaign_messages`.`subscription` = `subscription__2`.`id` where `campaign_messages`.`campaign` = 1 and `campaign_messages`.`list` = 2 and `subscription__2`.`status` = 1) - ) - as `pending_subscriptions_all` where `sent` = false group by `email` - ) as `pending_subscriptions` on `campaign_lists`.`id` = `pending_subscriptions`.`campaign_list_id` where `campaign_lists`.`campaign` = 1 limit 1 + (select `subscription__2`.`email`, 8 AS campaign_list_id, related_campaign_messages.id IS NOT NULL AS sent from `subscription__2` left join + (select * from `campaign_messages` where `campaign_messages`.`campaign` = 1 and `campaign_messages`.`list` = 2) + as `related_campaign_messages` on `related_campaign_messages`.`subscription` = `subscription__2`.`id` where `subscription__2`.`status` = 1) + UNION ALL + (select `subscription__1`.`email`, 9 AS campaign_list_id, related_campaign_messages.id IS NOT NULL AS sent from `subscription__1` left join + (select * from `campaign_messages` where `campaign_messages`.`campaign` = 1 and `campaign_messages`.`list` = 1) + as `related_campaign_messages` on `related_campaign_messages`.`subscription` = `subscription__1`.`id` where `subscription__1`.`status` = 1) + ) as `pending_subscriptions_all` where `sent` = false group by `email`) + as `pending_subscriptions` on `campaign_lists`.`id` = `pending_subscriptions`.`campaign_list_id` where `campaign_lists`.`campaign` = '1' This was too much for Knex, so we partially construct these queries directly as strings; */ @@ -595,14 +594,19 @@ async function getSubscribersQueryGeneratorTx(tx, campaignId, onlyUnsent, batchS const subsTable = subscriptions.getSubscriptionTableName(cpgList.list); const sqlQry = knex.from(subsTable) - .leftJoin('campaign_messages', 'campaign_messages.subscription', subsTable + '.id') - .where('campaign_messages.campaign', cpgList.campaign) - .where('campaign_messages.list', cpgList.list) + .leftJoin( + function () { + return this.from('campaign_messages') + .where('campaign_messages.campaign', cpgList.campaign) + .where('campaign_messages.list', cpgList.list) + .as('related_campaign_messages'); + }, + 'related_campaign_messages.subscription', subsTable + '.id') .where(subsTable + '.status', SubscriptionStatus.SUBSCRIBED) .where(function() { addSegmentQuery(this); }) - .select([subsTable + '.email', knex.raw('? AS campaign_list_id', [cpgList.id]), knex.raw('campaign_messages.id IS NOT NULL AS sent')]) + .select([subsTable + '.email', knex.raw('? AS campaign_list_id', [cpgList.id]), knex.raw('related_campaign_messages.id IS NOT NULL AS sent')]) .toSQL().toNative(); subsQrys.push(sqlQry); diff --git a/services/sender-master.js b/services/sender-master.js index 5609a15f..cb7ad19d 100644 --- a/services/sender-master.js +++ b/services/sender-master.js @@ -27,7 +27,7 @@ const workerBatchSize = 100; const messageQueue = new Map(); // campaignId -> [{listId, email}] const messageQueueCont = new Map(); // campaignId -> next batch callback -const workerSchedulerCont = null; +let workerSchedulerCont = null; function messagesProcessed(workerId) { @@ -202,7 +202,7 @@ async function spawnWorker(workerId) { return resolve(); } else if (msg.type === 'messages-processed') { - messageProcessed(workerId); + messagesProcessed(workerId); } }