mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-21 00:35:02 +00:00
commit
07e4f53170
2 changed files with 55 additions and 40 deletions
|
@ -12,12 +12,15 @@ import { CounterTriggerState } from "./entities/CounterTriggerState";
|
||||||
import moment from "moment-timezone";
|
import moment from "moment-timezone";
|
||||||
import { DAYS, DBDateFormat, HOURS, MINUTES } from "../utils";
|
import { DAYS, DBDateFormat, HOURS, MINUTES } from "../utils";
|
||||||
import { connection } from "./db";
|
import { connection } from "./db";
|
||||||
|
import { Queue } from "../Queue";
|
||||||
|
|
||||||
const DELETE_UNUSED_COUNTERS_AFTER = 1 * DAYS;
|
const DELETE_UNUSED_COUNTERS_AFTER = 1 * DAYS;
|
||||||
const DELETE_UNUSED_COUNTER_TRIGGERS_AFTER = 1 * DAYS;
|
const DELETE_UNUSED_COUNTER_TRIGGERS_AFTER = 1 * DAYS;
|
||||||
|
|
||||||
const MAX_COUNTER_VALUE = 2147483647; // 2^31-1, for MySQL INT
|
const MAX_COUNTER_VALUE = 2147483647; // 2^31-1, for MySQL INT
|
||||||
|
|
||||||
|
const decayQueue = new Queue();
|
||||||
|
|
||||||
async function deleteCountersMarkedToBeDeleted(): Promise<void> {
|
async function deleteCountersMarkedToBeDeleted(): Promise<void> {
|
||||||
await getRepository(Counter)
|
await getRepository(Counter)
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
|
@ -158,7 +161,8 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async decay(id: number, decayPeriodMs: number, decayAmount: number) {
|
decay(id: number, decayPeriodMs: number, decayAmount: number) {
|
||||||
|
return decayQueue.add(async () => {
|
||||||
const counter = (await this.counters.findOne({
|
const counter = (await this.counters.findOne({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
|
@ -186,14 +190,16 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
? `GREATEST(value - ${decayAmountToApply}, 0)`
|
? `GREATEST(value - ${decayAmountToApply}, 0)`
|
||||||
: `LEAST(value + ${Math.abs(decayAmountToApply)}, ${MAX_COUNTER_VALUE})`;
|
: `LEAST(value + ${Math.abs(decayAmountToApply)}, ${MAX_COUNTER_VALUE})`;
|
||||||
|
|
||||||
await this.counterValues.update(
|
// Using an UPDATE with ORDER BY in an attempt to avoid deadlocks from simultaneous decays
|
||||||
{
|
// Also see https://dev.mysql.com/doc/refman/8.0/en/innodb-deadlocks-handling.html
|
||||||
counter_id: id,
|
await this.counterValues
|
||||||
},
|
.createQueryBuilder("CounterValue")
|
||||||
{
|
.where("counter_id = :id", { id })
|
||||||
|
.orderBy("id")
|
||||||
|
.update({
|
||||||
value: () => rawUpdate,
|
value: () => rawUpdate,
|
||||||
},
|
})
|
||||||
);
|
.execute();
|
||||||
|
|
||||||
await this.counters.update(
|
await this.counters.update(
|
||||||
{
|
{
|
||||||
|
@ -203,6 +209,7 @@ export class GuildCounters extends BaseGuildRepository {
|
||||||
last_decay_at: newLastDecayDate,
|
last_decay_at: newLastDecayDate,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async markUnusedTriggersToBeDeleted(triggerIdsToKeep: number[]) {
|
async markUnusedTriggersToBeDeleted(triggerIdsToKeep: number[]) {
|
||||||
|
|
|
@ -27,7 +27,15 @@ export const TRegex = new t.Type<RegExp, string>(
|
||||||
(s): s is RegExp => s instanceof RegExp,
|
(s): s is RegExp => s instanceof RegExp,
|
||||||
(from, to) =>
|
(from, to) =>
|
||||||
either.chain(t.string.validate(from, to), s => {
|
either.chain(t.string.validate(from, to), s => {
|
||||||
|
try {
|
||||||
return t.success(inputPatternToRegExp(s));
|
return t.success(inputPatternToRegExp(s));
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof InvalidRegexError) {
|
||||||
|
return t.failure(s, [], err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
s => `/${s.source}/${s.flags}`,
|
s => `/${s.source}/${s.flags}`,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue