Skip to content

Commit

Permalink
PersistentTimer: add copious logging to ticking
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlul committed Nov 30, 2024
1 parent a184b6b commit 8050f19
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/timer/PersistentTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export class PersistentTimer {
protected interval?: NodeJS.Timeout;

/// This constructor has side effects as it immediately starts the timer!
protected constructor(protected entity: Countdown, protected discord: PersistentTimerDiscordDelegate) {
protected constructor(
protected entity: Countdown,
protected discord: PersistentTimerDiscordDelegate
) {
this.interval = setInterval(() => this.tick(), 1000);
}

Expand Down Expand Up @@ -123,23 +126,31 @@ export class PersistentTimer {
protected async tick(): Promise<void> {
const end = this.entity.end;
const now = new Date();
const iso = now.toISOString();
logger.verbose(`tick: ${this.entity.id} now(${iso}) end(${end.toISOString()})`);
if (end <= now) {
logger.verbose(`tick: ${this.entity.id} now(${iso}) aborting`);
await this.abort();
try {
logger.verbose(`tick: ${this.entity.id} now(${iso}) sending message`);
await this.discord.sendMessage(this.entity.channelId, this.entity.finalMessage);
logger.verbose(`tick: ${this.entity.id} now(${iso}) finished`);
} catch (error) {
logger.warn(error);
}
}
const secondsRemaining = Math.ceil((now.getTime() - end.getTime()) / 1000);
if (secondsRemaining % this.entity.cronIntervalSeconds == 0) {
logger.verbose(`tick: ${this.entity.id} now(${iso}) secondsRemaining(${secondsRemaining})`);
if (secondsRemaining % this.entity.cronIntervalSeconds === 0) {
const left = PersistentTimer.formatTime(end.getTime() - Date.now());
logger.verbose(`tick: ${this.entity.id} now(${iso}) left(${left})`);
try {
await this.discord.editMessage(
this.entity.channelId,
this.entity.messageId,
`Time left in the round: \`${left}\`. Ends ${time(end)} (${time(end, "R")}).`
);
logger.verbose(`tick: ${this.entity.id} now(${iso}) edited`);
} catch (error) {
logger.warn(`tick: could not edit ${this.entity.channelId} ${this.entity.messageId}`);
logger.warn(error);
Expand Down

0 comments on commit 8050f19

Please sign in to comment.