109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import { Comment } from "../../src/rdrama/models/Comment";
|
|
import * as readline from 'readline';
|
|
import RedisCommentService from "../../src/redis/services/CommentService"
|
|
import RedisSessionManager from '../../src/redis/session/SessionManager';
|
|
|
|
|
|
const comment: Comment = {
|
|
"author_id": 9978,
|
|
"author_name": "J",
|
|
"body": "!!oregon 3",
|
|
"body_html": "<p>!!oregon 3</p>",
|
|
"created_utc": 1712512562,
|
|
"deleted_utc": 0,
|
|
"distinguished": false,
|
|
"downvotes": 0,
|
|
"edited_utc": 0,
|
|
"id": 6227144,
|
|
"is_banned": false,
|
|
"is_bot": false,
|
|
"is_nsfw": false,
|
|
"level": 1,
|
|
"permalink": "/comment/6227144#context",
|
|
"pinned": null,
|
|
"post_id": 0,
|
|
"replies": [],
|
|
"reports": {},
|
|
"score": 1,
|
|
"upvotes": 1
|
|
}
|
|
|
|
|
|
|
|
async function main(): Promise<void> {
|
|
console.log('Redis Start');
|
|
const redisManager = RedisSessionManager.getInstance();
|
|
continuouslyPrompt(redisManager);
|
|
setupProcessListeners(redisManager);
|
|
}
|
|
|
|
// Create an interface for input and output
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
// Function to continuously prompt for commands, update, and publish comments
|
|
const continuouslyPrompt = async (redisManager: RedisSessionManager) => {
|
|
rl.setPrompt('Enter command (or type "exit" to quit): ');
|
|
rl.prompt();
|
|
|
|
rl.on('line', async (line) => {
|
|
if (line.trim().toLowerCase() === 'exit') {
|
|
console.log('Exiting...');
|
|
rl.close();
|
|
} else {
|
|
// Update the comment body and body_html with the provided command
|
|
comment.body = line.trim();
|
|
comment.body_html = `<p>${line.trim()}</p>`;
|
|
|
|
// Publish the updated comment to the Redis server
|
|
try {
|
|
const publishResult = await RedisCommentService.publishComment(comment);
|
|
console.log(`Comment published with result: ${publishResult}`);
|
|
} catch (error) {
|
|
console.error('Failed to publish comment:', error);
|
|
}
|
|
|
|
rl.prompt();
|
|
}
|
|
}).on('close', async () => {
|
|
console.log('Goodbye!');
|
|
await redisManager.shutdown();
|
|
process.exit(0);
|
|
});
|
|
};
|
|
|
|
|
|
|
|
function setupProcessListeners(redisManager: RedisSessionManager) {
|
|
process.on('SIGINT', async () => {
|
|
console.log('SIGINT received. Shutting down gracefully...');
|
|
await shutdownServices(redisManager);
|
|
});
|
|
|
|
process.on('SIGTERM', async () => {
|
|
console.log('SIGTERM received. Shutting down gracefully...');
|
|
await shutdownServices(redisManager);
|
|
});
|
|
|
|
process.on('uncaughtException', async (error) => {
|
|
console.error('Uncaught Exception:', error);
|
|
await shutdownServices(redisManager);
|
|
process.exit(1); // Exit with error
|
|
});
|
|
|
|
process.on('unhandledRejection', async (reason, promise) => {
|
|
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
|
await shutdownServices(redisManager);
|
|
process.exit(1); // Exit with error
|
|
});
|
|
}
|
|
|
|
async function shutdownServices(redisManager: RedisSessionManager) {
|
|
await redisManager.shutdown();
|
|
console.log('Services shut down successfully.');
|
|
process.exit(0); // Exit cleanly
|
|
}
|
|
|
|
main(); |