Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using env variables in the @Cron handle #1510

Closed
2 of 4 tasks
Mortefal opened this issue Nov 28, 2023 · 2 comments
Closed
2 of 4 tasks

Using env variables in the @Cron handle #1510

Mortefal opened this issue Nov 28, 2023 · 2 comments
Labels
bug Something isn't working needs triage

Comments

@Mortefal
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using the @Cron function to create a scheduled job, it fails to read variables from env. I

Minimum reproduction code

https://github.com/Mortefal/cron-proof

Steps to reproduce

  1. npm i
  2. create .env file and add CRON_TEST_STRING with a valid cron value. eg. */10 * * * * *
  3. npm run start
  4. expect this error:

node_modules\cron\dist\time.js:360 source = source.toLowerCase(); ^ TypeError: Cannot read properties of undefined (reading 'toLowerCase')

Where i would expect it to register both cron jobs in app.service.ts.

Expected behavior

Where i would expect it to register both cron jobs in app.service.ts.

Seems like the @Cron function runs without access to the process.env

Package version

^4.0.0

NestJS version

^10.0.0

Node.js version

v18.12.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@Mortefal Mortefal added bug Something isn't working needs triage labels Nov 28, 2023
@sheerlox
Copy link
Contributor

I'll let the Nest maintainers comment on using environment variables in the Cron decorator. However, this may be due to the AppService decorators being loaded before the ConfigModule loads the .env file.

Here's a workaround using the dynamic scheduler API in the meantime:

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Cron, CronExpression, SchedulerRegistry } from '@nestjs/schedule';
import { CronJob } from 'cron';

@Injectable()
export class AppService {
  constructor(
    private configService: ConfigService,
    private schedulerRegistry: SchedulerRegistry,
  ) {
    this.schedulerRegistry.addCronJob(
      'getFoo',
      CronJob.from({
        cronTime: this.configService.get('CRON_TEST_STRING'),
        start: true,
        onTick: () => {
          this.getFoo();
        },
      }),
    );
  }

  @Cron(CronExpression.EVERY_10_SECONDS)
  getHello(): string {
    console.log('Hello World!');
    return 'Hello World!';
  }

  getFoo(): string {
    console.log('Foo');
    return 'Foo';
  }
}

@kamilmysliwiec
Copy link
Member

I'll let the Nest maintainers comment on using environment variables in the Cron decorator. However, this may be due to the AppService decorators being loaded before the ConfigModule loads the .env file.

Correct.

For this, you should use the dynamic cron jobs API https://docs.nestjs.com/techniques/task-scheduling#dynamic-cron-jobs

@nestjs nestjs locked and limited conversation to collaborators Nov 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

3 participants