MCPcopy Index your code
hub / github.com/cameri/nostream / AppWorker

Class AppWorker

src/app/worker.ts:10–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8
9const logger = createLogger('app-worker')
10export class AppWorker implements IRunnable {
11 private watchers: FSWatcher[] | undefined
12
13 public constructor(
14 private readonly process: NodeJS.Process,
15 private readonly adapter: IWebSocketServerAdapter,
16 ) {
17 this.process
18 .on('message', this.onMessage.bind(this))
19 .on('SIGINT', this.onExit.bind(this))
20 .on('SIGHUP', this.onExit.bind(this))
21 .on('SIGTERM', this.onExit.bind(this))
22 .on('uncaughtException', this.onError.bind(this))
23 .on('unhandledRejection', this.onError.bind(this))
24 }
25
26 public run(): void {
27 this.watchers = SettingsStatic.watchSettings()
28
29 const port = process.env.PORT || process.env.RELAY_PORT || 8008
30 this.adapter.listen(typeof port === 'number' ? port : Number(port))
31 }
32
33 private onMessage(message: { eventName: string; event: unknown }): void {
34 this.adapter.emit(message.eventName, message.event)
35 }
36
37 private onError(error: Error) {
38 if (error.name === 'TypeError' && error.message === "Cannot read properties of undefined (reading '__knexUid')") {
39 logger.error(
40 'Unable to acquire connection. Please increase DB_MAX_POOL_SIZE, DB_ACQUIRE_CONNECTION_TIMEOUT and tune postgresql.conf to make use of server\'s resources.'
41 )
42 return
43 }
44 logger.error('uncaught error:', error)
45 }
46
47 private onExit() {
48 logger('exiting')
49 this.close(() => {
50 this.process.exit(0)
51 })
52 }
53
54 public close(callback?: () => void) {
55 logger('closing')
56 if (Array.isArray(this.watchers)) {
57 for (const watcher of this.watchers) {
58 watcher.close()
59 }
60 }
61 this.adapter.close(async () => {
62 await closeCacheClient()
63 callback?.()
64 })
65 logger('closed')
66 }
67}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected