Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-jars-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openfn/ws-worker': patch
---

Fail a run cleanly instead of hanging when Lightning rejects the plan fetch after a claim
15 changes: 10 additions & 5 deletions packages/ws-worker/src/channels/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ const joinRunChannel = (
if (!didReceiveOk) {
didReceiveOk = true;
logger.success(`connected to ${channelName}`, e);
const run = await sendEvent<GetPlanReply>(
{ channel, logger, id: runId, options: {} },
GET_PLAN
);
resolve({ channel, run });
try {
const run = await sendEvent<GetPlanReply>(
{ channel, logger, id: runId, options: {} },
GET_PLAN
);
resolve({ channel, run });
} catch (err) {
channel?.leave();
reject(err);
}
}
})
.receive('error', (err: any) => {
Expand Down
24 changes: 24 additions & 0 deletions packages/ws-worker/test/channels/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ test('should fail to join an run channel with an invalid token', async (t) => {
}
});

test('should reject (not hang) when fetch:plan replies an error', async (t) => {
const logger = createMockLogger();
const socket = new MockSocket('www', {
'run:a': mockChannel({
join: () => ({ status: 'ok' }),
[GET_PLAN]: () => {
throw { reason: 'adaptor_not_ready' };
},
}),
});

// race against a short timeout so a hang fails fast instead of hanging CI
const result = await Promise.race([
joinRunChannel(socket, 'x.y.z', 'a', logger).then(
() => 'resolved',
() => 'rejected'
),
new Promise((resolve) => setTimeout(() => resolve('timed-out'), 200)),
]);

// 'timed-out' would mean joinRunChannel's promise never settled
t.is(result, 'rejected');
});

test('should log an error including channel state when the channel errors', async (t) => {
const logger = createMockLogger();
const channel = mockChannel({
Expand Down
Loading