Skip to content

Commit

Permalink
fix storage identifier not changing between deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBlanche committed Aug 18, 2023
1 parent 84eeb80 commit 557e52a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/cache/CacheStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export type CacheStorage = {
export interface CacheStorageCreator {
instance(): {
import: { name: string; url: string };
instanceParams: (config: string, manifest: string) => string[];
instanceParams: (config: string, manifest: string, deploymentId: string) => string[];
};
}
19 changes: 11 additions & 8 deletions src/cache/UpstashCacheStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Manifest } from "../Manifest.ts";
import { CacheStorage, CacheStorageCreator } from "./CacheStorage.ts";

export class UpstashCache implements CacheStorageCreator {
Expand All @@ -13,24 +12,28 @@ export class UpstashCache implements CacheStorageCreator {
instance() {
return {
import: { name: "UpstashCacheStorage", url: import.meta.url },
instanceParams: (_config: string, manifest: string) => [`"${this.#url}"`, `"${this.#token}"`, manifest],
instanceParams: (
_config: string,
_manifest: string,
deploymentId: string,
) => [`"${this.#url}"`, `"${this.#token}"`, deploymentId],
};
}
}

export class UpstashCacheStorage implements CacheStorage {
#url: string;
#token: string;
#manifest: Manifest;
#deploymentId: string;

constructor(url: string, token: string, manifest: Manifest) {
constructor(url: string, token: string, deploymentId: string) {
this.#url = url;
this.#token = token;
this.#manifest = manifest;
this.#deploymentId = deploymentId;
}

async set(key: string, content: string) {
const command = ["hset", this.#manifest.id, key, content];
const command = ["hset", this.#deploymentId, key, content];

const response = await this.#sendCommand(command);

Expand All @@ -41,7 +44,7 @@ export class UpstashCacheStorage implements CacheStorage {
}

async get(key: string) {
const command = ["hget", this.#manifest.id, key];
const command = ["hget", this.#deploymentId, key];

const response = await this.#sendCommand(command);
const body = await response.json();
Expand All @@ -54,7 +57,7 @@ export class UpstashCacheStorage implements CacheStorage {
}

async delete(key: string) {
const command = ["hdel", this.#manifest.id, key];
const command = ["hdel", this.#deploymentId, key];

const response = await this.#sendCommand(command);
if (response.status !== 200) {
Expand Down
27 changes: 24 additions & 3 deletions src/export/DenoExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ async function insert(cacheStorage, responsePath, response) {

const manifestName = await getManifestName(this.#config);

const dateFormater = new Intl.DateTimeFormat("ja", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
const timeFormAter = new Intl.DateTimeFormat("en", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3,
hour12: false,
timeZone: "UTC",
});
const now = new Date();

await fs.ensureFile(serverScriptURL);
await Deno.writeTextFile(
serverScriptURL,
Expand All @@ -88,18 +103,24 @@ import { ${cacheStorageInstance.import.name} as CacheStorage } from "${
import userConfig from "${resolveFrugal(path.fromFileUrl(this.#config.self), serverScriptURL)}"
import * as manifest from "../${manifestName}"
const deploymentId = "${dateFormater.format(now)}-${timeFormAter.format(now)}";
console.log('deployment id', deploymentId)
const config = new FrugalConfig(userConfig)
const cacheStorage = new CacheStorage(${cacheStorageInstance.instanceParams("config", "manifest").join(", ")})
const cacheStorage = new CacheStorage(${
cacheStorageInstance.instanceParams("config", "manifest", "deploymentId").join(", ")
})
const cache = new RuntimeStorageCache(cacheStorage)
const router = new Router({ config, manifest, cache })
const current = await cacheStorage.get("__frugal__current")
if (current !== router.id) {
if (current !== deploymentId) {
console.log('populate')
const { populate } = await import("./populate.mjs")
await populate(cacheStorage, router.id)
await populate(cacheStorage, deploymentId)
}
const server = new FrugalServer({
Expand Down

0 comments on commit 557e52a

Please sign in to comment.