| 498 | } |
| 499 | |
| 500 | async start(name) { |
| 501 | debug('start', name); |
| 502 | |
| 503 | if (!this._init) await this.init(); |
| 504 | |
| 505 | if (name) { |
| 506 | const job = this.config.jobs.find((j) => j.name === name); |
| 507 | if (!job) { |
| 508 | throw new Error(`Job ${name} does not exist`); |
| 509 | } |
| 510 | |
| 511 | if ( |
| 512 | this.timeouts.has(name) || |
| 513 | this.intervals.has(name) || |
| 514 | this.workers.has(name) |
| 515 | ) { |
| 516 | throw new Error(`Job "${name}" is already started`); |
| 517 | } |
| 518 | |
| 519 | debug('job', job); |
| 520 | |
| 521 | // Check for date and if it is in the past then don't run it |
| 522 | if (job.date instanceof Date) { |
| 523 | debug('job date', job); |
| 524 | if (job.date.getTime() < Date.now()) { |
| 525 | debug('job date was in the past'); |
| 526 | // not throwing an error so that jobs can be set with a specifc date |
| 527 | // and only run on that date then never run again without changing config |
| 528 | this.config.logger.warn( |
| 529 | `Job "${name}" was skipped because it was in the past.` |
| 530 | ); |
| 531 | this.emit('job past', name); |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | this.timeouts.set( |
| 536 | name, |
| 537 | setTimeout(() => { |
| 538 | this.run(name); |
| 539 | if (this.isSchedule(job.interval)) { |
| 540 | debug('job.interval is schedule', job); |
| 541 | this.intervals.set( |
| 542 | name, |
| 543 | later.setInterval( |
| 544 | () => this.run(name), |
| 545 | job.interval, |
| 546 | job.timezone |
| 547 | ) |
| 548 | ); |
| 549 | } else if (Number.isFinite(job.interval) && job.interval > 0) { |
| 550 | debug('job.interval is finite', job); |
| 551 | this.intervals.set( |
| 552 | name, |
| 553 | setInterval(() => this.run(name), job.interval) |
| 554 | ); |
| 555 | } else { |
| 556 | debug('job.date was scheduled to run only once', job); |
| 557 | } |