Defer ignores an absent ENV variable
pangolingo opened this issue · comments
Current behavior:
If the DEFER_TOKEN
environment variable is absent, a deferred function will run locally with no indication that it is not running as a background job.
Expected behavior:
If the deferred function could not be run as a background job on Defer's infrastructure because of a missing/misconfigured environment variable or some other reason, an error should be thrown.
Or at minimum the user should be able to specify that the function should or should not fall back to running locally.
Steps to reproduce:
- Add the Defer library
- Do not add a
DEFER_TOKEN
environment variable - Write a deferred function
- Call the deferred function
- Check the logs for errors
Explanation:
I'd like to see a log or error message to indicate that I didn't set up Defer correctly.
Some of my jobs are not appropriate to run locally - for example it may be a long-running job called from a serverless function with a short timeout
Hi @pangolingo!
Thank you for your detailed feedback!
I have a follow-up question: what would be your expectations for a proper Defer local setup?
Some of my jobs are not appropriate to run locally - for example it may be a long-running job called from a serverless function with a short timeout
It is not documented yet, but the @defer/client
exports a deferEnabled()
function that could be used for this use case to conditionally execute or not a background function locally. What do you think?
I have a follow-up question: what would be your expectations for a proper Defer local setup?
- I'd expect a warning if no ENV variable is set when running a Deferred function
- I'd expect a way to opt out (or opt in) to running the function locally/synchronously (I envision
runLocally: process.env.NODE_ENV !== 'production'
. Maybe ifrunLocally
is explicitly set then there is no warning? Or mayberunLocally
has multiple options:never
,always
,fallback
. It would be nice if this could be set globally instead of per function.
It is not documented yet, but the @defer/client exports a deferEnabled() function that could be used for this use case to conditionally execute or not a background function locally. What do you think?
So deferEnabled()
is a way to check whether functions will be run locally or not (whether the ENV variable is set)?
Is this how I'd use it?
// force this function to only run if Defer is set up properly
if (deferEnabled()) {
defer(helloWorld);
}
- I'd expect a warning if no ENV variable is set when running a Deferred function
Since v1.15.0, @gearnode added a hint that the @defer/client
prints when no DEFER_TOKEN
is present (for local dev)
I'd expect a way to opt out (or opt in) to running the function locally/synchronously (I envision runLocally: process.env.NODE_ENV !== 'production'. Maybe if runLocally is explicitly set then there is no warning? Or maybe runLocally has multiple options: never, always, fallback. It would be nice if this could be set globally instead of per function.
So
deferEnabled()
is a way to check whether functions will be run locally or not (whether the ENV variable is set)?
Yes, deferEnabled()
helps to know if the Defer Functions will run locally or on the Defer platform.
Example:
src/api/hello.ts
import { deferEnabled } from '@defer/client'
import longRunningJob from '../defer/longRunningJob'
export default async function handler(req, res) {
// ...
if (deferEnabled()) { // when deployed on staging, production
await longRunningJob()
} else {
// return a mock response for local env
}
// ...
}
Thanks @charlypoly! I think this gives me what I need.