Skip to content

Commit

Permalink
Run monitorFn if data is outdated #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 fa31c47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 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
8 changes: 5 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. It is also executed once at the beginning.
* 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 All @@ -118,14 +118,16 @@ 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')) {
logIterator = this.debugJob();
}
let monitorFn = async () => {
await this.describeJob();
if (this.getDataAge() >= interval-1) {
await this.describeJob();
}
let logs = logIterator ? await logIterator.nextLogs() : [];
if (lastStatus !== this.status || logs.length > 0) {
callback(this, logs);
Expand Down
8 changes: 5 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. It is also executed once at the beginning.
* 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.
Expand All @@ -103,14 +103,16 @@ 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')) {
logIterator = this.debugService();
}
let monitorFn = async () => {
await this.describeService();
if (this.getDataAge() >= interval-1) {
await this.describeService();
}
let logs = logIterator ? await logIterator.nextLogs() : [];
if (wasEnabled !== this.enabled || logs.length > 0) {
callback(this, logs);
Expand Down

0 comments on commit fa31c47

Please sign in to comment.