mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-20 16:25:03 +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 { DAYS, DBDateFormat, HOURS, MINUTES } from "../utils";
|
||||
import { connection } from "./db";
|
||||
import { Queue } from "../Queue";
|
||||
|
||||
const DELETE_UNUSED_COUNTERS_AFTER = 1 * DAYS;
|
||||
const DELETE_UNUSED_COUNTER_TRIGGERS_AFTER = 1 * DAYS;
|
||||
|
||||
const MAX_COUNTER_VALUE = 2147483647; // 2^31-1, for MySQL INT
|
||||
|
||||
const decayQueue = new Queue();
|
||||
|
||||
async function deleteCountersMarkedToBeDeleted(): Promise<void> {
|
||||
await getRepository(Counter)
|
||||
.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({
|
||||
where: {
|
||||
id,
|
||||
|
@ -186,14 +190,16 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
? `GREATEST(value - ${decayAmountToApply}, 0)`
|
||||
: `LEAST(value + ${Math.abs(decayAmountToApply)}, ${MAX_COUNTER_VALUE})`;
|
||||
|
||||
await this.counterValues.update(
|
||||
{
|
||||
counter_id: id,
|
||||
},
|
||||
{
|
||||
// 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
|
||||
await this.counterValues
|
||||
.createQueryBuilder("CounterValue")
|
||||
.where("counter_id = :id", { id })
|
||||
.orderBy("id")
|
||||
.update({
|
||||
value: () => rawUpdate,
|
||||
},
|
||||
);
|
||||
})
|
||||
.execute();
|
||||
|
||||
await this.counters.update(
|
||||
{
|
||||
|
@ -203,6 +209,7 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
last_decay_at: newLastDecayDate,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
async markUnusedTriggersToBeDeleted(triggerIdsToKeep: number[]) {
|
||||
|
|
|
@ -27,7 +27,15 @@ export const TRegex = new t.Type<RegExp, string>(
|
|||
(s): s is RegExp => s instanceof RegExp,
|
||||
(from, to) =>
|
||||
either.chain(t.string.validate(from, to), s => {
|
||||
try {
|
||||
return t.success(inputPatternToRegExp(s));
|
||||
} catch (err) {
|
||||
if (err instanceof InvalidRegexError) {
|
||||
return t.failure(s, [], err.message);
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
}),
|
||||
s => `/${s.source}/${s.flags}`,
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue