denoland / deploy_feedback

For reporting issues with Deno Deploy
https://deno.com/deploy
74 stars 5 forks source link

[KV Feedback]: unavailable just before Deploy termination #656

Closed tamo closed 5 months ago

tamo commented 5 months ago

🔍

Type of feedback

Feature request

Description

Deno KV is great. It helps me code as soon as an idea comes up. Today I thought it would be useful to save some values on unload (because Deno Deploy shuts down often) and load the values on next startup. But ~KV doesn't seem to be available on unload or beforeunload.~ unload events are not triggered when a Deno deploy instance is destroyed.

Steps to reproduce (if applicable)

Something like

const kv = await Deno.openKv();
globalThis.addEventListener("beforeunload", (event) => {
  console.log("in event");
  event.preventDefault();
  kv.set(["some", "variable"], "value to store"); // .then((s) => console.log(s));
  setTimeout(() => console.log("later"), 3000); // to keep from shutdown for a while
});
Deno.serve(async () => new Response((await kv.get(["some", "variable"])).value))

Expected behavior (if applicable)

I'd like to get/set KV just before the deploy instance shuts down.

Possible solution (if applicable)

Maybe another event?

Additional context

In Deno Deploy context.

tamo commented 5 months ago

I'll check further whether this is legit

tamo commented 5 months ago

~Deno's unload doesn't seem to allow me to Kv.set.~ Deno deploy doesn't allow me to do anything before killing the instance. So events of Kv's closing / unloading would be great. I would like to Kv.set when (or just before) an instance of Deno deploy exits.

tamo commented 5 months ago

Signals can't save me either

const kv = await Deno.openKv();

const setandexit = (signame: string) => (() => {
  console.log(signame);
  kv.set(["last shutdown"], new Date().toISOString())
    .then(() => console.log("set"));
  setTimeout(() => Deno.exit(), 1000);
});
for (const signame of ["SIGHUP", "SIGINT", "SIGTERM", "SIGPIPE"] as Deno.Signal[])
  Deno.addSignalListener(signame, setandexit(signame));

Deno.serve(async () => new Response(
  String((await kv.get(["last shutdown"])).value)
));
kt3k commented 5 months ago

@tamo What happens if you await the kv.set call?

tamo commented 5 months ago

@tamo What happens if you await the kv.set call?

Looks the same. Kv has no data after restarting the instance. https://dash.deno.com/playground/low-ape-11

kt3k commented 5 months ago

Ah, sorry. Signal handlers are not supported in Deploy (They are just no-op functions in Deploy). unload and beforeunload events are also not supported.

I'm curious what you would want to accomplish by saving data at instance termination.

tamo commented 5 months ago

Thank you for the clarification! Fair enough that they are not supported. I'm closing this issue.

What I wanted to do was, in short, to keep some data over rebooting, while avoiding Kv.set/get as much as possible.

https://github.com/tamo/shm-rss/blob/801c5e0ff3d1d602415b7a396b751d9f475bd9f7/shm.ts#L310-L314