Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
Cómo programar trabajos cron quincenales usando el módulo @nestjs/schedule

Por quincenal me refiero a cada dos semanas.

Tengo un trabajo que me gustaría ejecutar cada dos semanas los sábados a las 4:30 p. m. y estoy usando el decorador @Cron de NestJs para especificar cuándo se ejecuta.

¿Existe una forma sencilla de especificar intervalos de dos semanas a partir de una fecha de inicio específica?

 import { Injectable, Logger } from '@nestjs/common'; import { Cron} from '@nestjs/schedule'; @Injectable() export class TasksService { private readonly logger = new Logger(TasksService.name); _called = 0; @Cron('* * * * * sat') handleBiWeekly(): void { this.scrapeContestData(); } scrapeContestData(): void { this._called += 1; } }

Estos dos casos de prueba deben pasar:

 import { INestApplication } from '@nestjs/common'; import { ScheduleModule } from '@nestjs/schedule'; import { Test } from '@nestjs/testing'; import { AppModule } from '../app.module'; import { TasksModule } from './tasks.module'; import { TasksService } from './tasks.service'; import * as sinon from 'sinon'; describe('TasksService', () => { let app: INestApplication; let clock: sinon.SinonFakeTimers; beforeEach(async () => { const module = await Test.createTestingModule({ imports: [ { module: AppModule, imports: [ScheduleModule.forRoot(), TasksModule], providers: [TasksService], }, ], }).compile(); app = module.createNestApplication(); }); describe('biweekly', () => { test('Feb 19 at 4:30 PM, scrapeContestData TO BE called', async () => { const service = app.get(TasksService); clock = sinon.useFakeTimers({ now: new Date('2022-02-19T16:30Z').valueOf(), }); await app.init(); clock.tick(3000); expect(new Date().getDay()).toBe(6); expect(service._called).toBe(3); }); test('a week after Feb 19 at 4:30PM, scrapeContestData to NOT BE called', async () => { const service = app.get(TasksService); clock = sinon.useFakeTimers({ now: new Date('2022-02-26T16:30Z').valueOf(), }); await app.init(); clock.tick(3000); expect(new Date().getDay()).toBe(6); expect(service._called).toBe(0); }); });
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!