Skip to content

Commit

Permalink
fix(cache): use UTC dates for cache strategy calculations (#21488)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrindel committed Apr 13, 2023
1 parent 7666dd9 commit a12ac33
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class AbstractGithubGraphqlCacheStrategy<
/**
* The time which is used during single cache access cycle.
*/
protected readonly now = DateTime.now();
protected readonly now = DateTime.now().toUTC();

/**
* Set of all versions which were reconciled
Expand Down Expand Up @@ -73,14 +73,14 @@ export abstract class AbstractGithubGraphqlCacheStrategy<
const storedData = await this.load();
if (storedData) {
const cacheTTLDuration = {
days: AbstractGithubGraphqlCacheStrategy.cacheTTLDays,
hours: AbstractGithubGraphqlCacheStrategy.cacheTTLDays * 24,
};
if (!isDateExpired(this.now, storedData.createdAt, cacheTTLDuration)) {
result = storedData;
}
}

this.createdAt = DateTime.fromISO(result.createdAt);
this.createdAt = DateTime.fromISO(result.createdAt).toUTC();
this.items = result.items;
return this.items;
}
Expand All @@ -91,7 +91,7 @@ export abstract class AbstractGithubGraphqlCacheStrategy<
*/
private isStabilized(item: GithubItem): boolean {
const unstableDuration = {
days: AbstractGithubGraphqlCacheStrategy.cacheTTLDays,
hours: AbstractGithubGraphqlCacheStrategy.cacheTTLDays * 24,
};
return isDateExpired(this.now, item.releaseTimestamp, unstableDuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@ import { clone } from '../../../clone';
import type { GithubDatasourceItem, GithubGraphqlCacheRecord } from '../types';
import { GithubGraphqlMemoryCacheStrategy } from './memory-cache-strategy';

const isoTs = (t: string) => DateTime.fromJSDate(new Date(t)).toISO()!;
// const isoTs = (t: string) => DateTime.fromJSDate(new Date(t)).toISO()!;

const hourMinRe = /T\d{2}:\d{2}$/;
const hourMinSecRe = /T\d{2}:\d{2}:\d{2}$/;
const hourMinSecMillisRe = /T\d{2}:\d{2}:\d{2}\.\d\d\d$/;

const isoTs = (t: string) => {
let iso = t.replace(' ', 'T');
if (hourMinSecMillisRe.test(iso)) {
iso = iso + 'Z';
} else if (hourMinSecRe.test(iso)) {
iso = iso + '.000Z';
} else if (hourMinRe.test(iso)) {
iso = iso + ':00.000Z';
} else {
throw new Error('Unrecognized date-time string. ' + t);
}
return iso;
};

const mockTime = (input: string): void => {
const now = DateTime.fromISO(isoTs(input)).valueOf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { clone } from '../../../clone';
import type { GithubDatasourceItem, GithubGraphqlCacheRecord } from '../types';
import { GithubGraphqlPackageCacheStrategy } from './package-cache-strategy';

const isoTs = (t: string) => DateTime.fromJSDate(new Date(t)).toISO()!;
const isoTs = (t: string) => t.replace(' ', 'T') + ':00.000Z';

const mockTime = (input: string): void => {
const now = DateTime.fromISO(isoTs(input)).valueOf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export class GithubGraphqlPackageCacheStrategy<
cacheRecord: GithubGraphqlCacheRecord<GithubItem>
): Promise<void> {
if (this.hasUpdatedItems) {
const expiry = this.createdAt.plus({
// Not using 'days' as it does not handle adjustments for Daylight Saving time.
// The offset in the resulting DateTime object does not match that of the expiry or this.now.
hours: AbstractGithubGraphqlCacheStrategy.cacheTTLDays * 24,
});
const expiry = this.createdAt
.plus({
// Not using 'days' as it does not handle adjustments for Daylight Saving time.
// The offset in the resulting DateTime object does not match that of the expiry or this.now.
hours: AbstractGithubGraphqlCacheStrategy.cacheTTLDays * 24,
})
.toUTC();
const ttlMinutes = expiry.diff(this.now, ['minutes']).as('minutes');
if (ttlMinutes && ttlMinutes > 0) {
await packageCache.set(
Expand Down
2 changes: 1 addition & 1 deletion lib/util/github/graphql/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export function isDateExpired(
initialTimestamp: string,
duration: DurationLikeObject
): boolean {
const expiryTime = DateTime.fromISO(initialTimestamp).plus(duration);
const expiryTime = DateTime.fromISO(initialTimestamp).plus(duration).toUTC();
return currentTime >= expiryTime;
}

0 comments on commit a12ac33

Please sign in to comment.