Skip to content

Commit

Permalink
Run monitorFn always on start #26 + bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Sep 3, 2020
1 parent 383b137 commit f39bac5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Job extends BaseEntity {
* Checks for status changes and new log entries every x seconds.
*
* On every status change observed or on new log entries (if supported by the back-end),
* the callback is executed.
* the callback is executed. It is also executed once at the beginning.
* The callback receives the job (this object) and the logs (array) passed.
*
* The monitoring stops once the job has finished, was canceled or errored out.
Expand All @@ -118,7 +118,7 @@ class Job extends BaseEntity {
throw new Error('Monitoring Jobs not supported by the back-end.');
}

let lastStatus = this.status;
let lastStatus = null;
let intervalId = null;
let logIterator = null;
if (capabilities.hasFeature('debugJob')) {
Expand All @@ -135,7 +135,8 @@ class Job extends BaseEntity {
stopFn();
}
};
intervalId = setTimeout(monitorFn, interval * 1000);
setTimeout(monitorFn, 0);
intervalId = setInterval(monitorFn, interval * 1000);
let stopFn = () => {
if (intervalId) {
clearInterval(intervalId);
Expand Down
7 changes: 4 additions & 3 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Service extends BaseEntity {
* Checks for new log entries every x seconds.
*
* On every status change (enabled/disabled) observed or on new log entries (if supported by the back-end),
* the callback is executed.
* the callback is executed. It is also executed once at the beginning.
* The callback receives the service (this object) and the logs (array) passed.
*
* Returns a function that can be called to stop monitoring the service manually.
Expand All @@ -103,7 +103,7 @@ class Service extends BaseEntity {
throw new Error('Monitoring Services not supported by the back-end.');
}

let wasEnabled = this.enabled;
let wasEnabled = null;
let intervalId = null;
let logIterator = null;
if (capabilities.hasFeature('debugService')) {
Expand All @@ -117,7 +117,8 @@ class Service extends BaseEntity {
}
wasEnabled = this.enabled;
};
intervalId = setTimeout(monitorFn, interval * 1000);
setTimeout(monitorFn, 0);
intervalId = setInterval(monitorFn, interval * 1000);
let stopFn = () => {
if (intervalId) {
clearInterval(intervalId);
Expand Down

0 comments on commit f39bac5

Please sign in to comment.