Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache): use UTC dates for cache strategy calculations #21488

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTime.utc()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the Luxon doc, I see toUTC()? I do not see an instance method called utc(). I do see a static function called utc().


/**
* 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;
}