-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OGUI-1412] Add new CacheService and Broadcast new calibration inform…
…ation (#2185) * adds a new cacheservice(in-memory) that is to be used by all the other components for updates * broadcast calibration run to all clients if information is new * udpates `RunController` to initially serve data from cache and if missing, to request from Bookkeeping
- Loading branch information
Showing
10 changed files
with
266 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @license | ||
* Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
* All rights not expressly granted are reserved. | ||
* | ||
* This software is distributed under the terms of the GNU General Public | ||
* License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
* | ||
* In applying this license CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
|
||
/** | ||
* Keys that are used to set/get information to/from the CacheService | ||
*/ | ||
const CacheKeys = Object.freeze({ | ||
CALIBRATION_RUNS_BY_DETECTOR: 'CALIBRATION_RUNS_BY_DETECTOR' | ||
}); | ||
|
||
exports.CacheKeys = CacheKeys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
* All rights not expressly granted are reserved. | ||
* | ||
* This software is distributed under the terms of the GNU General Public | ||
* License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
* | ||
* In applying this license CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
|
||
const {WebSocketMessage} = require('@aliceo2/web-ui'); | ||
|
||
/** | ||
* @class | ||
* BroadcastService class is to be used for building a websocket message and broadcasting it via web sockets | ||
*/ | ||
class BroadcastService { | ||
/** | ||
* @constructor | ||
* Constructor for initializing the service with AliceO2/websocket service instance to use | ||
* @param {WebSocket} wsService - which is to be used for broadcasting | ||
*/ | ||
constructor(wsService) { | ||
/** | ||
* @type {WebSocket} | ||
*/ | ||
this._wsService = wsService; | ||
} | ||
|
||
/** | ||
* Method to receive command and payload to build a WebSocket message and broadcast it to all listening clients | ||
* @param {String} command - command to be added to websocket message | ||
* @param {Object} payload - payload to be sent to the clients | ||
* @return {void} | ||
*/ | ||
broadcast(command, payload) { | ||
if (payload) { | ||
const message = new WebSocketMessage() | ||
.setCommand(command) | ||
.setPayload(payload); | ||
this._wsService?.broadcast(message); | ||
} | ||
} | ||
} | ||
|
||
module.exports = {BroadcastService}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @license | ||
* Copyright 2019-2020 CERN and copyright holders of ALICE O2. | ||
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. | ||
* All rights not expressly granted are reserved. | ||
* | ||
* This software is distributed under the terms of the GNU General Public | ||
* License v3 (GPL Version 3), copied verbatim in the file "COPYING". | ||
* | ||
* In applying this license CERN does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an Intergovernmental Organization | ||
* or submit itself to any jurisdiction. | ||
*/ | ||
|
||
const {Log} = require('@aliceo2/web-ui'); | ||
const {deepStrictEqual, AssertionError} = require('assert'); | ||
|
||
/** | ||
* @class | ||
* CacheService class is designed to store in-memory information and allow users to also broadcast new information to the all or registered clients. | ||
*/ | ||
class CacheService { | ||
/** | ||
* @constructor | ||
* Constructor for initializing the service with: | ||
* - empty maps for needed information | ||
* - optional service for broadcasting information | ||
* @param {BroadcastService} broadcastService - which is to be used for broadcasting | ||
*/ | ||
constructor(broadcastService) { | ||
|
||
/** | ||
* @type {Object<String, Object>} | ||
*/ | ||
this._memory = {}; | ||
|
||
/** | ||
* @type {BroadcastService} | ||
*/ | ||
this._broadcastService = broadcastService; | ||
|
||
this._logger = new Log(`${process.env.npm_config_log_label ?? 'cog'}/cache-service`); | ||
} | ||
|
||
/** | ||
* Method to receive a function for retrieval of information and a key under which the information should be updated | ||
* @param {String} key - key under which the information should be stored | ||
* @param {String} value - command to be used for broadcasting message | ||
* @param {Object} broadcastConfig - object containing broadcast information; if present information will be broadcasted | ||
* @return {void} | ||
*/ | ||
async updateByKeyAndBroadcast(key, value, {command} = {}) { | ||
if (value) { | ||
try { | ||
deepStrictEqual(value, this._memory[key]); | ||
} catch (error) { | ||
if (error instanceof AssertionError) { | ||
this._memory[key] = value; | ||
if (command) { | ||
this._broadcastService.broadcast(command, value); | ||
} | ||
} else { | ||
this._logger.debug(`Unable to update key ${key} due to ${error}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Getter for retrieving a copy of the information stored in-memory under a certain key | ||
* @param {key} - key under which information is stored | ||
* @return {Object} | ||
*/ | ||
getByKey(key) { | ||
if (this._memory[key]) { | ||
return JSON.parse(JSON.stringify(this._memory[key])); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
module.exports = {CacheService}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.