// samples/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'));

class SampleWorker extends SiteWorker {

  constructor (dtp) {
    super(dtp, dtp.config.component);
  }

  async start ( ) {
    await super.start();

    await this.loadProcessor(path.join(__dirname, 'your-worker', 'cron', 'expire-things.js'));
    await this.loadProcessor(path.join(__dirname, 'your-worker', 'job', 'process-things.js'));

    await this.startProcessors();
  }

  async stop ( ) {
    await super.stop();
  }
}

(async ( ) => {
  try {
    module.rootPath = path.resolve(__dirname, '..', '..');
    module.pkg = require(path.resolve(__dirname, '..', '..', 'package.json'));
    module.component = { name: 'theWorkerName', slug: 'the-worker-name' };

    module.config = {
      environment: process.env.NODE_ENV,
      root: module.rootPath,
      component: module.component,
    };
    
    module.config.site = require(path.join(module.rootPath, 'config', 'site'));
    module.log = new SiteLog(module, module.component);

    module.worker = new SampleWorker(module);
    await module.worker.start();

    module.log.info(`${module.pkg.name} v${module.pkg.version} ${module.component.name} started`);
  } catch (error) {
    module.log.error('failed to start worker', { component: module.component.name, error });
    process.exit(-1);
  }

})();