// worker-template.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 TemplateService extends SiteRuntime { static get name ( ) { return 'TemplateService'; } static get slug ( ) { return 'template'; } constructor (rootPath) { super(TemplateService, rootPath); } async start ( ) { await super.start(); const mongoose = await import('mongoose'); this.Link = mongoose.model('Link'); this.viewModel = { }; await this.populateViewModel(this.viewModel); /* * Cron jobs */ const cronJobSchedule = '*/5 * * * * *'; // Every 5 seconds this.cronJob = new CronJob( cronJobSchedule, this.cronJobProcessor.bind(this), null, true, CRON_TIMEZONE, ); /* * Bull Queue job processors */ this.log.info('registering queue job processor', { config: this.config.jobQueues.links }); this.linksProcessingQueue = this.services.jobQueue.getJobQueue('links', this.config.jobQueues.links); this.linksProcessingQueue.process('link-ingest', 1, this.ingestLink.bind(this)); } async shutdown ( ) { this.log.alert('ChatLinksWorker shutting down'); await super.shutdown(); } async cronJobProcessor ( ) { this.log.info('your cron job is running now'); } async ingestLink (job) { this.log.info('processing queue job', { id: job.id }); } } (async ( ) => { try { const { fileURLToPath } = await import('node:url'); const __dirname = dirname(fileURLToPath(import.meta.url)); // jshint ignore:line const worker = new TemplateService(path.resolve(__dirname, '..', '..')); await worker.start(); } catch (error) { console.error('failed to start template worker', { error }); process.exit(-1); } })();