schedule icon indicating copy to clipboard operation
schedule copied to clipboard

TypeError: date.format is not a function

Open mudlee opened this issue 2 years ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

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 [email protected]` 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?

  • [ ] macOS
  • [ ] Windows
  • [X] Linux

Other

No response

mudlee avatar Sep 27 '22 09:09 mudlee

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

kamilmysliwiec avatar Sep 27 '22 12:09 kamilmysliwiec

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 [email protected] to update! npm notice

Techevan avatar Oct 16 '22 10:10 Techevan

@Techevan please provide some project that reproduces that issue.

micalevisk avatar Oct 16 '22 13:10 micalevisk

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

cc @kamilmysliwiec

crypto-jianyong avatar Oct 17 '22 07:10 crypto-jianyong

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

kamilmysliwiec avatar Oct 24 '22 09:10 kamilmysliwiec