Coder Social home page Coder Social logo

Comments (4)

pangolingo avatar pangolingo commented on June 18, 2024 1

Thanks @charlypoly! I think this gives me what I need.

from defer.client.

charlypoly avatar charlypoly commented on June 18, 2024

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?

from defer.client.

pangolingo avatar pangolingo commented on June 18, 2024

I have a follow-up question: what would be your expectations for a proper Defer local setup?

  1. I'd expect a warning if no ENV variable is set when running a Deferred function
  2. 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.

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);
}

from defer.client.

charlypoly avatar charlypoly commented on June 18, 2024
  1. 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
  }

   // ...
}

from defer.client.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.