Skip to content

Commit

Permalink
Fix #55 notify auditlog after 3 min inactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
boly38 committed Jun 9, 2024
1 parent 070fad2 commit 5457602
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/config/ApplicationConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Plantnet from "../plugins/Plantnet.js";
import AskPlantnet from "../plugins/AskPlantnet.js";
import UnMute from "../plugins/UnMute.js";
import Summary from "../plugins/Summary.js";
import InactivityDetector from "../services/InactivityDetector.js";

export default class ApplicationConfig {
constructor() {
Expand Down Expand Up @@ -49,6 +50,10 @@ export default class ApplicationConfig {
.addArgument(container.get('config'))
.addArgument(container.get('loggerService'));

container.register('inactivityDetector', InactivityDetector)
.addArgument(container.get('config'))
.addArgument(container.get('loggerService'));

container.register('plantnetCommonService', PlantnetCommonService)
.addArgument(container.get('loggerService'))
.addArgument(container.get('auditLogsService'))
Expand Down Expand Up @@ -128,7 +133,8 @@ export default class ApplicationConfig {
blueskyService: container.get('blueskyService'),
newsService: container.get('newsService'),
auditLogsService: container.get('auditLogsService'),
summaryService: container.get('summaryService')
summaryService: container.get('summaryService'),
inactivityDetector: container.get('inactivityDetector')
});

const expressServer = container.get('expressServer');
Expand Down
15 changes: 14 additions & 1 deletion src/services/ExpressServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {unauthorized} from "../lib/CommonApi.js";
import {generateErrorId} from "../lib/Common.js";
import {StatusCodes} from "http-status-codes";
import ApplicationConfig from "../config/ApplicationConfig.js";

const __dirname = path.resolve();
const wwwPath = path.join(__dirname, './src/www');
Expand All @@ -23,14 +24,15 @@ export default class ExpressServer {
const {
config, loggerService, blueskyService,
botService, newsService,
auditLogsService, summaryService
auditLogsService, summaryService, inactivityDetector
} = services;
this.config = config;
this.blueskyService = blueskyService;
this.botService = botService;
this.newsService = newsService;
this.auditLogsService = auditLogsService;
this.summaryService = summaryService;
this.inactivityDetector = inactivityDetector;

this.logger = loggerService.getLogger().child({label: 'ExpressServer'});

Expand All @@ -48,6 +50,7 @@ export default class ExpressServer {
expressServer.app = express();

expressServer.app.use(express.static(path.join(wwwPath, './public')));
expressServer.app.use(expressServer.handleActivityTic.bind(this));
expressServer.app.set('views', path.join(wwwPath, './views'));
expressServer.app.set('view engine', 'ejs');
expressServer.app.get('/api/about', expressServer.aboutResponse.bind(this));
Expand All @@ -60,6 +63,11 @@ export default class ExpressServer {
// build initial cache
await this.summaryService.cacheGetWeekSummary({})

// register inactivity listener
this.inactivityDetector.registerOnInactivityListener(
async ()=>{await ApplicationConfig.sendAuditLogs();}
)

expressServer.listeningServer = await expressServer.app.listen(expressServer.port);
expressServer.logger.info(`Bot ${expressServer.version} listening on ${expressServer.port} with health on ${HEALTH_ENDPOINT}`);
return expressServer.listeningServer;
Expand All @@ -72,6 +80,11 @@ export default class ExpressServer {
|| "???";
}

handleActivityTic(req, res, next) {
this.inactivityDetector.activityTic();
next();
}

aboutResponse(req, res) {
const {version} = this;
res.json({version});
Expand Down
36 changes: 36 additions & 0 deletions src/services/InactivityDetector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* handle app inactivity trigger
*/
export default class InactivityDetector {
constructor(config, loggerService) {
this.config = config;
this.logger = loggerService.getLogger().child({label: 'InactivityDetector'});
this.INACTIVITY_DELAY_MIN = 3;
}
onInactivity() {
InactivityDetector.timer = null;
for (const handler of InactivityDetector.onInactivityListeners) {
try {
handler();
} catch (exception) {
this.logger.warn(`onInactivity handler error : ${exception.message}`);
}
}
}

activityTic() {
const {onInactivity, INACTIVITY_DELAY_MIN, logger} = this;
if (InactivityDetector.timer !== null) {
clearTimeout(InactivityDetector.timer);
}
if (InactivityDetector.onInactivityListeners.length > 0){
logger.debug(`setTimeout(${INACTIVITY_DELAY_MIN} min, ...) with ${InactivityDetector.onInactivityListeners.length} listeners`)
InactivityDetector.timer = setTimeout(onInactivity.bind(this), INACTIVITY_DELAY_MIN*60000);
}
}
registerOnInactivityListener(listener) {
InactivityDetector.onInactivityListeners.push(listener)
}
}
InactivityDetector.timer = null;
InactivityDetector.onInactivityListeners = [];

0 comments on commit 5457602

Please sign in to comment.