Skip to content

Commit 46ac1b6

Browse files
committed
Added shutdown signal handling
1 parent ce90e31 commit 46ac1b6

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ ENV NODE_ENV=production
3939
ENV RSS_WATCHER_DATA_DIR=/app/data
4040

4141
# Start the application
42-
CMD ["npm", "start"]
42+
CMD ["node", "dist/server/index.js"]

server/src/feedMonitor.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ export class FeedMonitor {
151151
private async sendNotification(item: FeedItem) {
152152
const config = this.configManager.getConfig();
153153
const ntfyUrl = `${config.ntfyServerAddress}/${config.ntfyTopic}`;
154-
// console.log(`Sending notification for ${item.title}`);
154+
const sanitizedTitle = item.title.replace(/[^\x20-\x7E]/g, '');
155155

156156
try {
157157
// Sanitize the title by removing any characters
158158
// that aren't in the ASCII printable range (0x20 to 0x7E)
159-
const sanitizedTitle = item.title.replace(/[^\x20-\x7E]/g, '');
159+
console.log(`Sending notification for ${sanitizedTitle}`);
160160
await fetch(ntfyUrl, {
161161
method: 'POST',
162162
body: item.description,
@@ -166,7 +166,7 @@ export class FeedMonitor {
166166
}
167167
});
168168
} catch (error) {
169-
console.error(`Error sending notification for ${item.title}:`, error);
169+
console.error(`Error sending notification for ${sanitizedTitle}:`, error);
170170
}
171171
}
172172

server/src/index.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import './bootstrap.js';
22
import { Server } from './server.js';
33

4+
let server: Server;
5+
46
async function startApp() {
5-
const server = new Server();
7+
server = new Server();
68
await server.start(3000);
79
}
810

11+
async function shutdown(signal: string) {
12+
console.log(`\nReceived ${signal}, starting graceful shutdown...`);
13+
if (server) {
14+
await server.stop();
15+
console.log('Server stopped successfully');
16+
}
17+
process.exit(0);
18+
}
19+
20+
// Handle shutdown signals
21+
process.on('SIGTERM', () => shutdown('SIGTERM'));
22+
process.on('SIGINT', () => shutdown('SIGINT'));
23+
924
startApp().catch(console.error);

server/src/server.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class Server {
1212
private app: express.Application;
1313
private configManager: ConfigManager;
1414
private feedMonitor: FeedMonitor;
15+
private server?: ReturnType<typeof express.application.listen>;
1516

1617
constructor() {
1718
this.app = express();
@@ -122,12 +123,36 @@ export class Server {
122123

123124
public async start(port: number = 3000) {
124125
await this.setupVite();
125-
this.app.listen(port, () => {
126-
console.log(`Server running on port ${port}`);
126+
this.server = this.app.listen(port, () => {
127+
console.log(`
128+
╔═══════════════════════════════════════════╗
129+
║ RSS Watcher ║
130+
╚═══════════════════════════════════════════╝
131+
🚀 Server is running on port ${port}
132+
📡 Mode: ${process.env.NODE_ENV || 'production'}
133+
134+
Monitoring your feeds! 📰
135+
`);
127136
});
128137
}
129138

130-
public stop() {
139+
public async stop() {
140+
console.log('Stopping feed monitor...');
131141
this.feedMonitor.stop();
142+
143+
if (this.server) {
144+
console.log('Closing HTTP server...');
145+
return new Promise<void>((resolve, reject) => {
146+
this.server!.close((err) => {
147+
if (err) {
148+
console.error('Error while closing HTTP server:', err);
149+
reject(err);
150+
} else {
151+
console.log('HTTP server closed successfully');
152+
resolve();
153+
}
154+
});
155+
});
156+
}
132157
}
133158
}

0 commit comments

Comments
 (0)