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

chore: improve stats cronjob scheduling #33311

Merged
merged 4 commits into from
Nov 18, 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
5 changes: 5 additions & 0 deletions .changeset/seven-otters-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Sends server statistics only once a day despite multiple instance being started at different times.
64 changes: 43 additions & 21 deletions apps/meteor/app/statistics/server/functions/sendUsageReport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IStats } from '@rocket.chat/core-typings';
import type { Logger } from '@rocket.chat/logger';
import { Statistics } from '@rocket.chat/models';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
Expand All @@ -7,32 +8,53 @@ import { Meteor } from 'meteor/meteor';
import { statistics } from '..';
import { getWorkspaceAccessToken } from '../../../cloud/server';

export async function sendUsageReport(logger: Logger): Promise<string | undefined> {
return tracerSpan('generateStatistics', {}, async () => {
const cronStatistics = await statistics.save();
async function sendStats(logger: Logger, cronStatistics: IStats): Promise<string | undefined> {
try {
const token = await getWorkspaceAccessToken();
const headers = { ...(token && { Authorization: `Bearer ${token}` }) };

try {
const token = await getWorkspaceAccessToken();
const headers = { ...(token && { Authorization: `Bearer ${token}` }) };
const response = await fetch('https://collector.rocket.chat/', {
method: 'POST',
body: {
...cronStatistics,
host: Meteor.absoluteUrl(),
},
headers,
});

const { statsToken } = await response.json();

if (statsToken != null) {
await Statistics.updateOne({ _id: cronStatistics._id }, { $set: { statsToken } });
return statsToken;
}
} catch (err) {
logger.error({ msg: 'Failed to send usage report', err });
}
}

const response = await fetch('https://collector.rocket.chat/', {
method: 'POST',
body: {
...cronStatistics,
host: Meteor.absoluteUrl(),
},
headers,
});
export async function sendUsageReport(logger: Logger): Promise<string | undefined> {
return tracerSpan('generateStatistics', {}, async () => {
const last = await Statistics.findLast();
if (last) {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

const { statsToken } = await response.json();
// if the last data we have has less than 24h and was not sent to yet, send it
if (last.createdAt > yesterday) {
// but if it has the confirmation token, we can skip
if (last.statsToken) {
return last.statsToken;
}

if (statsToken != null) {
await Statistics.updateOne({ _id: cronStatistics._id }, { $set: { statsToken } });
return statsToken;
// if it doesn't it means the request failed, so we try sending again with the same data
return sendStats(logger, last);
}
} catch (error) {
/* error*/
logger.warn('Failed to send usage report');
}

// if our latest stats has more than 24h, it is time to generate a new one and send it
const cronStatistics = await statistics.save();

return sendStats(logger, cronStatistics);
});
}
Loading