Skip to content

Commit b115999

Browse files
committed
fix: working api
1 parent 83abdaa commit b115999

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

package-lock.json

Lines changed: 61 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"license": "ISC",
2424
"devDependencies": {
2525
"@degulabs/build": "^0.0.1",
26+
"@playwright/test": "^1.48.2",
2627
"@types/ms": "^0.7.34",
2728
"@types/node": "^22.8.5",
2829
"@types/ws": "^8.5.12",

src/genericService.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
import { setTimeout } from 'timers/promises';
12
import { WebSocket } from 'ws';
23
import * as dockerApi from './dockerApi.js';
34

5+
async function connectWebSocket(address: string, deadline: number): Promise<WebSocket|undefined> {
6+
while (Date.now() < deadline) {
7+
const socket = new WebSocket(address);
8+
const result = await new Promise<boolean>((resolve, reject) => {
9+
socket.on('open', () => resolve(true));
10+
socket.on('error', () => reject(false));
11+
});
12+
if (result)
13+
return socket;
14+
await setTimeout(100, undefined);
15+
}
16+
return undefined;
17+
}
18+
419
export class GenericService {
520
static async launch(options: {
621
imageName: string,
@@ -17,7 +32,7 @@ export class GenericService {
1732
}) {
1833
const images = await dockerApi.listImages();
1934
const imageName = options.imageName + '-deadmanswitch';
20-
const image = images.find(image => image.names.includes(options.imageName));
35+
const image = images.find(image => image.names.includes(imageName));
2136
if (!image)
2237
throw new Error(`ERROR: no image named "${imageName}" - run 'npx deadmanswitch-pull ${options.imageName}'`);
2338

@@ -32,6 +47,7 @@ export class GenericService {
3247
command: options.command,
3348
env: options.env,
3449
});
50+
const deadline = Date.now() + 10000; // 10s is default timeout before deadmanswitch will kill container.
3551
const container = (await dockerApi.listContainers()).find(container => container.containerId === containerId);
3652
if (!container)
3753
throw new Error('ERROR: failed to launch container!');
@@ -41,17 +57,14 @@ export class GenericService {
4157
await dockerApi.stopContainer({ containerId: container.containerId });
4258
throw new Error('Failed to expose service to host');
4359
}
44-
const ws = new WebSocket(`ws://localhost:${switchBinding.hostPort}`);
45-
const wsConnectionPromise = new Promise<void>((resolve, reject) => {
46-
ws.on('open', () => resolve());
47-
ws.on('error', reject);
48-
});
49-
await wsConnectionPromise;
50-
const service = new GenericService(containerId, serviceBinding.hostPort, ws);
5160

5261
while (!(await dockerApi.isContainerHealthy(container.containerId)))
53-
await new Promise(x => setTimeout(x, 100));
62+
await setTimeout(100, undefined);
5463

64+
const ws = await connectWebSocket(`ws://localhost:${switchBinding.hostPort}`, deadline);
65+
if (!ws)
66+
throw new Error('Failed to connect to launched container');
67+
const service = new GenericService(containerId, serviceBinding.hostPort, ws);
5568
return service;
5669
}
5770

0 commit comments

Comments
 (0)