Skip to content

Commit

Permalink
Improve Tiltify API fallback, actually put Discord webhook in the cor…
Browse files Browse the repository at this point in the history
…rect spot
  • Loading branch information
zoton2 committed Feb 17, 2024
1 parent 1168350 commit e78e3af
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/extension/tracker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const eventConfig = nodecg().bundleConfig.event;
const config = nodecg().bundleConfig.tracker;
const { useTestData } = nodecg().bundleConfig;
let cookies: NeedleResponse['cookies'];
let tiltifyApiBackupInterval: NodeJS.Timeout | undefined;
let tiltifyApiBackupTimeout: NodeJS.Timeout | undefined;
const tiltifyApiBackupLength = 5 * 60 * 1000;

/**
Expand Down Expand Up @@ -55,20 +55,6 @@ async function updateDonationTotalFromAPI(init = false): Promise<void> {
if (init || donationTotal.value < total) {
nodecg().log.info('[Tracker] API donation total changed: $%s', total);
donationTotal.value = total;
// If we checked the donation total on an interval and it was different, the MQ
// messages may be failing. Using a Discord Webhook to notify someone for ease of use.
const webhookUrl = nodecg().bundleConfig.tiltify.errorDiscordWebhook;
const userId = nodecg().bundleConfig.tiltify.errorDiscordWebhookUserIdToPing;
if (!init && webhookUrl) {
await needle(
'post',
webhookUrl,
{
content: `${userId ? `<@${userId}> ` : ''}There may be an issue with the esa-layouts `
+ 'Tiltify integration with RabbitMQ messages!',
},
);
}
}
} catch (err) {
nodecg().log.warn('[Tracker] Error updating donation total from API');
Expand All @@ -91,11 +77,31 @@ async function updateDonationTotalFromAPITiltify(init = false): Promise<void> {
if (init || donationTotal.value < total) {
nodecg().log.info('[Tracker] API donation total changed: $%s', total);
donationTotal.value = total;
// If we checked the donation total on an interval and it was different, the MQ
// messages may be failing. Using a Discord Webhook to notify someone for ease of use.
const webhookUrl = nodecg().bundleConfig.tiltify.errorDiscordWebhook;
const userId = nodecg().bundleConfig.tiltify.errorDiscordWebhookUserIdToPing;
if (!init && webhookUrl) {
try {
await needle(
'post',
webhookUrl,
{
content: `${userId ? `<@${userId}> ` : ''}There may be an issue with the esa-layouts `
+ 'Tiltify integration with RabbitMQ messages!',
},
);
nodecg().log.debug('[Tracker] Discord webhook sent');
} catch (err) {
nodecg().log.debug('[Tracker] Discord webhook failed:', err);
}
}
}
} catch (err) {
nodecg().log.warn('[Tracker] Error updating donation total from API');
nodecg().log.debug('[Tracker] Error updating donation total from API:', err);
}
tiltifyApiBackupTimeout = setTimeout(updateDonationTotalFromAPITiltify, tiltifyApiBackupLength);
}

// Triggered when a donation total is updated in our tracker.
Expand All @@ -104,9 +110,9 @@ mq.evt.on('donationTotalUpdated', (data) => {
let total = 0;
// HARDCODED FOR NOW!
if (data.event === 'esaw2024') {
clearInterval(tiltifyApiBackupInterval);
clearInterval(tiltifyApiBackupTimeout);
total += data.new_total;
tiltifyApiBackupInterval = setInterval(
tiltifyApiBackupTimeout = setTimeout(
updateDonationTotalFromAPITiltify,
tiltifyApiBackupLength,
);
Expand Down Expand Up @@ -237,7 +243,6 @@ if (config.enabled) {
setup();
} else {
// FOR TILTIFY USE!
// Get initial total from API and set an interval as a fallback.
// Get initial total from API (function also sets a timeout as a fallback).
updateDonationTotalFromAPITiltify(true);
tiltifyApiBackupInterval = setInterval(updateDonationTotalFromAPITiltify, tiltifyApiBackupLength);
}

0 comments on commit e78e3af

Please sign in to comment.