nestjs / schedule

Schedule module for Nest framework (node.js) ⏰
https://nestjs.com
MIT License
349 stars 77 forks source link

TypeError: date.format is not a function #1048

Closed mudlee closed 1 year ago

mudlee commented 1 year ago

Is there an existing issue for this?

Current behavior

We have a NestJS application which uses @nestjs/schedule, version 2.1.0. For deployment we use Docker images. For almost all of our deployments it works just fine. One of the client reported though, that on their onprem, the app stopped with the following error:

","stream":"stdout","time":"2022-09-08T09:37:15.496090995Z"} {"log":"/root/app/node_modules/cron/lib/time.js:271
","stream":"stderr","time":"2022-09-21T20:19:45.716126382Z"} {"log":"                           Time Zone: ${zone || '\"\"'} - Cron String: ${this} - UTC offset: ${date.format(
","stream":"stderr","time":"2022-09-21T20:19:45.716202505Z"} {"log":"                            ^
","stream":"stderr","time":"2022-09-21T20:19:45.71621206Z"} {"log":"
","stream":"stderr","time":"2022-09-21T20:19:45.716218529Z"} {"log":"TypeError: date.format is not a function
","stream":"stderr","time":"2022-09-21T20:19:45.716224284Z"} {"log":" at CT._getNextDateFrom (/root/app/node_modules/cron/lib/time.js:271:79)
","stream":"stderr","time":"2022-09-21T20:19:45.716230014Z"} {"log":" at CT.sendAt (/root/app/node_modules/cron/lib/time.js:185:17)
","stream":"stderr","time":"2022-09-21T20:19:45.716245614Z"} {"log":" at CT.getTimeout (/root/app/node_modules/cron/lib/time.js:202:29)
","stream":"stderr","time":"2022-09-21T20:19:45.716251935Z"} {"log":" at CJ.start (/root/app/node_modules/cron/lib/job.js:118:31)
","stream":"stderr","time":"2022-09-21T20:19:45.716257787Z"} {"log":" at Timeout.callbackWrapper [as _onTimeout] (/root/app/node_modules/cron/lib/job.js:171:11)
","stream":"stderr","time":"2022-09-21T20:19:45.716270449Z"} {"log":" at listOnTimeout (node:internal/timers:559:17)
","stream":"stderr","time":"2022-09-21T20:19:45.716276992Z"} {"log":" at processTimers (node:internal/timers:502:7)
","stream":"stderr","time":"2022-09-21T20:19:45.71628273Z"} {"log":"npm notice 
","stream":"stderr","time":"2022-09-21T20:19:45.793334096Z"} {"log":"npm notice New minor version of npm available! 8.15.0 -> 8.19.1
","stream":"stderr","time":"2022-09-21T20:19:45.79369313Z"} {"log":"npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.19.1>
","stream":"stderr","time":"2022-09-21T20:19:45.794002242Z"} {"log":"npm notice Run `npm install -g npm@8.19.1` to update!
","stream":"stderr","time":"2022-09-21T20:19:45.794012227Z"} {"log":"npm notice 
","stream":"stderr","time":"2022-09-21T20:19:45.794136382Z"}

We have multiple @Cron annotations in our codebase, but all looks the same:

@Cron(CronExpression.EVERY_5_SECONDS, { name: DEVICE_REGISTRATION_CRON_NAME })
async exampleCron(): Promise<void> {
  this.schedulerRegistry.getCronJob(DEVICE_REGISTRATION_CRON_NAME).stop();
  try {
    // ... code that may fail, and we must not run other iteration till it finishes
  } finally {
    this.schedulerRegistry.getCronJob(DEVICE_REGISTRATION_CRON_NAME).start();
  }
}

I searched for this error, and found some similar at https://github.com/kelektiv/node-cron/issues, but those are not same, but I would say, very similar, and relates to the same bug.

What can I do? What the NestJS team will do?

Minimum reproduction code

....

Steps to reproduce

No response

Expected behavior

I would expect even if the cron fails, it would not stop the whole application.

Package version

2.1.0

NestJS version

9.0.0

Node.js version

16

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 year ago

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

Techevan commented 1 year ago

Got the same issue. Wonder how to deal with it. (@nestjs/common version: 8.4.7, cron version: 2.0.0)

/app/node_modules/cron/lib/time.js:271 Time Zone: ${zone || '""'} - Cron String: ${this} - UTC offset: ${date.format( ^

TypeError: date.format is not a function at CT._getNextDateFrom (/app/node_modules/cron/lib/time.js:271:79) at CT.sendAt (/app/node_modules/cron/lib/time.js:185:17) at CT.getTimeout (/app/node_modules/cron/lib/time.js:202:29) at CJ.start (/app/node_modules/cron/lib/job.js:118:31) at Timeout.callbackWrapper [as _onTimeout] (/app/node_modules/cron/lib/job.js:171:11) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7) npm notice npm notice New minor version of npm available! 8.5.0 -> 8.19.2 npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.19.2 npm notice Run npm install -g npm@8.19.2 to update! npm notice

micalevisk commented 1 year ago

@Techevan please provide some project that reproduces that issue.

crypto-jianyong commented 1 year ago

@micalevisk https://stackblitz.com/edit/nestjs-typescript-starter-apu3jw?file=README.md You can see that toDate and format are undefined.

cc @kamilmysliwiec

kamilmysliwiec commented 1 year ago

Please report this issue in this repository https://github.com/kelektiv/node-cron