Skip to content

Commit 36dc3db

Browse files
feat: ws-worker respond to wakeup call (#877)
* feat: trigger instant claim on work-available event * feat: update lightning mock to send work-available event via ws * fix: whitelist timestamp key in lighning mock * chore: fix type issues * tests: should recieve worker:queue message events from lightning * tests: add onMessage mock to socket mock * tests: manually trigger claim on connected workers * feat: emit messages for worker:queue directly * minor tweaks - make ?wakeup easier in the mock - catch claim errors in the worker after work-available - fix a typo in the readme * changesets * lint * version bumps * simplify wake-up tests * simplify wakup in mock * [email protected] --------- Co-authored-by: Joe Clark <[email protected]>
1 parent 7cbc8cc commit 36dc3db

File tree

18 files changed

+269
-33
lines changed

18 files changed

+269
-33
lines changed

integration-tests/worker/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# @openfn/integration-tests-worker
22

3+
## 1.0.77
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [87f10f7]
8+
- Updated dependencies [87f10f7]
9+
- @openfn/lightning-mock@2.1.0
10+
- @openfn/ws-worker@1.11.0
11+
312
## 1.0.76
413

514
### Patch Changes

integration-tests/worker/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@openfn/integration-tests-worker",
33
"private": true,
4-
"version": "1.0.76",
4+
"version": "1.0.77",
55
"description": "Lightning WOrker integration tests",
66
"author": "Open Function Group <[email protected]>",
77
"license": "ISC",

packages/lightning-mock/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @openfn/lightning-mock
22

3+
## 2.1.0
4+
5+
### Minor Changes
6+
7+
- 87f10f7: Add ?wakeup paramter to trigger work-available events
8+
39
## 2.0.31
410

511
### Patch Changes

packages/lightning-mock/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The server exposes a small dev API allowing you to post an Run.
3333
You can add an run (`{ jobs, triggers, edges }`) to the queue with:
3434

3535
```
36-
curl http://localhost:8888/run --json @tmp/run.json
36+
curl -X POST http://localhost:8888/run --json @tmp/run.json
3737
```
3838

3939
Here's an example run:

packages/lightning-mock/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openfn/lightning-mock",
3-
"version": "2.0.31",
3+
"version": "2.1.0",
44
"private": true,
55
"description": "A mock Lightning server",
66
"main": "dist/index.js",

packages/lightning-mock/src/api-dev.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import type {
1414
import { ServerState } from './server';
1515
import { RUN_COMPLETE } from './events';
1616
import type { DevServer, LightningEvents } from './types';
17+
import { PhoenixEvent } from './socket-server';
1718

1819
type Api = {
1920
startRun(runId: string): void;
21+
messageClients(message: PhoenixEvent): void;
2022
};
2123

2224
const setupDevAPI = (
@@ -40,6 +42,10 @@ const setupDevAPI = (
4042

4143
app.getDataclip = (id: string) => state.dataclips[id];
4244

45+
app.messageSocketClients = (message: PhoenixEvent) => {
46+
api.messageClients(message);
47+
};
48+
4349
app.enqueueRun = (run: LightningPlan, workerId = 'rte') => {
4450
state.runs[run.id] = run;
4551
state.results[run.id] = {
@@ -169,6 +175,19 @@ const setupRestAPI = (
169175

170176
app.enqueueRun(run);
171177

178+
// triggering wakeup in all connected workers
179+
if ('wakeup' in ctx.query) {
180+
logger.info(
181+
'WAKE UP! Sending work-available event to all listening workers'
182+
);
183+
app.messageSocketClients({
184+
topic: 'worker:queue',
185+
event: 'work-available',
186+
payload: {},
187+
join_ref: '',
188+
ref: '',
189+
});
190+
}
172191
ctx.response.status = 200;
173192
});
174193

packages/lightning-mock/src/api-sockets.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ const createSocketAPI = (
159159

160160
return {
161161
startRun,
162+
messageClients: wss.sendToClients.bind(this),
162163
close: () => {
163164
server.close();
164165
(wss as any).close();
@@ -352,8 +353,14 @@ const createSocketAPI = (
352353
runId: string
353354
) {
354355
const { ref, join_ref, topic } = evt;
355-
const { final_dataclip_id, reason, error_type, error_message, ...rest } =
356-
evt.payload;
356+
const {
357+
final_dataclip_id,
358+
reason,
359+
error_type,
360+
error_message,
361+
timestamp, // whitelist timestamp
362+
...rest
363+
} = evt.payload;
357364

358365
logger?.info('Completed run ', runId);
359366
logger?.debug(final_dataclip_id);

packages/lightning-mock/src/socket-server.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type MockSocketServer = typeof WebSocketServer & {
8080
topic: Topic,
8181
events: Record<string, EventHandler>
8282
) => { unsubscribe: () => void };
83+
sendToClients: (message: PhoenixEvent) => void;
8384
};
8485

8586
function createServer({
@@ -95,6 +96,8 @@ function createServer({
9596
phoenix: new Set([() => null]),
9697
};
9798

99+
const clients: Set<DevSocket> = new Set();
100+
98101
const wsServer =
99102
server ||
100103
new WebSocketServer({
@@ -202,6 +205,9 @@ function createServer({
202205

203206
logger?.debug(`>> [${topic}] ${event} ${ref} :: ${stringify(payload)}`);
204207

208+
// tracking connected worker:queue workers
209+
if (topic === 'worker:queue' && event === 'phx_join') clients.add(ws);
210+
205211
if (event in events) {
206212
// handle system/phoenix events
207213
// @ts-ignore
@@ -227,10 +233,19 @@ function createServer({
227233
}
228234
}
229235
});
236+
237+
ws.on('close', () => clients.delete(ws));
230238
});
231239

232240
const mockServer = wsServer as MockSocketServer;
233241

242+
mockServer.sendToClients = async (message) => {
243+
clients.forEach((client) => {
244+
// @ts-ignore
245+
client.sendJSON(message);
246+
});
247+
};
248+
234249
// debug API
235250
// TODO should this in fact be (topic, event, fn)?
236251
mockServer.listenToChannel = (topic: Topic, fn: EventHandler) => {

packages/lightning-mock/src/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import type {
55
Credential,
66
} from '@openfn/lexicon/lightning';
77
import type { ServerState } from './server';
8+
import { PhoenixEvent } from './socket-server';
89

9-
export type LightningEvents = 'log' | 'run-complete';
10+
export type LightningEvents = 'log' | 'run-complete' | string; // not complete!
1011

1112
export type DevServer = Koa & {
1213
state: ServerState;
@@ -20,13 +21,15 @@ export type DevServer = Koa & {
2021
getQueueLength(): number;
2122
getResult(runId: string): any;
2223
getState(): ServerState;
24+
messageSocketClients(message: PhoenixEvent): void;
2325
on(event: LightningEvents, fn: (evt: any) => void): void;
2426
once(event: LightningEvents, fn: (evt: any) => void): void;
2527
onSocketEvent(
2628
event: LightningEvents,
2729
runId: string,
28-
fn: (evt: any) => void
29-
): void;
30+
fn: (evt: any) => void,
31+
once?: boolean
32+
): () => void;
3033
registerRun(run: LightningPlan): void;
3134
removeAllListeners(): void;
3235
reset(): void;

packages/ws-worker/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# ws-worker
22

3+
## 1.11.0
4+
5+
### Minor Changes
6+
7+
- 87f10f7: Respond to `work:available` events.
8+
9+
When the worker receives `work:available` in the worker queue, it'll instantly trigger a claim event.
10+
11+
This claim is independent of the workloop and does not affect backoff in any way.
12+
313
## 1.10.0
414

515
### Minor Changes

0 commit comments

Comments
 (0)