forked from SubStream-Protocol/SubStream-Protocol-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
208 lines (178 loc) · 6.88 KB
/
worker.js
File metadata and controls
208 lines (178 loc) · 6.88 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env node
const { loadConfig } = require('./src/config');
const { BackgroundWorkerService } = require('./src/services/backgroundWorkerService');
const { SorobanIndexerWorker } = require('./src/services/sorobanIndexerWorker');
const { getVaultService } = require('./src/services/vaultService');
const { getSorobanIndexerFailoverHandler, resetSorobanIndexerFailoverHandler } = require('./src/services/sorobanIndexerFailover');
const { getRedisCacheFailoverHandler, resetRedisCacheFailoverHandler } = require('./src/services/redisCacheFailover');
// Initialize Vault service if enabled
let vaultService = null;
if (process.env.VAULT_ENABLED === 'true') {
vaultService = getVaultService({
vaultAddr: process.env.VAULT_ADDR || 'http://vault:8200',
vaultRole: process.env.VAULT_ROLE || 'substream-backend',
authPath: process.env.VAULT_AUTH_PATH || 'auth/kubernetes',
secretPath: process.env.VAULT_SECRET_PATH || 'secret/data/substream',
dbPath: process.env.VAULT_DB_PATH || 'database/creds/substream-role'
});
console.log('[Vault] Vault integration enabled in worker');
}
// SIGHUP signal handler for hot-reloading secrets
if (process.env.VAULT_ENABLED === 'true' && vaultService) {
process.on('SIGHUP', async () => {
console.log('[Vault] Received SIGHUP signal, reloading secrets...');
try {
await vaultService.reloadSecrets();
console.log('[Vault] Successfully reloaded secrets on SIGHUP');
} catch (error) {
console.error('[Vault] Failed to reload secrets on SIGHUP:', error.message);
}
});
}
// Graceful shutdown handler
const cleanup = async () => {
console.log('[Shutdown] Cleaning up worker resources...');
if (vaultService) {
try {
await vaultService.cleanup();
console.log('[Vault] Vault service cleaned up');
} catch (error) {
console.error('[Vault] Error during cleanup:', error.message);
}
}
process.exit(0);
};
process.on('SIGTERM', cleanup);
process.on('SIGINT', cleanup);
// Failover signal handler (SIGUSR2)
let sorobanFailoverHandler = null;
let redisFailoverHandler = null;
if (process.env.ENABLE_FAILOVER_HANDLING === 'true') {
process.on('SIGUSR2', async () => {
console.log('[Failover] Received SIGUSR2 signal, handling failover...');
try {
const config = await loadConfig(process.env, vaultService);
if (!sorobanFailoverHandler) {
sorobanFailoverHandler = getSorobanIndexerFailoverHandler(config);
await sorobanFailoverHandler.initialize();
}
const sorobanResult = await sorobanFailoverHandler.handleFailover();
console.log('[Failover] Soroban indexer failover result:', sorobanResult);
if (!redisFailoverHandler) {
redisFailoverHandler = getRedisCacheFailoverHandler(config);
await redisFailoverHandler.initialize();
}
const redisResult = await redisFailoverHandler.handleFailover({ strategy: 'application' });
console.log('[Failover] Redis cache clear result:', redisResult);
console.log('[Failover] Failover handled successfully');
} catch (error) {
console.error('[Failover] Failed to handle failover:', error.message);
process.exit(1);
}
});
console.log('[Failover] Failover handling enabled (SIGUSR2)');
}
/**
* Standalone background worker process
*/
async function startWorker() {
console.log('Starting SubStream Background Worker...');
// Initialize Vault if enabled
if (vaultService) {
try {
await vaultService.initialize();
console.log('[Vault] Vault service initialized successfully');
} catch (vaultError) {
console.error('[Vault] Vault initialization failed, continuing with environment variables:', vaultError.message);
}
}
const config = await loadConfig(process.env, vaultService);
// RabbitMQ Check
if (!config.rabbitmq || (!config.rabbitmq.url && !config.rabbitmq.host)) {
console.error('RabbitMQ configuration is missing. Please set RABBITMQ_URL or RABBITMQ_HOST environment variables.');
process.exit(1);
}
const worker = new BackgroundWorkerService(config.rabbitmq);
// Handle graceful shutdown for main background worker
const shutdown = async (signal) => {
console.log(`\nReceived ${signal}. Shutting down gracefully...`);
try {
await worker.stop();
console.log('Background worker stopped successfully');
process.exit(0);
} catch (error) {
console.error('Error during shutdown:', error);
process.exit(1);
}
};
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
// Start the main background worker
try {
await worker.start();
console.log('Background worker started successfully');
console.log('Processing events from queues:');
console.log(` - Events: ${config.rabbitmq.eventQueue}`);
console.log(` - Notifications: ${config.rabbitmq.notificationQueue}`);
console.log(` - Emails: ${config.rabbitmq.emailQueue}`);
console.log(` - Leaderboard: ${config.rabbitmq.leaderboardQueue}`);
} catch (error) {
console.error('Failed to start background worker:', error);
process.exit(1);
}
}
// ========================
// Worker Routing Logic
// ========================
const args = process.argv.slice(2);
if (args.includes('--soroban')) {
// Start Soroban Indexer Worker
console.log('[Worker] Starting Soroban Indexer...');
const sorobanWorker = new SorobanIndexerWorker();
if (args.includes('--health')) {
sorobanWorker.healthCheck()
.then(health => {
console.log(JSON.stringify(health, null, 2));
process.exit(health.healthy ? 0 : 1);
})
.catch((error) => {
console.error('Soroban worker health check failed:', error);
process.exit(1);
});
} else {
sorobanWorker.start().catch(error => {
console.error('Failed to start Soroban worker:', error);
process.exit(1);
});
}
} else {
// Start Main Background Worker + Webhook Dispatcher
if (args.includes('--health')) {
const config = loadConfig();
const worker = new BackgroundWorkerService(config.rabbitmq);
worker.start()
.then(() => {
const status = worker.getStatus();
console.log(JSON.stringify(status, null, 2));
process.exit(status.isRunning && status.connected ? 0 : 1);
})
.catch((error) => {
console.error('Health check failed:', error);
process.exit(1);
});
} else {
startWorker();
// === NEW: Start Merchant Webhook Dispatcher Worker ===
console.log('[Worker] Starting Merchant Webhook Dispatcher...');
try {
require('./src/workers/webhookWorker');
} catch (error) {
console.error('[Worker] Failed to start Webhook Dispatcher:', error.message);
// Don't crash the whole worker if webhook fails to start
}
}
}
// Keep the process alive when running as background worker
if (!args.includes('--soroban') && !args.includes('--health')) {
process.stdin.resume();
}