Skip to content

Commit 595810d

Browse files
committed
poll.ts cleanups
1 parent 977e941 commit 595810d

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Empty for now.
2+
// TODO Address this

ghost/core/core/server/services/automations/poll.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ const MAX_STEPS_PER_BATCH = 100;
55
const MAX_ATTEMPTS = 10;
66
const RETRY_DELAY_MS = 10 * 60 * 1000;
77

8-
const logging = require('@tryghost/logging');
9-
const {Member} = require('../../models');
10-
const {MEMBER_WELCOME_EMAIL_ELIGIBLE_STATUSES, MEMBER_WELCOME_EMAIL_SLUGS} = require('../member-welcome-emails/constants');
8+
import logging from '@tryghost/logging';
9+
import errors from '@tryghost/errors';
10+
import {Member} from '../../models';
11+
import {MEMBER_WELCOME_EMAIL_ELIGIBLE_STATUSES, MEMBER_WELCOME_EMAIL_SLUGS} from '../member-welcome-emails/constants';
1112

1213
type MemberWelcomeEmailService = {
1314
init: () => unknown;
@@ -93,13 +94,15 @@ const processStep = async ({
9394
}
9495

9596
if (!step.member_id) {
97+
// TODO: This should be an error like "member was deleted"
9698
await automationsApi.markStepTerminal(step, 'member unsubscribed');
9799
return;
98100
}
99101

100102
const member = await Member.findOne({id: step.member_id}) as MemberModel | null;
101103

102104
if (!member) {
105+
// TODO: This should be an internal server error, possibly
103106
await automationsApi.markStepTerminal(step, 'member unsubscribed');
104107
return;
105108
}
@@ -110,15 +113,12 @@ const processStep = async ({
110113
return;
111114
}
112115

116+
let nextReadyAt: Date | null = null;
117+
113118
switch (step.type) {
114119
case 'wait': {
115-
const nextReadyAt = await automationsApi.finishStepAndEnqueueNext(step);
116-
117-
if (nextReadyAt) {
118-
enqueueAnotherPollAt(nextReadyAt);
119-
}
120-
121-
return;
120+
nextReadyAt = await automationsApi.finishStepAndEnqueueNext(step);
121+
break;
122122
}
123123
case 'send_email': {
124124
memberWelcomeEmailService.init();
@@ -134,18 +134,15 @@ const processStep = async ({
134134
subject: step.email_subject
135135
},
136136
member: {
137+
// TODO: This is weird
137138
email: member.get('email') ?? step.member_email,
138139
name: member.get('name'),
139140
uuid: member.get('uuid') ?? ''
140141
},
141142
memberStatus
142143
});
143144

144-
const nextReadyAt = await automationsApi.finishStepAndEnqueueNext(step);
145-
146-
if (nextReadyAt) {
147-
enqueueAnotherPollAt(nextReadyAt);
148-
}
145+
nextReadyAt = await automationsApi.finishStepAndEnqueueNext(step);
149146
} catch (err) {
150147
logging.error({
151148
err,
@@ -157,18 +154,27 @@ const processStep = async ({
157154

158155
if (step.step_attempts < MAX_ATTEMPTS) {
159156
const retryAt = new Date(Date.now() + RETRY_DELAY_MS);
157+
// TODO: I don't understand this part
160158
const didRetry = await automationsApi.retryStep(step, retryAt);
161-
162159
if (didRetry) {
163160
enqueueAnotherPollAt(retryAt);
164161
}
165162
} else {
166163
await markMaxAttemptsExceeded(automationsApi, step);
167164
}
168165
}
169-
170-
return;
166+
break;
171167
}
168+
default: {
169+
const _exhaustive: never = step;
170+
throw new errors.InternalServerError({
171+
message: `Unexpected automation step type ${_exhaustive}`
172+
});
173+
}
174+
}
175+
176+
if (nextReadyAt) {
177+
enqueueAnotherPollAt(nextReadyAt);
172178
}
173179
};
174180

0 commit comments

Comments
 (0)