-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(run-engine): trigger tasks pinned to an external deployment id #4664
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| "@trigger.dev/core": patch | ||
| "@trigger.dev/sdk": patch | ||
| --- | ||
|
|
||
| Pin runs to the deployment your calling code came from, so an old release never triggers tasks from a new one: set `TRIGGER_EXTERNAL_DEPLOYMENT_ID` to the id you deployed with, or `TRIGGER_AUTOMATIC_SKEW_VERSION_PROTECTION=1` to detect the commit automatically on Vercel and most CI systems. Runs triggered before that deployment finishes building wait for it, then start pinned. |
This file contains hidden or 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 hidden or 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
214 changes: 214 additions & 0 deletions
214
apps/webapp/app/services/externalDeploymentCache.server.ts
This file contains hidden or 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,214 @@ | ||
| import type { Callback, Redis, Result } from "ioredis"; | ||
| import { logger } from "./logger.server"; | ||
|
|
||
| export type ExternalDeploymentCacheEntry = { | ||
| workerId: string; | ||
| version: string; | ||
| sdkVersion: string; | ||
| cliVersion: string; | ||
| }; | ||
|
|
||
| export type ExternalDeploymentCacheResult = | ||
| | { outcome: "deployed"; entry: ExternalDeploymentCacheEntry } | ||
| | { outcome: "missing" }; | ||
|
|
||
| export interface ExternalDeploymentCache { | ||
| get(environmentId: string, externalId: string): Promise<ExternalDeploymentCacheResult | null>; | ||
| setIfNewer( | ||
| environmentId: string, | ||
| externalId: string, | ||
| entry: ExternalDeploymentCacheEntry | ||
| ): Promise<void>; | ||
| setMissing(environmentId: string, externalId: string): Promise<void>; | ||
| } | ||
|
|
||
| const KEY_PREFIX = "skewid:"; | ||
|
|
||
| const DEFAULT_TTL_SECONDS = 30 * 24 * 60 * 60; | ||
|
|
||
| const DEFAULT_MISSING_TTL_SECONDS = 20; | ||
|
|
||
| const MISSING_ENTRY = JSON.stringify({ m: 1 }); | ||
|
|
||
| function buildKey(environmentId: string, externalId: string): string { | ||
| return `${KEY_PREFIX}${environmentId}:${externalId}`; | ||
| } | ||
|
|
||
| type CachedEntry = { | ||
| w: string; | ||
| v: string; | ||
| s: string; | ||
| c: string; | ||
| }; | ||
|
|
||
| function encode(entry: ExternalDeploymentCacheEntry): string { | ||
| return JSON.stringify({ | ||
| w: entry.workerId, | ||
| v: entry.version, | ||
| s: entry.sdkVersion, | ||
| c: entry.cliVersion, | ||
| } satisfies CachedEntry); | ||
| } | ||
|
|
||
| function decode(raw: string): ExternalDeploymentCacheResult | null { | ||
| const parsed: unknown = JSON.parse(raw); | ||
|
|
||
| if (typeof parsed !== "object" || parsed === null) { | ||
| return null; | ||
| } | ||
|
|
||
| const { w, v, s, c, m } = parsed as Partial<CachedEntry> & { m?: unknown }; | ||
|
|
||
| if (m === 1) { | ||
| return { outcome: "missing" }; | ||
| } | ||
|
|
||
| if (typeof w !== "string" || typeof v !== "string") { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| outcome: "deployed", | ||
| entry: { | ||
| workerId: w, | ||
| version: v, | ||
| sdkVersion: typeof s === "string" ? s : "", | ||
| cliVersion: typeof c === "string" ? c : "", | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const SET_IF_NEWER_LUA = ` | ||
| local existing = redis.call("GET", KEYS[1]) | ||
|
|
||
| if existing then | ||
| local ok, decoded = pcall(cjson.decode, existing) | ||
| if ok and type(decoded) == "table" and type(decoded.v) == "string" then | ||
| local existingDate, existingCounter = string.match(decoded.v, "^([^.]*)%.?(.*)$") | ||
| local incomingDate, incomingCounter = string.match(ARGV[2], "^([^.]*)%.?(.*)$") | ||
|
|
||
| if existingDate > incomingDate then | ||
| return 0 | ||
| end | ||
|
|
||
| if existingDate == incomingDate then | ||
| if (tonumber(existingCounter) or 0) >= (tonumber(incomingCounter) or 0) then | ||
| return 0 | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| redis.call("SET", KEYS[1], ARGV[1], "EX", tonumber(ARGV[3])) | ||
| return 1 | ||
| `; | ||
|
|
||
| declare module "ioredis" { | ||
| interface RedisCommander<Context> { | ||
| skewIdSetIfNewer( | ||
| key: string, | ||
| entry: string, | ||
| version: string, | ||
| ttlSeconds: string, | ||
| callback?: Callback<number> | ||
| ): Result<number, Context>; | ||
| } | ||
| } | ||
|
|
||
| export type RedisExternalDeploymentCacheOptions = { | ||
| redis: Redis; | ||
| ttlSeconds?: number; | ||
| missingTtlSeconds?: number; | ||
| }; | ||
|
|
||
| export class RedisExternalDeploymentCache implements ExternalDeploymentCache { | ||
| private readonly redis: Redis; | ||
| private readonly ttlSeconds: number; | ||
| private readonly missingTtlSeconds: number; | ||
|
|
||
| constructor(options: RedisExternalDeploymentCacheOptions) { | ||
| this.redis = options.redis; | ||
| this.ttlSeconds = options.ttlSeconds ?? DEFAULT_TTL_SECONDS; | ||
| this.missingTtlSeconds = options.missingTtlSeconds ?? DEFAULT_MISSING_TTL_SECONDS; | ||
|
|
||
| this.redis.defineCommand("skewIdSetIfNewer", { numberOfKeys: 1, lua: SET_IF_NEWER_LUA }); | ||
| } | ||
|
|
||
| async get( | ||
| environmentId: string, | ||
| externalId: string | ||
| ): Promise<ExternalDeploymentCacheResult | null> { | ||
| try { | ||
| const raw = await this.redis.get(buildKey(environmentId, externalId)); | ||
| if (!raw) return null; | ||
| return decode(raw); | ||
| } catch (error) { | ||
| logger.error("Failed to read external deployment resolution from cache", { | ||
| environmentId, | ||
| externalId, | ||
| error, | ||
| }); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| async setIfNewer( | ||
| environmentId: string, | ||
| externalId: string, | ||
| entry: ExternalDeploymentCacheEntry | ||
| ): Promise<void> { | ||
| try { | ||
| await this.redis.skewIdSetIfNewer( | ||
| buildKey(environmentId, externalId), | ||
| encode(entry), | ||
| entry.version, | ||
| String(this.ttlSeconds) | ||
| ); | ||
| } catch (error) { | ||
| logger.error("Failed to write external deployment resolution to cache", { | ||
| environmentId, | ||
| externalId, | ||
| version: entry.version, | ||
| error, | ||
| }); | ||
|
|
||
| try { | ||
| await this.redis.del(buildKey(environmentId, externalId)); | ||
| } catch (deleteError) { | ||
| logger.error("Failed to evict stale external deployment resolution after write failure", { | ||
| environmentId, | ||
| externalId, | ||
| error: deleteError, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async setMissing(environmentId: string, externalId: string): Promise<void> { | ||
| try { | ||
| await this.redis.set( | ||
| buildKey(environmentId, externalId), | ||
| MISSING_ENTRY, | ||
| "EX", | ||
| this.missingTtlSeconds, | ||
| "NX" | ||
| ); | ||
| } catch (error) { | ||
| logger.error("Failed to write missing external deployment marker to cache", { | ||
| environmentId, | ||
| externalId, | ||
| error, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export class NoopExternalDeploymentCache implements ExternalDeploymentCache { | ||
| async get(): Promise<ExternalDeploymentCacheResult | null> { | ||
| return null; | ||
| } | ||
|
|
||
| async setIfNewer(): Promise<void> {} | ||
|
|
||
| async setMissing(): Promise<void> {} | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.