Skip to content

Commit

Permalink
[OGUI-1509] Update Task Counters following cache update (#2396)
Browse files Browse the repository at this point in the history
* adds a check for environments tasks and updates the counters 
* if counters are different than at last check, sends them via websocket
  • Loading branch information
graduta authored May 27, 2024
1 parent 5f31811 commit 64ed5a0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Control/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module.exports.setup = (http, ws) => {
aliecsReqHandler.setWs(ws);
aliecsReqHandler.workflowService = workflowService;

const envCache = new EnvCache(ctrlService);
const envCache = new EnvCache(ctrlService, envService);
envCache.setWs(ws);

const bkpService = new BookkeepingService(config.bookkeeping ?? {});
Expand Down
36 changes: 29 additions & 7 deletions Control/lib/control-core/EnvCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class EnvCache {

/**
* @param {object} ctrlService - Handle to Control service
* @param {EnvironmentService} environmentService - service to be used to retrieve information on environments
*/
constructor(ctrlService) {
constructor(ctrlService, environmentService) {
this.ctrlService = ctrlService;
this.cache = {};
this.timeout = 9000;
this.cacheEvictionTimeout = 5 * 60 * 1000;
this.cacheEvictionLast = new Date();
this.refreshInterval = setInterval(() => this.refresh(), this.timeout);
this._cacheRefreshRate = 6000;
this.refreshInterval = setInterval(() => this.refresh(), this._cacheRefreshRate);
this._environmentService = environmentService;
}

/**
Expand Down Expand Up @@ -81,16 +84,35 @@ class EnvCache {
try {
const deadline = Date.now() + this.timeout;
const envs = await this.ctrlService.executeCommandNoResponse('GetEnvironments', {}, {deadline});
if (!this._cacheInSync(envs)) {
this.cache = envs;
this.webSocket?.broadcast(new WebSocketMessage().setCommand('environments').setPayload(this.cache));
log.debug('Updated cache');

for (let [index, currentEnv] of envs.environments.entries()) {
try {
const environment = await this._environmentService.getEnvironment(currentEnv.id);
envs.environments[index] = environment;
this._updateCache(envs);
} catch (error) {
console.error(error);
}
}
this.cacheEvictionLast = new Date();
this._updateCache(envs);
} catch (error) {
log.debug(error);
}
this.evictCache();
}

/**
* Method to update cache if there are any changes
* @param {Object{Environments}} - environments' data that is to be stored in cache
* @return {void}
*/
_updateCache(envs) {
if (!this._cacheInSync(envs)) {
this.cache = envs;
this.webSocket?.broadcast(new WebSocketMessage().setCommand('environments').setPayload(this.cache));
log.debug('Updated cache');
}
this.cacheEvictionLast = new Date();
}
}
module.exports = EnvCache;
2 changes: 1 addition & 1 deletion Control/lib/services/Environment.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EnvironmentService {
* Parses the environment and prepares the information for GUI purposes
* @param {string} id - environment id as defined by AliECS Core
* @param {string} taskSource - Source of where to request tasks from: FLP, EPN, QC, TRG
* @return {EnvironmentDetails}
* @return {EnvironmentInfo}
* @throws {Error}
*/
async getEnvironment(id, taskSource) {
Expand Down
8 changes: 6 additions & 2 deletions Control/public/common/enums/TaskState.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const TaskState = Object.freeze({
});

/**
* List of possible states for a task sorted alphabetically with ERROR first and RUNNING second
* List of possible states for a task sorted alphabetically with ERROR first and RUNNING second and CONFIGURED third
* @return {Array<String>} list of task states
*/
export const TASK_STATES = Object.values(TaskState)
Expand All @@ -36,6 +36,10 @@ export const TASK_STATES = Object.values(TaskState)
return -1;
} else if (b === 'RUNNING') {
return 1;
} else if (a === 'CONFIGURED') {
return -1;
} else if (b === 'CONFIGURED') {
return 1;
} else {
return a.localeCompare(b);
}
Expand All @@ -44,7 +48,7 @@ export const TASK_STATES = Object.values(TaskState)
export const TaskStateClassAssociation = Object.freeze({
ERROR: '.danger',
RUNNING: '.success',
CONFIGURED: '.warning',
CONFIGURED: '.primary',
STANDBY: '',
DONE: '',
MIXED: '',
Expand Down

0 comments on commit 64ed5a0

Please sign in to comment.