DTP Base provides a scalable and secure Node.js application development harness ready for production service.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

70 lines
1.5 KiB

// chat-processor.js
// Copyright (C) 2024 DTP Technologies, LLC
// All Rights Reserved
'use strict';
import 'dotenv/config';
import path, { dirname } from 'path';
import { SiteRuntime } from '../../lib/site-lib.js';
import { CronJob } from 'cron';
const CRON_TIMEZONE = 'America/New_York';
class ChatProcessorService extends SiteRuntime {
static get name ( ) { return 'ChatProcessorService'; }
static get slug ( ) { return 'chatProcessor'; }
constructor (rootPath) {
super(ChatProcessorService, rootPath);
}
async start ( ) {
await super.start();
const mongoose = await import('mongoose');
this.ChatMessage = mongoose.model('ChatMessage');
/*
* Cron jobs
*/
const messageExpireSchedule = '0 0 * * * *'; // Every hour
this.cronJob = new CronJob(
messageExpireSchedule,
this.expireChatMessages.bind(this),
null,
true,
CRON_TIMEZONE,
);
}
async shutdown ( ) {
this.log.alert('ChatLinksWorker shutting down');
await super.shutdown();
}
async expireChatMessages ( ) {
const { chat: chatService } = this.services;
await chatService.expireMessages();
}
}
(async ( ) => {
try {
const { fileURLToPath } = await import('node:url');
const __dirname = dirname(fileURLToPath(import.meta.url)); // jshint ignore:line
const worker = new ChatProcessorService(path.resolve(__dirname, '..', '..'));
await worker.start();
} catch (error) {
console.error('failed to start chat processing worker', { error });
process.exit(-1);
}
})();