Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extending a serve handler by overriding the existing isProduction #425

Closed
wants to merge 3 commits into from

Conversation

jpwilliams
Copy link
Member

@jpwilliams jpwilliams commented Dec 13, 2023

Summary

Allows users to override the existing isProduction check performed by serve handlers, providing their own value (or dynamic value).

Note

Not specifying the value--or if the value or dynamic value is undefined--will ensure the SDK falls back to using the current production checks.

import { functions, inngest } from "@/inngest";
import { serve } from "inngest/next";

export default serve({
  client: inngest,
  functions,
  // set a static value
  isProduction: false,
});
import { functions, inngest } from "@/inngest";
import { serve } from "inngest/next";

export default serve({
  client: inngest,
  functions,
  // or use an environment variable check
  isProduction: process.env.MY_CUSTOM_VAR === "yes",
});
import { functions, inngest } from "@/inngest";
import { serve } from "inngest/next";

export default serve({
  client: inngest,
  functions,
  // or use a function to calculate the value dynamically
  isProduction: async () => {
    const amIProd = await makeRequestToCheck();
    return amIProd;
  },
});

It's specified on the serve() handler as they already need to be able to decide for themselves whether they're in production mode, as some pieces of the puzzle (like environment variables) are not available until the request enters the system.

Therefore, this is the first part of allowing users to extend existing serve handlers to override pieces of functionality such as production checks, URL creation, or accessing environment variables.

Checklist

  • Added a docs PR that references this PR
  • Added unit/integration tests
  • Added changesets if applicable

Related

@jpwilliams jpwilliams added prerelease/inngest Create snapshot releases for a PR for the "inngest" package. ⬆️ improvement Performance, reliability, or usability improvements labels Dec 13, 2023
@jpwilliams jpwilliams self-assigned this Dec 13, 2023
Copy link

changeset-bot bot commented Dec 13, 2023

⚠️ No Changeset found

Latest commit: caf0316

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@inngest-release-bot inngest-release-bot added the 📦 inngest Affects the `inngest` package label Dec 13, 2023
@inngest-release-bot
Copy link
Collaborator

inngest-release-bot commented Dec 13, 2023

A user has added the prerelease/inngest label, so this PR will be published to npm with the tag pr-425. It will be updated with the latest changes as you push commits to this PR.

You can install this prerelease version with:

npm install inngest@pr-425

The last release was built and published from caf0316.

Copy link
Member

@djfarrelly djfarrelly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is a weird DX that this is isProduction which the dev then may only want sometimes to opt-out of production checks, but they may want the production checks that already exist by default.

Thinking through the usage of this for integration testing, you might see this and say, if I set this to false, that should make it not act as it was in prod, but that' snot true due to the check in the comm handler's constructor:

export default serve({
  client: inngest,
  functions,
  // Setting false will not work - I want to opt-out, not opt in
  isProduction: !!process.env.INTEGRATION_TEST ? 
    false : 
    // I'd still want to default NODE_ENV check to work
    // so I don't want to say if not integration test, then true
    undefined, 
});
INTEGRATION_TEST=1 node ./index.js # this won't work right if NODE_ENV=production

So then to solve this in the current code where there is the options.isProduction check within the comm handler's constructor, you'd have to make it a function:

export default serve({
  client: inngest,
  functions,
  // Function has to return undefined to fallback to the default NODE_ENV check
  // w/ the nullish coalescing in handleAction
  isProduction: () =>  !!process.env.INTEGRATION_TEST ? 
    false : 
    undefined, 
});
INTEGRATION_TEST=1 node ./index.js

It feels a bit cumbersome to basically just opt-out of the signing key security checks. Our customer had an interesting thought of just making this more explicit like disableProductionChecks: boolean which is more of an opt-in than the current approach.

export default serve({
  client: inngest,
  functions,
  // Some positives:
  // - The user can customize this as needed
  // - It's less clever - more straightforward what this does
  // - It clearly demonstrates some sort of risk if you opt-out
  disableProductionChecks: () =>  !!process.env.INTEGRATION_TEST
});

Sharing this in the PR for transparency/posterity 😄

@IGassmann
Copy link
Contributor

@jpwilliams, a user tried this feature and experienced some issues. Linking the conversation..

@jpwilliams
Copy link
Member Author

Thanks, @IGassmann.

Closing this now. Superseded by #488.

@jpwilliams jpwilliams closed this Feb 14, 2024
jpwilliams added a commit that referenced this pull request Feb 23, 2024
## Summary
<!-- Succinctly describe your change, providing context, what you've
changed, and why. -->

Adds support for `INNGEST_DEV` and a new `isDev` option on the client.

This lightly refactors the current checks based around `isProd` and
`skipDevServer()`, which were getting a little difficult to read.

- The SDK now has two "modes:" `"dev"` and `"cloud"`.
- Each mode is either **explicit** or **inferred**. An inferred mode
means that the current (`v3.x.x`) version of the SDK can make a decision
to attempt to contact the Dev Server. Future versions will remove this
and default to `"cloud"` mode.
- Setting the `INNGEST_DEV` environment variable or the `isDev` client
option **explicitly** sets the mode to either `"cloud"` or `"dev"`.
- `INNGEST_DEV` accepts some sensible defaults. We'll recommend `1` to
explicitly set `"dev"` mode and `0` to explicitly set `"cloud"` mode,
though it also accepts `"true"`, `"y"`, `"no"`, etc.
- Explicitly setting either mode also sets the event ingestion and
syncing URLs. They continue to be further overwritten by passing
`INNGEST_BASE_URL`, `INNGEST_API_BASE_URL`, and
`INNGEST_EVENT_API_BASE_URL`.

> [!NOTE]
> To support many runtimes and environments, environment variables are
not always accessible a) at all times, and b) on `process.env`.
Sometimes environment variables are accessed via different global
objects, or sometimes runtime objects that are passed to requests.
>
> For this reason, handling environment variables is more complex and
relies on making best guesses during instantiation, then later making
another decision when we have access to the environment.

Supersedes both #424 and #425.

## Checklist
<!-- Tick these items off as you progress. -->
<!-- If an item isn't applicable, ideally please strikeout the item by
wrapping it in "~~"" and suffix it with "N/A My reason for skipping
this." -->
<!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" -->

- [x] Added a [docs PR](https://github.com/inngest/website) documenting
these modes and the new environment variables that references this PR
- [x] Added unit/integration tests
- [x] Added changesets if applicable
- [x] Push env-related changes to the OS SDK Spec

## Related
<!-- A space for any related links, issues, or PRs. -->
<!-- Linear issues are autolinked. -->
<!-- e.g. - INN-123 -->
<!-- GitHub issues/PRs can be linked using shorthand. -->
<!-- e.g. "- inngest/inngest#123" -->
<!-- Feel free to remove this section if there are no applicable related
links.-->
- INN-2754
- Supersedes #424
- Supersedes #425
- inngest/website#679
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ improvement Performance, reliability, or usability improvements 📦 inngest Affects the `inngest` package prerelease/inngest Create snapshot releases for a PR for the "inngest" package.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants