-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathworker.ts
More file actions
67 lines (58 loc) · 2.01 KB
/
Copy pathworker.ts
File metadata and controls
67 lines (58 loc) · 2.01 KB
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
import { IRunnable } from '../@types/base'
import { IWebSocketServerAdapter } from '../@types/adapters'
import { closeCacheClient } from '../cache/client'
import { createLogger } from '../factories/logger-factory'
import { FSWatcher } from 'fs'
import { SettingsStatic } from '../utils/settings'
const debug = createLogger('app-worker')
export class AppWorker implements IRunnable {
private watchers: FSWatcher[] | undefined
public constructor(
private readonly process: NodeJS.Process,
private readonly adapter: IWebSocketServerAdapter
) {
this.process
.on('message', this.onMessage.bind(this))
.on('SIGINT', this.onExit.bind(this))
.on('SIGHUP', this.onExit.bind(this))
.on('SIGTERM', this.onExit.bind(this))
.on('uncaughtException', this.onError.bind(this))
.on('unhandledRejection', this.onError.bind(this))
}
public run(): void {
this.watchers = SettingsStatic.watchSettings()
const port = process.env.PORT || process.env.RELAY_PORT || 8008
this.adapter.listen(typeof port === 'number' ? port : Number(port))
}
private onMessage(message: { eventName: string, event: unknown }): void {
this.adapter.emit(message.eventName, message.event)
}
private onError(error: Error) {
if (error.name === 'TypeError' && error.message === "Cannot read properties of undefined (reading '__knexUid')") {
console.error(
'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.'
)
return
}
console.error('uncaught error:', error)
}
private onExit() {
debug('exiting')
this.close(() => {
this.process.exit(0)
})
}
public close(callback?: () => void) {
debug('closing')
if (Array.isArray(this.watchers)) {
for (const watcher of this.watchers) {
watcher.close()
}
}
this.adapter.close(async () => {
await closeCacheClient()
callback?.()
})
debug('closed')
}
}