-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch GitHub latest tag server side and cache in Redis
- Loading branch information
Showing
10 changed files
with
282 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { api } from 'fpp/utils/api'; | ||
|
||
import { useConfigStore } from 'fpp/store/config.store'; | ||
|
||
import { FeatureFlagType } from 'fpp/server/db/schema'; | ||
|
||
export const useConfigLoader = () => { | ||
const setFeatureFlags = useConfigStore((state) => state.setFeatureFlags); | ||
|
||
const { data: featureFlags, status: statusGetFeatureFlag } = | ||
api.config.getFeatureFlags.useQuery(undefined, { | ||
staleTime: Infinity, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
retry: false, | ||
}); | ||
|
||
const setLatestTag = useConfigStore((state) => state.setLatestTag); | ||
|
||
const { data: latestTag, status: statusGetLatestTag } = | ||
api.config.getLatestTag.useQuery(undefined, { | ||
staleTime: Infinity, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
retry: false, | ||
}); | ||
|
||
useEffect(() => { | ||
if (statusGetFeatureFlag === 'success') { | ||
setFeatureFlags(featureFlags); | ||
} else { | ||
setFeatureFlags( | ||
Object.keys(FeatureFlagType).map((name) => ({ | ||
name: name as keyof typeof FeatureFlagType, | ||
enabled: false, | ||
})), | ||
); | ||
} | ||
if (statusGetLatestTag === 'success') { | ||
setLatestTag(latestTag); | ||
} | ||
}, [statusGetFeatureFlag, statusGetLatestTag]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { env } from 'fpp/env.mjs'; | ||
|
||
import * as Sentry from '@sentry/nextjs'; | ||
import { Redis } from '@upstash/redis'; | ||
import { sql } from 'drizzle-orm'; | ||
|
||
import { logEndpoint } from 'fpp/constants/logging.constant'; | ||
|
||
import { createTRPCRouter, publicProcedure } from 'fpp/server/api/trpc'; | ||
import { FeatureFlagType, featureFlags } from 'fpp/server/db/schema'; | ||
|
||
const redis = new Redis({ | ||
url: env.UPSTASH_REDIS_REST_URL_ROOM_STATE, | ||
token: env.UPSTASH_REDIS_REST_TOKEN_ROOM_STATE, | ||
}); | ||
|
||
export const configRouter = createTRPCRouter({ | ||
getFeatureFlags: publicProcedure.query(async ({ ctx: { db } }) => { | ||
const activeFeatureFlags = ( | ||
await db | ||
.select({ name: featureFlags.name }) | ||
.from(featureFlags) | ||
.where(sql`${featureFlags.enabled} = 1`) | ||
).map((row) => row.name); | ||
|
||
return Object.keys(FeatureFlagType).map((name) => ({ | ||
name: name as keyof typeof FeatureFlagType, | ||
enabled: activeFeatureFlags.includes(name), | ||
})); | ||
}), | ||
getLatestTag: publicProcedure.query(async () => { | ||
let latestTag: string | null = null; | ||
|
||
// TODO: improve by combining the two redis calls into one | ||
|
||
const redisLatestCommitSha = await redis.get( | ||
`${env.NEXT_PUBLIC_NODE_ENV}:latestCommitSha`, | ||
); | ||
|
||
// Validate if the latest redis commit sha is the same as the current one to not fetch the latest tag from GitHub | ||
if ( | ||
redisLatestCommitSha && | ||
redisLatestCommitSha === env.VERCEL_GIT_COMMIT_SHA | ||
) { | ||
latestTag = await redis.get('latestTag'); | ||
} | ||
|
||
// Fetch the latest tag from GitHub if the redis commit sha is not the same as the current one or latestTag is null | ||
if (!latestTag) { | ||
await fetch( | ||
'https://api.github.com/repos/jkrumm/free-planning-poker/tags', | ||
).then(async (res) => | ||
res | ||
.json() | ||
.then(async (res: { name: string }[]) => { | ||
console.log('res', res); | ||
|
||
latestTag = res[0]!.name; | ||
|
||
if (!latestTag) { | ||
throw new Error('no latest tag found'); | ||
} | ||
|
||
// Persist the latest tag and commit sha in redis | ||
await redis.set('latestTag', latestTag); | ||
await redis.set( | ||
`${env.NEXT_PUBLIC_NODE_ENV}:latestCommitSha`, | ||
env.VERCEL_GIT_COMMIT_SHA, | ||
); | ||
|
||
console.warn('fetched latest tag', { | ||
redisLatestCommitSha, | ||
commitSha: env.VERCEL_GIT_COMMIT_SHA as string, | ||
nodeEnv: env.NEXT_PUBLIC_NODE_ENV, | ||
latestTag, | ||
}); | ||
}) | ||
.catch((e) => { | ||
console.error('failed to fetch latest tag', e); | ||
Sentry.captureException(e, { | ||
tags: { | ||
endpoint: logEndpoint.GET_LATEST_TAG, | ||
}, | ||
extra: { | ||
redisLatestCommitSha, | ||
commitSha: env.VERCEL_GIT_COMMIT_SHA, | ||
nodeEnv: env.NEXT_PUBLIC_NODE_ENV, | ||
}, | ||
}); | ||
}), | ||
); | ||
} | ||
|
||
// Fallback to 2.0.0 if latestTag is still null | ||
return latestTag ?? '2.0.0'; | ||
}), | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.