Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hiro5id committed Jan 16, 2023
1 parent f7c73d5 commit 12891ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cron-job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ export class CronJob {
this.throwError(`End date cannot be before start date, start: ${this._jobOptions.startDate.toISOString()} end: ${this._jobOptions.endDate.toISOString()}`);
}

let execIterations = '';
if (this._jobOptions.numberOfScheduledIterationsToExecute) {
execIterations = ` for ${this._jobOptions.numberOfScheduledIterationsToExecute} iterations.`;
}
this._scheduleGenerator = new CronScheduleGenerator(cronSchedule, this._jobOptions.startDate!);
this.log('info', `Scheduled to execute: ${this._scheduleGenerator.englishDescriptionOfSchedule}`);
this.log('info', `Scheduled to execute: ${this._scheduleGenerator.englishDescriptionOfSchedule}${execIterations}`);

this._jobRunnerPromise = new Promise((resolve, reject) => {
this._resolveJobRunner = resolve;
Expand Down Expand Up @@ -135,6 +139,7 @@ export class CronJob {
const now = this.getNow();
try {
this.numberOfExecutedIterations += 1;
this.log('info', `Executing iteration #${this.numberOfExecutedIterations}`);
// bind the "this" context because it gets lost
const logger = this.log.bind(this);
const actionResult = this.jobWorkerFunction(now, logger);
Expand Down
13 changes: 13 additions & 0 deletions test/cron-scheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Hello this is a test 2010-01-01T00:01:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:02:00.000Z the time is now 2010-01-01T00:01:00.000Z.',
'Job [testjob123]: Executing iteration #2',
'Job [testjob123]: Hello this is a test 2010-01-01T00:02:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:03:00.000Z the time is now 2010-01-01T00:02:00.000Z.',
'Job [testjob123]: Executing iteration #3',
'Job [testjob123]: Hello this is a test 2010-01-01T00:03:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: End date reached, resolving job promise...',
Expand Down Expand Up @@ -225,12 +228,15 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Hello this is a test 2010-01-01T00:01:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:02:00.000Z the time is now 2010-01-01T00:01:00.000Z.',
'Job [testjob123]: Executing iteration #2',
'Job [testjob123]: Hello this is a test 2010-01-01T00:02:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:03:00.000Z the time is now 2010-01-01T00:02:00.000Z.',
'Job [testjob123]: Executing iteration #3',
'Job [testjob123]: Hello this is a test 2010-01-01T00:03:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: End date reached, resolving job promise...',
Expand Down Expand Up @@ -342,6 +348,7 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Hello this is a test 2010-01-01T00:01:00.000Z',
'Job [testjob123]: Scheduled did not finish!',
]);
Expand Down Expand Up @@ -401,6 +408,7 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Scheduled did not finish!',
]);

Expand Down Expand Up @@ -461,6 +469,7 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Scheduled did not finish!',
]);

Expand Down Expand Up @@ -521,6 +530,7 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Scheduled did not finish!',
]);

Expand Down Expand Up @@ -591,12 +601,15 @@ describe('chron scheduler', function () {
expect(consoleMessages).eql([
'Job [testjob123]: Scheduled to execute: At every minute',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:01:00.000Z the time is now 2010-01-01T00:00:00.000Z.',
'Job [testjob123]: Executing iteration #1',
'Job [testjob123]: Hello this is a test 2010-01-01T00:01:00.000Z',
'Job [testjob123]: Scheduled did not finish!',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:02:00.000Z the time is now 2010-01-01T00:01:00.000Z.',
'Job [testjob123]: Executing iteration #2',
'Job [testjob123]: Hello this is a test 2010-01-01T00:02:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: Scheduling to trigger in the next 60000 ms, at 2010-01-01T00:03:00.000Z the time is now 2010-01-01T00:02:00.000Z.',
'Job [testjob123]: Executing iteration #3',
'Job [testjob123]: Hello this is a test 2010-01-01T00:03:00.000Z',
'Job [testjob123]: Scheduled trigger finished!',
'Job [testjob123]: End date reached, resolving job promise...',
Expand Down

0 comments on commit 12891ec

Please sign in to comment.