Skip to content

Commit

Permalink
Run monitorFn always on start #26 (#28)
Browse files Browse the repository at this point in the history
* Run monitorFn on start #26 + bugfix
  • Loading branch information
m-mohr authored Sep 3, 2020
1 parent 383b137 commit bccc77d
Show file tree
Hide file tree
Showing 3 changed files with 25 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
11 changes: 7 additions & 4 deletions src/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,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.
* The callback receives the job (this object) and the logs (array) passed.
* the callback is executed. It may also be executed once at the beginning.
* 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 Down Expand Up @@ -125,7 +125,9 @@ class Job extends BaseEntity {
logIterator = this.debugJob();
}
let monitorFn = async () => {
await this.describeJob();
if (this.getDataAge() > 1) {
await this.describeJob();
}
let logs = logIterator ? await logIterator.nextLogs() : [];
if (lastStatus !== this.status || logs.length > 0) {
callback(this, logs);
Expand All @@ -135,7 +137,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
11 changes: 7 additions & 4 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,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.
* The callback receives the service (this object) and the logs (array) passed.
* the callback is executed. It may also be executed once at the beginning.
* 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.
* The monitoring must be stopped manually, otherwise it runs forever.
Expand Down Expand Up @@ -110,14 +110,17 @@ class Service extends BaseEntity {
logIterator = this.debugService();
}
let monitorFn = async () => {
await this.describeService();
if (this.getDataAge() > 1) {
await this.describeService();
}
let logs = logIterator ? await logIterator.nextLogs() : [];
if (wasEnabled !== this.enabled || logs.length > 0) {
callback(this, logs);
}
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 bccc77d

Please sign in to comment.