Compare commits

...

4 Commits

6 changed files with 158 additions and 33 deletions

27
package-lock.json generated
View File

@ -9,10 +9,11 @@
"axios-retry": "^4.0.0",
"bottleneck": "^2.19.5",
"dotenv": "^16.4.5",
"ioredis": "^5.3.2"
"ioredis": "^5.3.2",
"readline": "^1.3.0"
},
"devDependencies": {
"@types/node": "^20.11.19",
"@types/node": "^20.11.26",
"copyfiles": "^2.4.1",
"typescript": "^5.3.3"
}
@ -23,9 +24,9 @@
"integrity": "sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg=="
},
"node_modules/@types/node": {
"version": "20.11.19",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz",
"integrity": "sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==",
"version": "20.12.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.5.tgz",
"integrity": "sha512-BD+BjQ9LS/D8ST9p5uqBxghlN+S42iuNxjsUGjeZobe/ciXzk2qb1B6IXc6AnRLS+yFJRpN2IPEHMzwspfDJNw==",
"dev": true,
"dependencies": {
"undici-types": "~5.26.4"
@ -486,6 +487,11 @@
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/readline": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz",
"integrity": "sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg=="
},
"node_modules/redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
@ -701,9 +707,9 @@
"integrity": "sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg=="
},
"@types/node": {
"version": "20.11.19",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz",
"integrity": "sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==",
"version": "20.12.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.5.tgz",
"integrity": "sha512-BD+BjQ9LS/D8ST9p5uqBxghlN+S42iuNxjsUGjeZobe/ciXzk2qb1B6IXc6AnRLS+yFJRpN2IPEHMzwspfDJNw==",
"dev": true,
"requires": {
"undici-types": "~5.26.4"
@ -1058,6 +1064,11 @@
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"readline": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz",
"integrity": "sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg=="
},
"redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",

View File

@ -4,10 +4,11 @@
"axios-retry": "^4.0.0",
"bottleneck": "^2.19.5",
"dotenv": "^16.4.5",
"ioredis": "^5.3.2"
"ioredis": "^5.3.2",
"readline": "^1.3.0"
},
"devDependencies": {
"@types/node": "^20.11.19",
"@types/node": "^20.12.5",
"copyfiles": "^2.4.1",
"typescript": "^5.3.3"
},

View File

@ -25,26 +25,26 @@
* @property {number} upvotes - The number of upvotes the comment has received.
*/
export type Comment = {
author_id: number;
author_name: string;
body: string;
body_html: string;
created_utc: number;
deleted_utc: number;
distinguished: boolean;
downvotes: number;
edited_utc: number;
id: number;
is_banned: boolean;
is_bot: boolean;
is_nsfw: boolean;
level: number;
permalink: string;
pinned: string;
post_id: number;
replies: number[];
reports: Record<string, unknown>;
score: number;
upvotes: number;
};
author_id: number;
author_name: string;
body: string;
body_html: string;
created_utc: number;
deleted_utc: number;
distinguished: boolean;
downvotes: number;
edited_utc: number;
id: number;
is_banned: boolean;
is_bot: boolean;
is_nsfw: boolean;
level: number;
permalink: string;
pinned: string | null;
parent_comment_id?: number | null;
post_id: number;
replies: number[];
reports: Record<string, unknown>;
score: number;
upvotes: number;
};

View File

@ -14,6 +14,7 @@ class RedisSessionManager {
public readonly client: Redis;
private constructor() {
if (!process.env.REDIS_HOST) throw new Error('Unknown Host')
const redisConfig: RedisConfig = {
host: process.env.REDIS_HOST!,
port: Number(process.env.REDIS_PORT),

View File

@ -0,0 +1,109 @@
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();

View File

@ -12,6 +12,9 @@
"types": [
"node"
],
"typeRoots": [
"./node_modules/@types"
],
"moduleResolution": "node"
},
"include": [