2020-04-10 01:01:46 +08:00
|
|
|
import ConnectionStatus from '/imports/api/connection-status';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import { check } from 'meteor/check';
|
2022-02-02 21:53:23 +08:00
|
|
|
import changeHasConnectionStatus from '/imports/api/users-persistent-data/server/modifiers/changeHasConnectionStatus';
|
2020-04-10 01:01:46 +08:00
|
|
|
|
2023-02-15 01:51:31 +08:00
|
|
|
const STATS = Meteor.settings.public.stats;
|
|
|
|
const STATS_INTERVAL = STATS.interval;
|
|
|
|
const STATS_CRITICAL_RTT = STATS.rtt[STATS.rtt.length - 1];
|
|
|
|
|
2023-03-10 20:06:13 +08:00
|
|
|
export default async function updateConnectionStatus(meetingId, userId, status) {
|
2020-04-10 01:01:46 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(userId, String);
|
|
|
|
|
2022-09-15 09:16:27 +08:00
|
|
|
const now = new Date().getTime();
|
2020-04-10 01:01:46 +08:00
|
|
|
|
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
};
|
|
|
|
|
|
|
|
const modifier = {
|
2020-05-14 21:35:25 +08:00
|
|
|
meetingId,
|
|
|
|
userId,
|
2022-09-15 09:16:27 +08:00
|
|
|
connectionAliveAt: now,
|
2023-02-15 01:51:31 +08:00
|
|
|
clientNotResponding: false,
|
2020-04-10 01:01:46 +08:00
|
|
|
};
|
|
|
|
|
2022-09-15 09:16:27 +08:00
|
|
|
// Store last not-normal status
|
|
|
|
if (status !== 'normal') {
|
|
|
|
modifier.status = status;
|
|
|
|
modifier.statusUpdatedAt = now;
|
|
|
|
}
|
2020-04-10 01:01:46 +08:00
|
|
|
|
2022-09-15 09:16:27 +08:00
|
|
|
try {
|
2023-03-10 20:06:13 +08:00
|
|
|
const { numberAffected } = await ConnectionStatus.upsertAsync(selector, { $set: modifier });
|
2022-09-15 09:16:27 +08:00
|
|
|
if (numberAffected && status !== 'normal') {
|
2023-03-10 20:06:13 +08:00
|
|
|
await changeHasConnectionStatus(true, userId, meetingId);
|
2022-09-15 09:16:27 +08:00
|
|
|
Logger.verbose(`Updated connection status meetingId=${meetingId} userId=${userId} status=${status}`);
|
2020-05-14 21:35:25 +08:00
|
|
|
}
|
2023-02-15 01:51:31 +08:00
|
|
|
|
2023-03-10 20:06:13 +08:00
|
|
|
Meteor.setTimeout(async () => {
|
|
|
|
const connectionLossTimeThreshold = new Date()
|
|
|
|
.getTime() - (STATS_INTERVAL + STATS_CRITICAL_RTT);
|
2023-02-15 01:51:31 +08:00
|
|
|
|
|
|
|
const selectorNotResponding = {
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
connectionAliveAt: { $lte: connectionLossTimeThreshold },
|
|
|
|
clientNotResponding: false,
|
|
|
|
};
|
|
|
|
|
2023-03-10 20:06:13 +08:00
|
|
|
const numberAffectedNotResponding = await ConnectionStatus
|
|
|
|
.updateAsync(selectorNotResponding, {
|
|
|
|
$set: { clientNotResponding: true },
|
|
|
|
});
|
2023-02-15 01:51:31 +08:00
|
|
|
|
|
|
|
if (numberAffectedNotResponding) {
|
|
|
|
Logger.info(`Updated clientNotResponding=true meetingId=${meetingId} userId=${userId}`);
|
|
|
|
}
|
|
|
|
}, STATS_INTERVAL + STATS_CRITICAL_RTT);
|
2021-03-26 05:22:50 +08:00
|
|
|
} catch (err) {
|
2021-04-27 05:19:17 +08:00
|
|
|
Logger.error(`Updating connection status meetingId=${meetingId} userId=${userId}: ${err}`);
|
2021-03-26 05:22:50 +08:00
|
|
|
}
|
2020-04-10 01:01:46 +08:00
|
|
|
}
|