Skip to content

Commit

Permalink
Removed notificationTimeout setting. Made it auto-detect timeout base…
Browse files Browse the repository at this point in the history
…d on next scheduled run. Resolves #356
  • Loading branch information
claabs committed Feb 6, 2024
1 parent 205665f commit 89c8ea4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
24 changes: 8 additions & 16 deletions src/common/config/classes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function, no-useless-constructor, max-classes-per-file */
import 'reflect-metadata';
import { ClassConstructor, Type } from 'class-transformer';
import { ClassConstructor, Expose, Type } from 'class-transformer';
import {
IsEmail,
IsUrl,
Expand All @@ -21,6 +21,7 @@ import {
} from 'class-validator';
import { ServerOptions } from 'https';
import { ListenOptions } from 'net';
import cronParser from 'cron-parser';

export enum NotificationType {
EMAIL = 'email',
Expand Down Expand Up @@ -682,6 +683,12 @@ export class AppConfig {
@IsString()
cronSchedule = process.env.CRON_SCHEDULE || '0 0,6,12,18 * * *';

@Expose({ toClassOnly: true })
getMsUntilNextRun() {
const cronExpression = cronParser.parseExpression(this.cronSchedule);
return cronExpression.next().getTime() - new Date().getTime();
}

/**
* A list of excluded game titles to skip during processing.
* @example ['Gigabash Demo', 'Another Blacklisted Game']
Expand Down Expand Up @@ -799,21 +806,6 @@ export class AppConfig {
})
notifiers?: AnyNotifierConfig[];

/**
* Number of hours to wait for a response for a notification.
* The notification wait is blocking, so while other accounts will still continue, the process won't exit until all login requests are solved.
* If the timeout is reached, the process will exit, and the URL in the notification will be inaccessible.
* @example 168
* @default 24
* @env NOTIFICATION_TIMEOUT_HOURS
*/
@IsOptional()
@IsNumber()
@Min(0)
notificationTimeoutHours = process.env.NOTIFICATION_TIMEOUT_HOURS
? parseInt(process.env.NOTIFICATION_TIMEOUT_HOURS, 10)
: 24;

/**
* When true, the process will send test notifications with a test redirect to example.com for all configured accounts.
* **Be sure to disable this after a successful test.**
Expand Down
18 changes: 14 additions & 4 deletions src/device-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ export interface DeviceLoginProps {
user: string;
}

const NOTIFICATION_TIMEOUT = config.notificationTimeoutHours * 60 * 60 * 1000;

const hashAlphabet = 'abcdefghijklmnopqrstuvwxyz';
const hashLength = 4;
const hashids = new Hashids(Math.random().toString(), hashLength, hashAlphabet);
Expand Down Expand Up @@ -129,11 +127,17 @@ export class DeviceLogin {

public async testServerNotify(): Promise<void> {
const { reqId, url } = getUniqueUrl();
const notificationTimeout = config.getMsUntilNextRun();

logger.trace(
{ notificationTimeout: `in ${(notificationTimeout / (60 * 1000)).toFixed(1)} minutes` },
'Awaiting test notification response'
);

// Wait on a promise to be resolved by the web redirect completing
await Promise.all([
promiseTimeout(
NOTIFICATION_TIMEOUT,
notificationTimeout,
new Promise((resolve, reject) => {
pendingRedirects.set(reqId, this.onTestVisit(resolve, reject).bind(this));
})
Expand All @@ -145,11 +149,17 @@ export class DeviceLogin {

public async newDeviceAuthLogin(): Promise<void> {
const { reqId, url } = getUniqueUrl();
const notificationTimeout = config.getMsUntilNextRun();

logger.trace(
{ notificationTimeout: `in ${(notificationTimeout / (60 * 1000)).toFixed(1)} minutes` },
'Awaiting login notification response'
);

// Wait on a promise to be resolved by the web redirect and login completing
await Promise.all([
promiseTimeout(
NOTIFICATION_TIMEOUT,
notificationTimeout,
new Promise((resolve, reject) => {
pendingRedirects.set(reqId, this.onLoginVisit(resolve, reject).bind(this));
})
Expand Down

0 comments on commit 89c8ea4

Please sign in to comment.