// sample-worker.js // Copyright (C) 2022 DTP Technologies, LLC // License: Apache-2.0 'use strict'; const path = require('path'); require('dotenv').config({ path: path.resolve(__dirname, '..', '..', '.env') }); const { SiteLog, SiteWorker, } = require(path.join(__dirname, '..', '..', 'lib', 'site-lib')); const { CronJob } = require('cron'); module.rootPath = path.resolve(__dirname, '..', '..'); module.pkg = require(path.resolve(__dirname, '..', '..', 'package.json')); module.config = { environment: process.env.NODE_ENV, root: module.rootPath, component: { name: 'sampleWorker', slug: 'sample-worker' }, }; module.config.site = require(path.join(module.rootPath, 'config', 'site')); module.config.http = require(path.join(module.rootPath, 'config', 'http')); class SampleWorker extends SiteWorker { constructor (dtp) { super(dtp, { }); } async start ( ) { const CRON_TIMEZONE = 'America/New_York'; await super.start(); this.log.info('starting worker job'); this.job = new CronJob( '*/5 * * * * *', this.runJob.bind(this), null, true, CRON_TIMEZONE, ); const { jobQueue: jobQueueService } = this.dtp.services; this.sampleJobQueue = jobQueueService.getJobQueue('dtp-sample', this.dtp.config.jobQueues['dtp-sample']); this.sampleJobQueue.process('dtp-sample', 1, this.processDtpSample.bind(this)); } async stop ( ) { this.log.info('stopping sample worker job'); this.job.stop(); delete this.job; await super.stop(); } async runJob ( ) { this.log.alert('sample job starting'); /* * Your worker will do interesting things here */ this.log.alert('sample job ending'); } async processDtpSample (job) { this.log.info('received sample job', { id: job.id, data: job.data }); } } (async ( ) => { try { module.log = new SiteLog(module, module.config.component); module.worker = new SampleWorker(module); await module.worker.start(); module.log.info(`${module.pkg.name} v${module.pkg.version} ${module.config.component.name} started`); } catch (error) { module.log.error('failed to start worker', { component: module.config.component, error, }); process.exit(-1); } })();