Skip to content

Commit

Permalink
🔀 Merge pull request #489
Browse files Browse the repository at this point in the history
🚧 add failsafe on updateOldPrices method - @idinium96
  • Loading branch information
idinium96 authored Mar 25, 2021
2 parents 0a7eca9 + 915c3e8 commit 0dbecdf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/classes/MyHandler/MyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ export default class MyHandler extends Handler {
this.refreshTimeout = setTimeout(() => {
this.enableAutoRefreshListings();
}, 5 * 60 * 1000);

// Send notification to admin/Discord Webhook if there's any item failed to go through updateOldPrices
const failedToUpdateOldPrices = this.bot.pricelist.failedUpdateOldPrices;

if (failedToUpdateOldPrices.length > 0) {
const dw = this.opt.discordWebhook.sendAlert;
const isDwEnabled = dw.enable && dw.url !== '';

if (this.opt.sendAlert.enable && this.opt.sendAlert.failedToUpdateOldPrices) {
if (isDwEnabled) {
sendAlert('failedToUpdateOldPrices', this.bot, '', null, null, failedToUpdateOldPrices);
} else {
this.bot.messageAdmins(
`Failed to update old prices (probably because autoprice is set to true but item does not exist` +
` on the pricer source):\n\n${failedToUpdateOldPrices.join(
'\n'
)}\n\nAll items above has been temporarily disabled.`,
[]
);
}
}

this.bot.pricelist.resetFailedUpdateOldPrices = 0;
}
}

onShutdown(): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion src/classes/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export const DEFAULTS = {
onSuccessUpdatePartialPriced: true,
onFailedUpdatePartialPriced: true
},
receivedUnusualNotInPricelist: true
receivedUnusualNotInPricelist: true,
failedToUpdateOldPrices: true
},

pricelist: {
Expand Down Expand Up @@ -1034,6 +1035,7 @@ interface SendAlert extends OnlyEnable {
unableToProcessOffer?: boolean;
partialPrice?: PartialPrice;
receivedUnusualNotInPricelist?: boolean;
failedToUpdateOldPrices?: boolean;
}

interface PartialPrice {
Expand Down
22 changes: 21 additions & 1 deletion src/classes/Pricelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ export default class Pricelist extends EventEmitter {

private retryGetKeyPrices: NodeJS.Timeout;

failedUpdateOldPrices: string[] = [];

set resetFailedUpdateOldPrices(value: number) {
this.failedUpdateOldPrices.length = value;
}

constructor(
private readonly priceSource: Pricer,
private readonly schema: SchemaManager.Schema,
Expand Down Expand Up @@ -767,7 +773,21 @@ export default class Pricelist extends EventEmitter {
const item = SKU.fromString(sku);

// Go through pricestf/custom pricer prices
const grouped = groupedPrices[item.quality][item.killstreak];
let grouped: Item[];
try {
grouped = groupedPrices[item.quality][item.killstreak];
} catch (err) {
if (currPrice.group !== 'failed-updateOldPrices') {
currPrice.enabled = false;
currPrice.group = 'failed-updateOldPrices';
this.failedUpdateOldPrices.push(sku);
log.warn(`updateOldPrices failed for ${sku}`, err);
pricesChanged = true;
}

continue;
}

const groupedCount = grouped.length;

for (let j = 0; j < groupedCount; j++) {
Expand Down
9 changes: 8 additions & 1 deletion src/lib/DiscordWebhook/sendAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type AlertType =
| 'autoUpdatePartialPriceSuccess'
| 'autoUpdatePartialPriceFailed'
| 'isPartialPriced'
| 'unusualInvalidItems';
| 'unusualInvalidItems'
| 'failedToUpdateOldPrices';

export default function sendAlert(
type: AlertType,
Expand Down Expand Up @@ -192,6 +193,12 @@ export default function sendAlert(
title = 'Partial price update';
description = msg;
color = '16776960'; // yellow
} else if (type === 'failedToUpdateOldPrices') {
title = 'Failed to update old prices';
description =
`Failed to update old prices (probably because autoprice is set to true but item does not exist` +
` on the pricer source):\n\n${items.join('\n')}\n\nAll items above has been temporarily disabled.`;
color = '16711680'; // red
} else {
title = 'High Valued Items';
description = `Someone is trying to take your **${items.join(', ')}** that is not in your pricelist.`;
Expand Down
3 changes: 3 additions & 0 deletions src/schemas/options-json/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ export const optionsSchema: jsonschema.Schema = {
},
receivedUnusualNotInPricelist: {
type: 'boolean'
},
failedToUpdateOldPrices: {
type: 'boolean'
}
},
required: [
Expand Down

0 comments on commit 0dbecdf

Please sign in to comment.