breejs / bree

Bree is a Node.js and JavaScript job task scheduler with worker threads, cron, Date, and human syntax. Built for @ladjs, @forwardemail, @spamscanner, @cabinjs.
https://jobscheduler.net
MIT License
2.96k stars 80 forks source link

Typescript diagnostic errors #200

Closed teemukarvinen closed 1 year ago

teemukarvinen commented 1 year ago

Describe the bug

Node.js version: v14.16.1

OS version: Ubuntu 20.04.4

Description: Using typescript with ts-node-dev seems to work but I get errors that do not help me to fix the issues:

Worker for job "weeklyAssignment" online undefined
There was an error while running a worker weeklyAssignment
{ diagnosticCodes: [ 2361, 2339, 2339 ] }
There was an error while running a worker weeklyAssignment
Error: Worker for job "weeklyAssignment" exited with code 1

Also using the @breejs/ts-worker package, not sure if this should be in its issues or here. Did try using ts-node with nodemon but get the same errors, so I believe it is not to do with ts-node-dev.

I think the error codes refer to https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json but how can I find out in what part of the code this error points to?

Actual behavior

Errors from TS that don't help fixing the code issues.

Expected behavior

Errors that indicate where in the code issue is so that it can be fixed.

Code to reproduce

bree.ts

import path from 'path';
import Bree from 'bree';
import jobs from './jobs';

Bree.extend(require('@breejs/ts-worker'));

// options in https://jobscheduler.net/#/?id=instance-options
const bree = new Bree({
  root: path.join(__dirname, 'jobs'),
  defaultExtension: process.env.TS_NODE_DEV ? 'ts' : 'js',
  jobs,
  errorHandler: (error, workerMetadata) => {
    // workerMetadata will be populated with extended worker information only if
    // Bree instance is initialized with parameter `workerMetadata: true`
    if (workerMetadata.threadId) {
      console.log(
        `There was an error while running a worker ${workerMetadata.name} with thread ID: ${workerMetadata.threadId}`
      );
    } else {
      console.log(`There was an error while running a worker ${workerMetadata.name}`);
    }

    console.error(error);
  },
});

export default bree;

jobs/index.ts

export default [
  {
    name: 'weeklyAssignment',
    interval: '20s', //'at 15:30 every Friday',
  },
  {
    name: 'monthlyAssignment',
    interval: 'on first day of month at 15:30',
  },
];

weeklyAssignment.ts

import { parentPort } from 'worker_threads';
import process from 'process';
import { Network } from 'database/models';
import { getAccountsForSummary, getAssignmentsForSummary } from 'api2/domains/assignments/functions';
import { sendTemplateNotification } from '..';
import config from 'lib/config';

(async () => {
  const networks = await Network.findAll();

  for (const network of networks) {
    const resipientIds = await getAccountsForSummary(network.id, 'weekly');
    const assignments = await getAssignmentsForSummary(network.id, 'weekly');

    const newAssignments = [];
    const activeAssignments = [];

    for (const assignment of assignments.new) {
      newAssignments.push({
        name: assignment.translations[0].name,
        url: `${config.hostname}/assignments/${assignment.id}`,
      });
    }

    for (const assignment of assignments.active) {
      activeAssignments.push({
        name: assignment.translations[0].name,
        url: `${config.hostname}/assignments/${assignment.id}`,
      });
    }

    const data = {
      newAssignments,
      activeAssignments,
    };

    await sendTemplateNotification('weekly-assignment-summary', data, resipientIds);
  }

  // signal to parent that the job is done
  if (parentPort) {
    parentPort.postMessage('done');
  } else {
    process.exit(0);
  }
})();

Checklist

shadowgate15 commented 1 year ago

You can always run the worker code on it's own, in order to debug. Bree simply passes on the error that is emitted by the worker.

teemukarvinen commented 1 year ago

This just does not work with my setup, so I'm going to use something else.