socialbaking / karma

MIT License
3 stars 1 forks source link

Time remaining functionality for background task + other requests #83

Open fabiancook opened 1 year ago

fabiancook commented 1 year ago

We have a maximum time a function can run for, a task should give up after that time

A new abort controller should be available for every request The controller should be set on the fastify request context

const controller = new AbortController();
requestContext.set(ABORT_CONTROLLER, controller);

We should be able to get the signal anywhere using the context

function getAbortController() {
   const controller = requestContext.get(ABORT_CONTROLLER);
   ok(controller, "Expected AbortController to be available");
   return controller;
}
function getSignal() {
  return getAbortController().signal;
}

The abort controller should be aborted once the timeout hits

const timeout = setTimeout(() => controller.abort());
requestContext.set(ABORT_TIMEOUT, timeout);

When the request finishes before the timeout we want to clear this timeout

const timeout = requestContext.get(ABORT_TIMEOUT);
clearTimeout(timeout);