-
Notifications
You must be signed in to change notification settings - Fork 368
/
local.runner.ts
72 lines (61 loc) · 2.44 KB
/
local.runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import type { Runner } from './runner.js';
import { RunnerType } from './runner.js';
import type { ChildProcess } from 'child_process';
import { execSync, spawn } from 'child_process';
import { getRunnerClient } from '@nangohq/nango-runner';
import { logger } from '@nangohq/shared';
export class LocalRunner implements Runner {
public client: any;
public runnerType: RunnerType = RunnerType.Local;
constructor(
public readonly id: string,
public readonly url: string,
private readonly childProcess: ChildProcess
) {
this.client = getRunnerClient(this.url);
}
suspend() {
this.childProcess.kill();
}
toJSON() {
return { runnerType: this.runnerType, id: this.id, url: this.url };
}
static fromJSON(obj: any): LocalRunner {
throw new Error(`'fromJSON(${obj})' not implemented`);
}
static async getOrStart(runnerId: string): Promise<LocalRunner> {
try {
const port = Math.floor(Math.random() * 1000) + 11000; // random port between 11000 and 12000;
let nodePath = '';
try {
nodePath = execSync('which node', { encoding: 'utf-8' }).trim();
} catch {
throw new Error('Unable to find node');
}
const nangoRunnerPath = process.env['NANGO_RUNNER_PATH'] || '../runner/dist/app.js';
const cmd = nodePath;
const runnerLocation = `${nangoRunnerPath}`;
const cmdOptions = [runnerLocation, port.toString(), runnerId];
logger.info(`[Runner] Starting runner with command: ${cmd} ${cmdOptions.join(' ')} `);
const childProcess = spawn(cmd, cmdOptions, {
stdio: [null, null, null]
});
if (!childProcess) {
throw new Error('Unable to spawn runner process');
}
if (childProcess.stdout) {
childProcess.stdout.on('data', (data) => {
logger.info(`[Runner] ${data.toString()} `);
});
}
if (childProcess.stderr) {
childProcess.stderr.on('data', (data) => {
logger.info(`[Runner][ERROR] ${data.toString()} `);
});
}
return new LocalRunner(runnerId, `http://localhost:${port}`, childProcess);
} catch (err) {
throw new Error(`Unable to get runner ${runnerId}: ${err}`);
}
}
}