From 705ea5bb74e0f6a4da1f079b2bcb079b3506c633 Mon Sep 17 00:00:00 2001 From: Rob Cameron Date: Wed, 18 Sep 2024 09:32:30 -0700 Subject: [PATCH] Default NODE_ENV to "development" if it's `undefined` when starting jobs worker (#11572) This mimics the behavior of `yarn rw dev` where `NODE_ENV` will equal `development` if you don't set it explicitly. Because of this, you need to make sure you explicitly set it in other environments. You should set `NODE_ENV=production` in your `.env` file/Dockerfile on your production server, for example. The docs have been updated to note this. Closes #11569 --- .changesets/11572.md | 3 ++ docs/docs/background-jobs.md | 16 +++++++++- .../version-8.0/background-jobs.md | 16 +++++++++- .../version-8.1/background-jobs.md | 16 +++++++++- packages/jobs/src/__tests__/setupEnv.test.ts | 29 +++++++++++++++++++ packages/jobs/src/bins/rw-jobs-worker.ts | 5 ++-- packages/jobs/src/bins/rw-jobs.ts | 5 ++-- packages/jobs/src/setupEnv.ts | 11 +++++++ 8 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 .changesets/11572.md create mode 100644 packages/jobs/src/__tests__/setupEnv.test.ts create mode 100644 packages/jobs/src/setupEnv.ts diff --git a/.changesets/11572.md b/.changesets/11572.md new file mode 100644 index 000000000000..2b2285f0f207 --- /dev/null +++ b/.changesets/11572.md @@ -0,0 +1,3 @@ +- Default NODE_ENV to "development" if it's `undefined` when starting jobs worker (#11572) by @cannikin + +This mimics the behavior of `yarn rw dev` where `NODE_ENV` will equal `development` if you don't set it explicitly. diff --git a/docs/docs/background-jobs.md b/docs/docs/background-jobs.md index 3fe45fe202e1..1bb62e47d184 100644 --- a/docs/docs/background-jobs.md +++ b/docs/docs/background-jobs.md @@ -701,7 +701,7 @@ By checking the `lastError` field in the database you can see what the last erro ## Deployment -For many use cases you may simply be able to rely on the job runner to start your job workers, which will run forever: +For many use cases you may be able to rely on the job runner to start and detach your job workers, which will then run forever: ```bash yarn rw jobs start @@ -723,6 +723,20 @@ Of course if you have a process monitor system watching your workers you'll want ::: +### NODE_ENV + +You'll need to explicitly set your `NODE_ENV` when in environments other than development or test. We like having a `.env` file in a serverfull production environment, and you just include: + +```bash +NODE_ENV=production +``` + +If you're using Docker, make sure you have an `ENV` declaration for it: + +```docker +ENV NODE_ENV="production" +``` + ## Advanced Job Workers As noted above, although the workers are started and detached using the `yarn rw jobs start` command, there is nothing to monitor those workers to make sure they keep running. To do that, you'll want to start the workers yourself (or have your process monitor start them) using command line flags. diff --git a/docs/versioned_docs/version-8.0/background-jobs.md b/docs/versioned_docs/version-8.0/background-jobs.md index e42e25c4f1ab..046bbc427d99 100644 --- a/docs/versioned_docs/version-8.0/background-jobs.md +++ b/docs/versioned_docs/version-8.0/background-jobs.md @@ -705,7 +705,7 @@ By checking the `lastError` field in the database you can see what the last erro ## Deployment -For many use cases you may simply be able to rely on the job runner to start your job workers, which will run forever: +For many use cases you may be able to rely on the job runner to start and detach your job workers, which will then run forever: ```bash yarn rw jobs start @@ -727,6 +727,20 @@ Of course if you have a process monitor system watching your workers you'll want ::: +### NODE_ENV + +You'll need to explicitly set your `NODE_ENV` when in environments other than development or test. We like having a `.env` file in a serverfull production environment, and you just include: + +```bash +NODE_ENV=production +``` + +If you're using Docker, make sure you have an `ENV` declaration for it: + +```docker +ENV NODE_ENV="production" +``` + ## Advanced Job Workers As noted above, although the workers are started and detached using the `yarn rw jobs start` command, there is nothing to monitor those workers to make sure they keep running. To do that, you'll want to start the workers yourself (or have your process monitor start them) using command line flags. diff --git a/docs/versioned_docs/version-8.1/background-jobs.md b/docs/versioned_docs/version-8.1/background-jobs.md index 3fe45fe202e1..9a545d9bf2d4 100644 --- a/docs/versioned_docs/version-8.1/background-jobs.md +++ b/docs/versioned_docs/version-8.1/background-jobs.md @@ -701,7 +701,7 @@ By checking the `lastError` field in the database you can see what the last erro ## Deployment -For many use cases you may simply be able to rely on the job runner to start your job workers, which will run forever: +For many use cases you may be able to rely on the job runner to start and detach your job workers, which will then run forever: ```bash yarn rw jobs start @@ -723,6 +723,20 @@ Of course if you have a process monitor system watching your workers you'll want ::: +### NODE_ENV + +You'll need to explicitly set your `NODE_ENV` when in environments other than development or test. We like having a `.env` file in a serverfull production environment, and you just include: + +```bash +NODE_ENV=production +``` + +If you're using Docker, make sure you have an `ENV` declaration for it: + +```docker +ENV NODE_ENV=production +``` + ## Advanced Job Workers As noted above, although the workers are started and detached using the `yarn rw jobs start` command, there is nothing to monitor those workers to make sure they keep running. To do that, you'll want to start the workers yourself (or have your process monitor start them) using command line flags. diff --git a/packages/jobs/src/__tests__/setupEnv.test.ts b/packages/jobs/src/__tests__/setupEnv.test.ts new file mode 100644 index 000000000000..7644afb41098 --- /dev/null +++ b/packages/jobs/src/__tests__/setupEnv.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it, vi, afterEach } from 'vitest' + +import { setupEnv } from '../setupEnv' + +vi.mock('@redwoodjs/cli-helpers/loadEnvFiles', () => { + return { + loadEnvFiles: () => {}, + } +}) + +const ORIGNAL_NODE_ENV = process.env.NODE_ENV + +describe('setupEnv', () => { + afterEach(() => { + process.env.NODE_ENV = ORIGNAL_NODE_ENV + }) + + it('if not called, NODE_ENV is not overridden in any way', () => { + expect(process.env.NODE_ENV).toEqual(ORIGNAL_NODE_ENV) + }) + + it('sets NODE_ENV to development if it starts undefined', () => { + delete process.env.NODE_ENV + + setupEnv() + + expect(process.env.NODE_ENV).toEqual('development') + }) +}) diff --git a/packages/jobs/src/bins/rw-jobs-worker.ts b/packages/jobs/src/bins/rw-jobs-worker.ts index f313ef5890e2..6c297c44fcf4 100755 --- a/packages/jobs/src/bins/rw-jobs-worker.ts +++ b/packages/jobs/src/bins/rw-jobs-worker.ts @@ -8,14 +8,13 @@ import process from 'node:process' import { hideBin } from 'yargs/helpers' import yargs from 'yargs/yargs' -import { loadEnvFiles } from '@redwoodjs/cli-helpers/loadEnvFiles' - import { PROCESS_TITLE_PREFIX } from '../consts.js' import type { Worker } from '../core/Worker.js' import { WorkerConfigIndexNotFoundError } from '../errors.js' import { loadJobsManager } from '../loaders.js' +import { setupEnv } from '../setupEnv.js' -loadEnvFiles() +setupEnv() const parseArgs = (argv: string[]) => { return yargs(hideBin(argv)) diff --git a/packages/jobs/src/bins/rw-jobs.ts b/packages/jobs/src/bins/rw-jobs.ts index ba13ab702655..cccd897a1ced 100755 --- a/packages/jobs/src/bins/rw-jobs.ts +++ b/packages/jobs/src/bins/rw-jobs.ts @@ -13,10 +13,9 @@ import { setTimeout } from 'node:timers' import { hideBin } from 'yargs/helpers' import yargs from 'yargs/yargs' -import { loadEnvFiles } from '@redwoodjs/cli-helpers/loadEnvFiles' - import { DEFAULT_LOGGER, PROCESS_TITLE_PREFIX } from '../consts.js' import { loadJobsManager } from '../loaders.js' +import { setupEnv } from '../setupEnv.js' import type { Adapters, BasicLogger, @@ -26,7 +25,7 @@ import type { export type NumWorkersConfig = [number, number][] -loadEnvFiles() +setupEnv() process.title = 'rw-jobs' diff --git a/packages/jobs/src/setupEnv.ts b/packages/jobs/src/setupEnv.ts new file mode 100644 index 000000000000..46fd8ef1e71a --- /dev/null +++ b/packages/jobs/src/setupEnv.ts @@ -0,0 +1,11 @@ +import { loadEnvFiles } from '@redwoodjs/cli-helpers/loadEnvFiles' + +export const setupEnv = () => { + loadEnvFiles() + + // If even after loading `.env` we find that `NODE_ENV` is `undefined` default + // to `development` to mimic what the other CLI tools to + if (process.env.NODE_ENV === undefined) { + process.env.NODE_ENV = 'development' + } +}