Skip to content

Commit

Permalink
regression: prevent unhandled exception from apps statistics (#32701)
Browse files Browse the repository at this point in the history
* regression: prevent unhandled exception from apps statistics

* Update Apps-Engine with better error logs

---------

Co-authored-by: Douglas Gubert <douglas.gubert@gmail.com>
  • Loading branch information
sampaiodiego and d-gubert authored Jun 28, 2024
1 parent c6add10 commit a9f8c8e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
61 changes: 38 additions & 23 deletions apps/meteor/app/statistics/server/lib/getAppsStatistics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Apps } from '@rocket.chat/apps';
import { AppStatus, AppStatusUtils } from '@rocket.chat/apps-engine/definition/AppStatus';
import mem from 'mem';

import { SystemLogger } from '../../../../server/lib/logger/system';
import { Info } from '../../../utils/rocketchat.info';

export type AppsStatistics = {
Expand All @@ -10,7 +12,7 @@ export type AppsStatistics = {
totalFailed: number | false;
};

export async function getAppsStatistics(): Promise<AppsStatistics> {
async function _getAppsStatistics(): Promise<AppsStatistics> {
if (!Apps.self?.isInitialized()) {
return {
engineVersion: Info.marketplaceApiVersion,
Expand All @@ -20,32 +22,45 @@ export async function getAppsStatistics(): Promise<AppsStatistics> {
};
}

const apps = await Apps.getManager().get();
try {
const apps = await Apps.getManager().get();

let totalInstalled = 0;
let totalActive = 0;
let totalFailed = 0;
let totalInstalled = 0;
let totalActive = 0;
let totalFailed = 0;

await Promise.all(
apps.map(async (app) => {
totalInstalled++;
await Promise.all(
apps.map(async (app) => {
totalInstalled++;

const status = await app.getStatus();
const status = await app.getStatus();

if (status === AppStatus.MANUALLY_DISABLED) {
totalFailed++;
}
if (status === AppStatus.MANUALLY_DISABLED) {
totalFailed++;
}

if (AppStatusUtils.isEnabled(status)) {
totalActive++;
}
}),
);
if (AppStatusUtils.isEnabled(status)) {
totalActive++;
}
}),
);

return {
engineVersion: Info.marketplaceApiVersion,
totalInstalled,
totalActive,
totalFailed,
};
return {
engineVersion: Info.marketplaceApiVersion,
totalInstalled,
totalActive,
totalFailed,
};
} catch (err: unknown) {
SystemLogger.error({ msg: 'Exception while getting Apps statistics', err });
return {
engineVersion: Info.marketplaceApiVersion,
totalInstalled: false,
totalActive: false,
totalFailed: false,
};
}
}

// since this function is called every 5s by `setPrometheusData` we're memoizing the result since the result won't change that often
export const getAppsStatistics = mem(_getAppsStatistics, { maxAge: 60000 });
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8509,8 +8509,8 @@ __metadata:
linkType: soft

"@rocket.chat/apps-engine@npm:alpha":
version: 1.43.0-alpha.783
resolution: "@rocket.chat/apps-engine@npm:1.43.0-alpha.783"
version: 1.43.0-alpha.784
resolution: "@rocket.chat/apps-engine@npm:1.43.0-alpha.784"
dependencies:
"@msgpack/msgpack": 3.0.0-beta2
adm-zip: ^0.5.9
Expand All @@ -8526,7 +8526,7 @@ __metadata:
uuid: ~8.3.2
peerDependencies:
"@rocket.chat/ui-kit": "*"
checksum: f580ff051914e1c7866fcf42c46844641cd4ab8d09b35a2df8c6a1369d535d060d9b7450eb64e6c8c5c2ac716faf5bf8082b52c9856496b4a41a29d96a781575
checksum: b86ec5cf809dc06658be0ae24b72b746f373bf83e7ee381e4fe83d7a5f4754359004cdf4d0f89723861bf4a3c7841d5f78bbe280ee863e2c833e64f73f16b124
languageName: node
linkType: hard

Expand Down

0 comments on commit a9f8c8e

Please sign in to comment.