Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regression: prevent unhandled exception from apps statistics #32701

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading