| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | /** |
| b69ab31 | | | 9 | * This file is expected to be run via child_process.fork() where: |
| b69ab31 | | | 10 | * |
| b69ab31 | | | 11 | * - The arguments to `startServer()` are JSON-serialized as the value |
| b69ab31 | | | 12 | * of the ISL_SERVER_ARGS environment variable. |
| b69ab31 | | | 13 | * - Communication to the parent process must be done via process.send() |
| b69ab31 | | | 14 | * and the parent process expects messages that conform to |
| b69ab31 | | | 15 | * `ChildProcessResponse`. |
| b69ab31 | | | 16 | */ |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | import type {StartServerArgs, StartServerResult} from './server'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | import * as fs from 'node:fs'; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | /** |
| b69ab31 | | | 23 | * This defines the shape of the messages that the parent process accepts. |
| b69ab31 | | | 24 | * As such, it is imperative that these types are serializable. |
| b69ab31 | | | 25 | */ |
| b69ab31 | | | 26 | export type ChildProcessResponse = |
| b69ab31 | | | 27 | | { |
| b69ab31 | | | 28 | type: 'message'; |
| b69ab31 | | | 29 | args: Parameters<typeof console.log>; |
| b69ab31 | | | 30 | } |
| b69ab31 | | | 31 | | { |
| b69ab31 | | | 32 | type: 'result'; |
| b69ab31 | | | 33 | result: StartServerResult; |
| b69ab31 | | | 34 | }; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | function sendMessageToParentProcess(msg: ChildProcessResponse): void { |
| b69ab31 | | | 37 | process.send?.(msg, undefined, {swallowErrors: true}); |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | function info(...args: Parameters<typeof console.log>): void { |
| b69ab31 | | | 41 | const msg = { |
| b69ab31 | | | 42 | type: 'message', |
| b69ab31 | | | 43 | args, |
| b69ab31 | | | 44 | } as ChildProcessResponse; |
| b69ab31 | | | 45 | sendMessageToParentProcess(msg); |
| b69ab31 | | | 46 | } |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | const args: StartServerArgs = JSON.parse(process.env.ISL_SERVER_ARGS as string); |
| b69ab31 | | | 49 | args.logInfo = info; |
| b69ab31 | | | 50 | import('./server') |
| b69ab31 | | | 51 | .then(({startServer}) => startServer(args)) |
| b69ab31 | | | 52 | .then((result: StartServerResult) => { |
| b69ab31 | | | 53 | sendMessageToParentProcess({type: 'result', result}); |
| b69ab31 | | | 54 | }) |
| b69ab31 | | | 55 | .catch(error => |
| b69ab31 | | | 56 | sendMessageToParentProcess({type: 'result', result: {type: 'error', error: String(error)}}), |
| b69ab31 | | | 57 | ); |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | process.on('uncaughtException', err => { |
| b69ab31 | | | 60 | const {logFileLocation} = args; |
| b69ab31 | | | 61 | fs.promises.appendFile( |
| b69ab31 | | | 62 | logFileLocation, |
| b69ab31 | | | 63 | `\n[${new Date().toString()}] ISL server child process got an uncaught exception:\n${ |
| b69ab31 | | | 64 | err?.stack ?? err?.message |
| b69ab31 | | | 65 | }\n\n`, |
| b69ab31 | | | 66 | 'utf8', |
| b69ab31 | | | 67 | ); |
| b69ab31 | | | 68 | }); |