Skip to content

Commit

Permalink
Gracefully failure when kv is not available
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Candeia <[email protected]>
  • Loading branch information
mcandeia committed Oct 15, 2024
1 parent 5cae25f commit e969250
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/actors/storage/denoKv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@ const ACTORS_DENO_KV_TOKEN = Deno.env.get("ACTORS_DENO_KV_TOKEN");
ACTORS_DENO_KV_TOKEN &&
Deno.env.set("DENO_KV_ACCESS_TOKEN", ACTORS_DENO_KV_TOKEN);

const kv = await Deno.openKv(ACTORS_KV_DATABASE);
let kv: Deno.Kv | null = null;

try {
kv = await Deno.openKv(ACTORS_KV_DATABASE);
} catch {
// ignore
}
function assertIsDefined<V>(
v: V | NonNullable<V>,
): asserts v is NonNullable<V> {
const isDefined = v !== null && typeof v !== "undefined";
if (!isDefined) {
throw new Error(`Expected 'v' to be defined, but received ${v}`);
}
}
interface AtomicOp {
kv: Deno.AtomicOperation;
dirty: Deno.KvEntryMaybe<unknown>[];
Expand All @@ -31,6 +44,7 @@ export class DenoKvActorStorage implements ActorStorage {
private kvOrTransaction: Deno.Kv | Deno.AtomicOperation;

constructor(protected options: StorageOptions) {
assertIsDefined(kv);
this.kv = kv;
this.kvOrTransaction = options.atomicOp?.kv ?? kv;
this.atomicOp = options.atomicOp;
Expand Down

0 comments on commit e969250

Please sign in to comment.