This repository was archived by the owner on Oct 12, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
193 lines (163 loc) · 5.87 KB
/
Copy pathserver.js
File metadata and controls
193 lines (163 loc) · 5.87 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
if (process.argv.includes('--version') || process.argv.includes('-v')) {
const { version } = require('./package.json');
// eslint-disable-next-line no-console
console.log(`flowly-plugin-server v${version}`);
process.exit(0);
}
const path = require('path');
const { createWSServer } = require('./core/ws-server');
const { authenticate } = require('./core/auth');
const { createContext } = require('./core/context');
const { loadModules } = require('./core/module-loader');
const { Pool } = require('pg');
const Redis = require('ioredis');
const { initRoleSubscriber } = require('./core/redis-subscriber');
const PORT = parseInt(process.env.PORT, 10) || 8094;
const HOST = process.env.HOST || '127.0.0.1';
/* ------------------------------------------------------------------
* Logger
* ---------------------------------------------------------------- */
/**
* Formats a log line following the project logging convention.
* @param {'INFO'|'WARN'|'ERROR'} level
* @param {string} mod - Module/component name.
* @param {string} message
* @returns {string}
*/
function log(level, mod, message) {
const ts = new Date().toISOString().replace('T', ' ').replace('Z', '');
return `[${ts}] [${level}] [${mod}] ${message}`;
}
/**
* Logger wrapper that prints to stdout/stderr.
* @param {'INFO'|'WARN'|'ERROR'} level
* @param {string} mod
* @param {string} message
*/
function logger(level, mod, message) {
const line = log(level, mod, message);
if (level === 'ERROR') {
console.error(line); // eslint-disable-line no-console
} else {
console.log(line); // eslint-disable-line no-console
}
}
/* ------------------------------------------------------------------
* Load modules
* ---------------------------------------------------------------- */
/* ------------------------------------------------------------------
* PostgreSQL pool
* ---------------------------------------------------------------- */
const coreServices = {};
if (process.env.PG_HOST) {
const pool = new Pool({
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT, 10) || 5432,
database: process.env.PG_DB || 'hasura_app',
user: process.env.PG_USER || 'postgres',
password: process.env.PG_PASSWORD || '',
});
pool.on('error', (err) => {
logger('ERROR', 'pg', `Pool error: ${err.message}`);
});
coreServices.pg = pool;
logger(
'INFO',
'pg',
`Pool created for ${process.env.PG_HOST}:${process.env.PG_PORT || 5432}/${process.env.PG_DB || 'hasura_app'}`,
);
} else {
logger('WARN', 'pg', 'PG_HOST not set — PostgreSQL disabled');
}
/* ------------------------------------------------------------------
* Redis (ioredis) — used for Akka pub/sub and role subscriber
* ---------------------------------------------------------------- */
const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
const REDIS_PORT = parseInt(process.env.REDIS_PORT, 10) || 6379;
const REDIS_PASSWORD = process.env.REDIS_PASSWORD || undefined;
/** Main Redis client for pub/sub publishing (context.publishToAkka). */
const redisPublisher = new Redis({
host: REDIS_HOST,
port: REDIS_PORT,
password: REDIS_PASSWORD,
lazyConnect: true,
retryStrategy: (times) => Math.min(times * 500, 5000),
});
redisPublisher
.connect()
.then(() => {
logger('INFO', 'redis', `Publisher connected to ${REDIS_HOST}:${REDIS_PORT}`);
})
.catch((err) => {
logger('WARN', 'redis', `Publisher connection failed: ${err.message} — will retry`);
});
coreServices.redis = redisPublisher;
/**
* Separate subscriber client — ioredis requires a dedicated connection
* for subscribe mode (no other commands can be issued on it).
*/
const redisSubscriber = new Redis({
host: REDIS_HOST,
port: REDIS_PORT,
password: REDIS_PASSWORD,
lazyConnect: true,
retryStrategy: (times) => Math.min(times * 500, 5000),
});
redisSubscriber
.connect()
.then(() => {
logger('INFO', 'redis', `Subscriber connected to ${REDIS_HOST}:${REDIS_PORT}`);
})
.catch((err) => {
logger('WARN', 'redis', `Subscriber connection failed: ${err.message} — will retry`);
});
/* ------------------------------------------------------------------
* Load modules
* ---------------------------------------------------------------- */
const modulesDir = path.join(__dirname, 'modules');
const { dispatch, unloadAll } = loadModules(modulesDir, coreServices, logger);
/* ------------------------------------------------------------------
* Start server
* ---------------------------------------------------------------- */
const { close, connections } = createWSServer({
port: PORT,
host: HOST,
authenticate,
dispatch,
coreServices,
createContext,
logger,
onListening: () => {
logger('INFO', 'server', `Listening on ${HOST}:${PORT}`);
},
});
/* ------------------------------------------------------------------
* Redis role subscriber — detects BBB role promotions via Akka events
* ---------------------------------------------------------------- */
const roleSubscriber = initRoleSubscriber(redisSubscriber, connections, logger);
/* ------------------------------------------------------------------
* Graceful shutdown
* ---------------------------------------------------------------- */
/**
* Handles SIGINT / SIGTERM for clean shutdown.
* @param {string} signal - The received signal name.
*/
async function shutdown(signal) {
logger('INFO', 'server', `Received ${signal}, shutting down…`);
try {
unloadAll();
roleSubscriber.stop();
await redisSubscriber.quit().catch(() => {});
await redisPublisher.quit().catch(() => {});
if (coreServices.pg) await coreServices.pg.end();
await close();
logger('INFO', 'server', 'Server closed');
process.exit(0);
} catch (err) {
logger('ERROR', 'server', `Shutdown error: ${err.message}`);
process.exit(1);
}
}
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));