Skip to content

Commit 93e025c

Browse files
committed
refactor timeout stuff to use options, not env directly
1 parent 11141b4 commit 93e025c

4 files changed

Lines changed: 42 additions & 28 deletions

File tree

packages/ws-worker/src/util/convert-lightning-plan.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export type WorkerRunOptions = ExecuteOptions & {
4848
outputDataclips?: boolean;
4949
payloadLimitMb?: number;
5050
jobLogLevel?: LogLevel;
51+
timeoutRetryCount?: number;
52+
timeoutRetryDelay?: number;
5153
};
5254

5355
type ConversionOptions = {

packages/ws-worker/src/util/send-event.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ import * as Sentry from '@sentry/node';
22
import type { Context } from '../api/execute';
33
import { LightningSocketError, LightningTimeoutError } from '../errors';
44

5+
// // When a message receives a timeout, how many times should we retry?
6+
// const TIMEOUT_RETRY_COUNT = process.env.WORKER_TIMEOUT_RETRY_COUNT
7+
// ? parseInt(process.env.WORKER_TIMEOUT_RETRY_COUNT)
8+
// : 10;
9+
10+
// // When a message receives a timeout, how long should we wait before retrying?
11+
// const TIMEOUT_RETRY_DELAY =
12+
// process.env.WORKER_TIMEOUT_RETRY_DELAY ??
13+
// process.env.WORKER_MESSAGE_TIMEOUT_SECONDS ??
14+
// 30 * 1000;
15+
516
export const sendEvent = <T>(
6-
context: Pick<Context, 'logger' | 'channel' | 'id'>,
17+
context: Pick<Context, 'logger' | 'channel' | 'id' | 'options'>,
718
event: string,
819
payload?: any,
920
attempts?: number
1021
) => {
11-
const thisAttempt = attempts ?? 1;
22+
// Low defaults here are better for unit tests
23+
const { timeoutRetryCount = 1, timeoutRetryDelay = 1 } =
24+
context.options ?? {};
1225

13-
// When a message receives a timeout, how many times should we retry?
14-
const TIMEOUT_RETRY_COUNT = process.env.WORKER_TIMEOUT_RETRY_COUNT
15-
? parseInt(process.env.WORKER_TIMEOUT_RETRY_COUNT)
16-
: 10;
17-
18-
// When a message receives a timeout, how long should we wait before retrying?
19-
const TIMEOUT_RETRY_DELAY =
20-
process.env.WORKER_TIMEOUT_RETRY_DELAY ??
21-
process.env.WORKER_MESSAGE_TIMEOUT_SECONDS ??
22-
30 * 1000;
26+
const thisAttempt = attempts ?? 1;
2327

2428
const { channel, logger, id: runId = '<unknown run>' } = context;
2529

@@ -55,25 +59,20 @@ export const sendEvent = <T>(
5559
report(new LightningSocketError(event, message));
5660
})
5761
.receive('timeout', () => {
58-
if (thisAttempt >= TIMEOUT_RETRY_COUNT) {
62+
if (thisAttempt >= timeoutRetryCount) {
5963
report(new LightningTimeoutError(event));
6064
} else {
6165
logger.warn(
6266
`${runId} event ${event} timed out, will retry (attempt ${
6367
thisAttempt + 1
64-
} of ${TIMEOUT_RETRY_COUNT})`
68+
} of ${timeoutRetryCount})`
6569
);
6670

67-
const delay =
68-
typeof TIMEOUT_RETRY_DELAY === 'string'
69-
? parseInt(typeof TIMEOUT_RETRY_DELAY, 10)
70-
: TIMEOUT_RETRY_DELAY;
71-
7271
setTimeout(() => {
7372
sendEvent<T>(context, event, payload, thisAttempt + 1)
7473
.then(resolve)
7574
.catch(reject);
76-
}, delay);
75+
}, timeoutRetryDelay);
7776
}
7877
})
7978
.receive('ok', resolve);

packages/ws-worker/test/reasons.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ test('exception: failed to load credential', async (t) => {
247247
});
248248

249249
test('exception: credential timeout', async (t) => {
250+
process.env.WORKER_TIMEOUT_RETRY_DELAY = '1';
251+
process.env.WORKER_TIMEOUT_RETRY_COUNT = '5';
252+
250253
const plan = createPlan({
251254
id: 'aa',
252255
expression: 'export default [(s) => s]',
@@ -264,6 +267,9 @@ test('exception: credential timeout', async (t) => {
264267
});
265268

266269
test('kill: timeout', async (t) => {
270+
process.env.WORKER_TIMEOUT_RETRY_DELAY = '1';
271+
process.env.WORKER_TIMEOUT_RETRY_COUNT = '5';
272+
267273
const plan = createPlan({
268274
id: 'x',
269275
expression: 'export default [(s) => { while(true) { } }]',

packages/ws-worker/test/util/send-event.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ test.serial('should throw if the event is rejected', async (t) => {
8484
});
8585

8686
test.serial('should throw if the event timesout and retry is 1', async (t) => {
87-
process.env.WORKER_TIMEOUT_RETRY_DELAY = '1';
88-
process.env.WORKER_TIMEOUT_RETRY_COUNT = '1';
89-
9087
const EVENT_NAME = 'test';
9188
const channel = mockChannel({
9289
// No handler so no reply
@@ -104,14 +101,18 @@ test.serial('should throw if the event timesout and retry is 1', async (t) => {
104101
await t.throwsAsync(() => sendEvent(context, EVENT_NAME, {}), {
105102
instanceOf: LightningTimeoutError,
106103
});
104+
105+
// Check it did not retry at all
106+
const events = logger._history.filter(
107+
({ level, message }: any) =>
108+
level === 'warn' && /event test timed out/.test(message)
109+
);
110+
t.is(events.length, 0);
107111
});
108112

109113
test.serial(
110114
'should throw after 5 attempts if the event timesout and retry is 5',
111115
async (t) => {
112-
process.env.WORKER_TIMEOUT_RETRY_DELAY = '1';
113-
process.env.WORKER_TIMEOUT_RETRY_COUNT = '5';
114-
115116
const EVENT_NAME = 'test';
116117
const channel = mockChannel({
117118
// No handler so no reply
@@ -124,6 +125,10 @@ test.serial(
124125
id: 'x',
125126
} as any),
126127
logger,
128+
options: {
129+
timeoutRetryCount: 5,
130+
timeoutRetryDelay: 1,
131+
},
127132
};
128133

129134
await t.throwsAsync(() => sendEvent(context, EVENT_NAME, {}), {
@@ -141,8 +146,6 @@ test.serial(
141146
test.serial(
142147
'should pass after 5 attempts if the event timesout and retry is 5',
143148
async (t) => {
144-
process.env.WORKER_TIMEOUT_RETRY_DELAY = '1';
145-
process.env.WORKER_TIMEOUT_RETRY_COUNT = '5';
146149
let count = 0;
147150

148151
const EVENT_NAME = 'test';
@@ -165,6 +168,10 @@ test.serial(
165168
id: 'x',
166169
} as any),
167170
logger,
171+
options: {
172+
timeoutRetryCount: 5,
173+
timeoutRetryDelay: 1,
174+
},
168175
};
169176

170177
const reply = await sendEvent(context, EVENT_NAME, {});

0 commit comments

Comments
 (0)