Skip to content

Commit

Permalink
update matches scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
ckagiri committed Dec 1, 2024
1 parent 3342910 commit 530b3f4
Showing 1 changed file with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const DEFAULT_INTERVAL_MILLISECONDS = 6 * 60 * 60 * 1000; // 6H
export class TodayAndMorrowScheduler extends BaseScheduler {
private scheduleDate?: Date;
private nextPoll?: moment.Moment; // ideally what is supposed to be the next poll
private hasLiveMatch = false;
private hasNewLiveMatch = false;
private liveMatchId = null;
private liveMatchHasFinished = false;

public static getInstance(
todayAndMorrowService = TodayAndMorrowServiceImpl.getInstance(),
Expand All @@ -30,12 +30,7 @@ export class TodayAndMorrowScheduler extends BaseScheduler {
this.scheduleDate = scheduleDate;
return;
}
const scheduleDateDiffInSeconds = moment.duration(moment(scheduleDate).diff(this.scheduleDate)).asSeconds()
if (scheduleDateDiffInSeconds >= 300) {
console.log(`${this.job.name} publish footballApiMatchUpdatesCompleted`);
this.eventMediator.publish('footballApiMatchUpdatesCompleted');
}
this.scheduleDate = scheduleDate;
console.log(`${this.job.name} onScheduled ${moment(scheduleDate).format()}`);
})
}

Expand All @@ -45,29 +40,38 @@ export class TodayAndMorrowScheduler extends BaseScheduler {
Math.round(moment.duration(this.nextPoll.diff(moment())).asHours())
if (nextPollInHours == undefined || nextPollInHours === 6) {
period = PERIOD.TODAY_AND_MORROW
} else if (this.hasLiveMatch && !this.hasNewLiveMatch) {
period = PERIOD.LIVE
} else if (this.liveMatchHasFinished) {
period = PERIOD.TODAY;
} else if (this.liveMatchId != null) {
period = PERIOD.LIVE;
}

const apiMatches = await this.todayAndMorrowService.syncMatches(period);
if (this.liveMatchId == null && this.liveMatchHasFinished) {
console.log(`${this.job.name} publish footballApiMatchUpdatesCompleted`);
this.eventMediator.publish('footballApiMatchUpdatesCompleted');
}

if (this.liveMatchHasFinished) {
this.liveMatchHasFinished = false;
}
return apiMatches;
}

calculateNextInterval(result: any = []): number {
const apiMatches: any[] = result;

let hasLiveMatch = false;
let hasNewLiveMatch = false;
let nextPoll = moment().add(12, 'hours');
let liveMatch: any = null;
for (const match of apiMatches) {
const matchStatus = getMatchStatus(match.status);
const matchStart = moment(match.utcDate);

if (matchStatus === MatchStatus.LIVE) {
hasLiveMatch = true;
if (Math.abs(matchStart.diff(moment(), 'seconds')) <= 90) {
hasNewLiveMatch = true;
break;
if (liveMatch == null) {
liveMatch = match;
} else if (matchStart.isBefore(moment(liveMatch.utcDate))) {
liveMatch = match;
}
} else if (matchStatus === MatchStatus.SCHEDULED) {
if (matchStart.isBefore(nextPoll)) {
Expand All @@ -76,18 +80,27 @@ export class TodayAndMorrowScheduler extends BaseScheduler {
}
}

if (this.hasLiveMatch && !hasLiveMatch) {
this.hasLiveMatch = false;
this.nextPoll = moment().add(3, 'minutes');
} else if (hasLiveMatch) {
this.hasLiveMatch = true;
this.hasNewLiveMatch = hasNewLiveMatch;
this.nextPoll = moment().add(90, 'seconds');
} else {
// precautionary handle nextPoll being behind
const diff = nextPoll.diff(moment(), 'minutes');
this.nextPoll = diff <= 0 ? moment().add(3, 'minutes') : nextPoll;
const liveMatchId = liveMatch?.id;
if (this.liveMatchId == null && liveMatchId != null) {
this.liveMatchId = liveMatchId;
} else if (this.liveMatchId != null && liveMatchId != null) {
if (this.liveMatchId !== liveMatchId) {
this.liveMatchId = liveMatchId;
this.liveMatchHasFinished = true;
}
} else if (this.liveMatchId != null && liveMatchId == null) {
this.liveMatchId = null;
this.liveMatchHasFinished = true;
}

// precautionary handle nextPoll being behind
const diff = nextPoll.diff(moment(), 'minutes');
nextPoll = diff <= 0 ? moment().add(1, 'minutes') : nextPoll;

if (liveMatchId != null || this.liveMatchId != null) {
nextPoll = moment().add(1, 'minutes');
}
this.nextPoll = nextPoll;

const nextIntervalInMs = Math.min(this.getDefaultIntervalMs(), this.nextPoll.diff(moment()));
const nextIntervalInUTC = new Date(Date.now() + nextIntervalInMs).toUTCString();
Expand Down

0 comments on commit 530b3f4

Please sign in to comment.