Create timer to automatically purge expired claims #268
4 changed files with 33 additions and 2 deletions
|
@ -20,6 +20,14 @@ export default class Claim extends ButtonEvent {
|
|||
const droppedBy = interaction.customId.split(" ")[3];
|
||||
const userId = interaction.user.id;
|
||||
|
||||
const whenDropped = interaction.message.createdAt;
|
||||
const lastClaimableDate = new Date(Date.now() - (1000 * 60 * 5)); // 5 minutes ago
|
||||
|
||||
if (whenDropped < lastClaimableDate) {
|
||||
await interaction.channel.send(`${interaction.user}, Cards can only be claimed within 5 minutes of it being dropped!`);
|
||||
return;
|
||||
}
|
||||
|
||||
AppLogger.LogSilly("Button/Claim", `Parameters: cardNumber=${cardNumber}, claimId=${claimId}, droppedBy=${droppedBy}, userId=${userId}`);
|
||||
|
||||
const user = await User.FetchOneById(User, userId) || new User(userId, CardConstants.StartingCurrency);
|
||||
|
|
|
@ -16,6 +16,7 @@ import { SeriesMetadata } from "../contracts/SeriesMetadata";
|
|||
import AppLogger from "./appLogger";
|
||||
import TimerHelper from "../helpers/TimerHelper";
|
||||
import GiveCurrency from "../timers/GiveCurrency";
|
||||
import PurgeClaims from "../timers/PurgeClaims";
|
||||
|
||||
export class CoreClient extends Client {
|
||||
private static _commandItems: ICommandItem[];
|
||||
|
@ -79,8 +80,10 @@ export class CoreClient extends Client {
|
|||
.then(() => {
|
||||
AppLogger.LogInfo("Client", "App Data Source Initialised");
|
||||
|
||||
const timerId = this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false);
|
||||
this._timerHelper.StartTimer(timerId);
|
||||
this._timerHelper.AddTimer("*/20 * * * *", "Europe/London", GiveCurrency, false);
|
||||
this._timerHelper.AddTimer("0 0 * * *", "Europe/London", PurgeClaims, false);
|
||||
|
||||
this._timerHelper.StartAllTimers();
|
||||
})
|
||||
.catch(err => {
|
||||
AppLogger.LogError("Client", "App Data Source Initialisation Failed");
|
||||
|
|
|
@ -39,6 +39,12 @@ export default class AppBaseEntity {
|
|||
await repository.remove(entity);
|
||||
}
|
||||
|
||||
public static async RemoveMany<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T[]): Promise<void> {
|
||||
const repository = AppDataSource.getRepository<T>(target);
|
||||
|
||||
await repository.remove(entity);
|
||||
}
|
||||
|
||||
public static async FetchAll<T extends AppBaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
|
||||
const repository = AppDataSource.getRepository<T>(target);
|
||||
|
||||
|
|
14
src/timers/PurgeClaims.ts
Normal file
14
src/timers/PurgeClaims.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
import AppLogger from "../client/appLogger";
|
||||
import Claim from "../database/entities/app/Claim";
|
||||
|
||||
export default async function PurgeClaims() {
|
||||
const claims = await Claim.FetchAll(Claim);
|
||||
|
||||
const whenLastClaimable = new Date(Date.now() - (1000 * 60 * 5)); // 5 minutes ago
|
||||
|
||||
const expiredClaims = claims.filter(x => x.WhenCreated < whenLastClaimable);
|
||||
|
||||
await Claim.RemoveMany(Claim, expiredClaims);
|
||||
|
||||
AppLogger.LogInfo("Timers/PurgeClaims", `Purged ${expiredClaims.length} claims from the database`);
|
||||
}
|
Loading…
Reference in a new issue