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 ab09cbc commit 649022a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/baseentity.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BaseEntity {
this.connection = connection;
this.apiToClientNames = {};
this.clientToApiNames = {};
this.lastRefreshTime = 0;
this.extra = {};
for(let i in properties) {
let backend, client;
Expand Down Expand Up @@ -59,9 +60,19 @@ class BaseEntity {
this[this.apiToClientNames[name]] = metadata[name];
}
}
this.lastRefreshTime = Date.now();
return this;
}

/**
* Returns the age of the data in seconds.
*
* @returns {integer}
*/
getDataAge() {
return (Date.now() - this.lastRefreshTime) / 1000;
}

/**
* Returns all data in the model.
*
Expand Down
10 changes: 6 additions & 4 deletions src/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ 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. It is also executed once at the beginning.
* On every status change observed or on new log entries (if supported by the back-end), the callback is executed.
* It is also executed once at the beginning if the data is older than the time specified in the interval.
* The callback receives the updated 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 = null;
let lastStatus = this.status;
let intervalId = null;
let logIterator = null;
if (capabilities.hasFeature('debugJob')) {
Expand All @@ -135,7 +135,9 @@ class Job extends BaseEntity {
stopFn();
}
};
setTimeout(monitorFn, 0);
if (this.getDataAge() > interval) {
setTimeout(monitorFn, 0);
}
intervalId = setInterval(monitorFn, interval * 1000);
let stopFn = () => {
if (intervalId) {
Expand Down
10 changes: 6 additions & 4 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ 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. It is also executed once at the beginning.
* On every status change (enabled/disabled) observed or on new log entries (if supported by the back-end), the callback is executed.
* It is also executed once at the beginning if the data is older than the time specified in the interval.
* The callback receives the updated 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 = null;
let wasEnabled = this.enabled;
let intervalId = null;
let logIterator = null;
if (capabilities.hasFeature('debugService')) {
Expand All @@ -117,7 +117,9 @@ class Service extends BaseEntity {
}
wasEnabled = this.enabled;
};
setTimeout(monitorFn, 0);
if (this.getDataAge() > interval) {
setTimeout(monitorFn, 0);
}
intervalId = setInterval(monitorFn, interval * 1000);
let stopFn = () => {
if (intervalId) {
Expand Down

0 comments on commit 649022a

Please sign in to comment.