Cannot register cron job "ClassName@methodName" because it is defined in a non static provider
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Current behavior
WARN [Scheduler] Cannot register cron job “ClassNameJob@methodName” because it is defined in a non static provider.
This class is throwing a WARN.
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
// other imports
@Injectable()
export class ClassNameJob {
constructor(private readonly xxxService: XxxService) {
}
@Cron(CronConstants.AT_EVERY_2TH_MINUTE)
async methodName(userId?: number) {
// ... code
}
}
ClassNameJob is imported into providers of app.module.ts
Minimum reproduction code
https://github.com/kuriel-trivu/nestjs-job-bug
Steps to reproduce
No response
Expected behavior
class to be registered into scheduler service
Package
- I don’t know. Or some 3rd-party package
-
@nestjs/common
-
@nestjs/core
-
@nestjs/microservices
-
@nestjs/platform-express
-
@nestjs/platform-fastify
-
@nestjs/platform-socket.io
-
@nestjs/platform-ws
-
@nestjs/testing
-
@nestjs/websockets
- Other (see below)
Other package
NestJS version
8.4.7
Packages versions
"@nestjs/common": "8.4.7",
"@nestjs/core": "8.4.7",
"@nestjs/jwt": "8.0.1",
"@nestjs/passport": "8.2.2",
"@nestjs/platform-express": "8.4.7",
"@nestjs/platform-socket.io": "8.4.7",
"@nestjs/schedule": "2.0.1",
"@nestjs/serve-static": "2.2.2",
"@nestjs/websockets": "8.4.7",
"cache-manager": "^4.0.1",
"class-transformer": "0.4.0",
"class-validator": "0.13.1",
"crypto": "1.0.1",
"dotenv": "6.2.0",
"express-useragent": "^1.0.15",
"faker": "5.5.3",
"hbs": "4.1.2",
"i18n-js": "3.5.1",
"lodash": "4.17.21",
"moment-timezone": "0.5.27",
"passport": "0.4.1",
"passport-jwt": "4.0.0",
"passport-local": "1.0.0",
"pg": "8.7.1",
"qs": "6.10.1",
"randomstring": "1.2.1",
"reflect-metadata": "0.1.13",
"rxjs": "7.4.0",
"sequelize": "6.6.2",
"sequelize-typescript": "2.1.0",
"sharp": "^0.30.6",
"socket.io": "4.2.0",
"socket.io-client": "2.0.4",
"typescript": "4.3.5"
Node.js version
v16.13.1
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
Issue Analytics
- State:
- Created a year ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
NestJS: Injected dependencies are undefined during cron job ...
It sounds like you have a dependency using the REQUEST scope, which cannot be instantiated during CRON as there is no request to...
Read more >Performing Recurrent Tasks - Hangfire Documentation
Recurring job registration is almost as simple as background job ... The Cron class contains different methods and overloads to run jobs on...
Read more >How to schedule a cron job with NestJs in less than 5 mins
I have developed a couple of apps with NestJs in the past and wanted to give it a try this time. The first...
Read more >All configuration options - Quarkus
AWS Lambda Type Default
AWS Lambda Common Type Default
AWS Lambda Gateway REST API Type Default
Agroal ‑ Database connection pool Type Default
Read more >Java.lang.Class class in Java | Set 1 - GeeksforGeeks
Please note that this method is used with class name, not with ... A member class is a class that is declared as...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I did the whole analysis and the reproduction is there. Not sure what you mean.
Reproduction: Decorate XxxService with Injectable decorator with scope: Scope.REQUEST.
BOOM: Error. Remove parameter scope: Scope.REQUEST. BOOM, No error. Can be done on any codebase. What more would you need?
The improvement is as simple as changing the error message, so that it suggests the dev to check his Injectable decorators for the dependencies injected into XxxService, instead of just bluntly throwing an error message without context: https://github.com/nestjs/schedule/blob/37b79bc227e50594d6f29e65d81dbf06928310f9/lib/schedule.explorer.ts#L90
That’s a free PR right there.
As Kamil said:
If it’s so easy to create, then please do so 😄