Skip to content

Commit

Permalink
Time monitor failure handling (#819)
Browse files Browse the repository at this point in the history
* Rename updated field to lastTimeChecked in TimeTag

* Remove unused special field in TimeTag

* Update time tag with old value in case of error, to reflect lastTimeCheck value

* Add prefix for TimeMonitor logger

* Don't use special field of TimeTag in Timekeeper
  • Loading branch information
adrianmroz-allegro committed Dec 7, 2021
1 parent c16c945 commit f8c7af2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/common/models/time-tag/time-tag.mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ describe("TimeTag", () => {
{
name: "dodo",
time: new Date("2015-10-15T19:20:00Z"),
updated: new Date("2015-10-15T19:20:13Z")
lastTimeChecked: new Date("2015-10-15T19:20:13Z")
},
{
name: "wikipedia",
time: new Date("2015-10-15T19:21:00Z"),
updated: new Date("2015-10-15T19:21:13Z")
lastTimeChecked: new Date("2015-10-15T19:21:13Z")
}
]);
});
Expand Down
27 changes: 11 additions & 16 deletions src/common/models/time-tag/time-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@

import { BaseImmutable, Property, PropertyType } from "immutable-class";

export type Special = "static" | "realtime";

export interface TimeTagValue {
name: string;
time?: Date;
updated?: Date;
special?: Special;
lastTimeChecked?: Date;
}

export interface TimeTagJS {
name: string;
time?: Date | string;
updated?: Date | string;
special?: Special;
lastTimeChecked?: Date | string;
}

export class TimeTag extends BaseImmutable<TimeTagValue, TimeTagJS> {
Expand All @@ -42,8 +38,7 @@ export class TimeTag extends BaseImmutable<TimeTagValue, TimeTagJS> {
static PROPERTIES: Property[] = [
{ name: "name" },
{ name: "time", type: PropertyType.DATE, defaultValue: null },
{ name: "updated", type: PropertyType.DATE, defaultValue: null },
{ name: "special", defaultValue: null }
{ name: "lastTimeChecked", type: PropertyType.DATE, defaultValue: null }
];

static fromJS(parameters: TimeTagJS): TimeTag {
Expand All @@ -52,19 +47,19 @@ export class TimeTag extends BaseImmutable<TimeTagValue, TimeTagJS> {

public name: string;
public time: Date;
public updated: Date;
public special: Special;
public lastTimeChecked: Date;

constructor(parameters: TimeTagValue) {
super(parameters);
if (this.time && !this.updated) this.updated = this.time;
if (this.time && !this.lastTimeChecked) this.lastTimeChecked = this.time;
}

public changeTime(time: Date, now: Date): TimeTag {
var value = this.valueOf();
value.time = time;
value.updated = now;
return new TimeTag(value);
public changeTime(time: Date, lastTimeChecked: Date): TimeTag {
return new TimeTag({
name: this.name,
time,
lastTimeChecked
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/models/timekeeper/timekeeper.mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ describe("Timekeeper", () => {
},
{
timeTags: {
lol: { name: "lol", time: new Date("2016-01-01T01:02:03Z"), updated: new Date("2016-01-01T01:02:03Z") }
lol: { name: "lol", time: new Date("2016-01-01T01:02:03Z"), lastTimeChecked: new Date("2016-01-01T01:02:03Z") }
},
nowOverride: null
},
{
timeTags: {
lol: { name: "lol", time: new Date("2016-01-01T01:02:03Z"), updated: new Date("2016-01-01T01:02:03Z") }
lol: { name: "lol", time: new Date("2016-01-01T01:02:03Z"), lastTimeChecked: new Date("2016-01-01T01:02:03Z") }
},
nowOverride: new Date("2016-01-01T01:02:03Z")
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/models/timekeeper/timekeeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export class Timekeeper implements Instance<TimekeeperValue, TimekeeperJS> {

getTime(name: string): Date {
const timeTag = this.timeTags.get(name);
if (!timeTag || timeTag.special === "realtime") return this.now();
return timeTag.time || this.now();
if (!timeTag || !timeTag.time) return this.now();
return timeTag.time;
}

private changeTimeTags(timeTags: Map<string, TimeTag>): Timekeeper {
Expand Down
11 changes: 7 additions & 4 deletions src/common/utils/time-monitor/time-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ export class TimeMonitor {
public timekeeper: Timekeeper;
private regularCheckInterval: number;
private checks: Map<string, Nullary<Promise<Date>>>;
private logger: Logger;
private doingChecks = false;

constructor(private logger: Logger) {
constructor(logger: Logger) {
this.logger = logger.addPrefix("TimeMonitor");
this.checks = new Map();
this.regularCheckInterval = 60000;
this.timekeeper = Timekeeper.EMPTY;
Expand All @@ -49,23 +51,24 @@ export class TimeMonitor {
return this;
}

private doCheck = ({ name }: TimeTag): Promise<void> => {
private doCheck = ({ name, time: previousTime }: TimeTag): Promise<void> => {
const { logger, checks } = this;
const check = checks.get(name);
if (!check) return Promise.resolve(null);
return check().then(updatedTime => {
logger.log(`Got the latest time for '${name}' (${updatedTime.toISOString()})`);
this.timekeeper = this.timekeeper.updateTime(name, updatedTime);
}).catch(e => {
logger.error(`Error getting time for '${name}': ${e.message}`);
logger.error(`Failed getting time for '${name}', using previous time.`, `Error: ${e.message}`);
this.timekeeper = this.timekeeper.updateTime(name, previousTime);
}
);
}

private isStale = (timeTag: TimeTag): boolean => {
const { timekeeper, regularCheckInterval } = this;
const now = timekeeper.now().valueOf();
return !timeTag.time || now - timeTag.updated.valueOf() > regularCheckInterval;
return !timeTag.time || now - timeTag.lastTimeChecked.valueOf() > regularCheckInterval;
}

private doChecks = (): void => {
Expand Down

0 comments on commit f8c7af2

Please sign in to comment.