Skip to content

Commit 705ea5b

Browse files
authored
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
1 parent 29a3dda commit 705ea5b

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

.changesets/11572.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Default NODE_ENV to "development" if it's `undefined` when starting jobs worker (#11572) by @cannikin
2+
3+
This mimics the behavior of `yarn rw dev` where `NODE_ENV` will equal `development` if you don't set it explicitly.

docs/docs/background-jobs.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ By checking the `lastError` field in the database you can see what the last erro
701701
702702
## Deployment
703703
704-
For many use cases you may simply be able to rely on the job runner to start your job workers, which will run forever:
704+
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:
705705
706706
```bash
707707
yarn rw jobs start
@@ -723,6 +723,20 @@ Of course if you have a process monitor system watching your workers you'll want
723723
724724
:::
725725
726+
### NODE_ENV
727+
728+
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:
729+
730+
```bash
731+
NODE_ENV=production
732+
```
733+
734+
If you're using Docker, make sure you have an `ENV` declaration for it:
735+
736+
```docker
737+
ENV NODE_ENV="production"
738+
```
739+
726740
## Advanced Job Workers
727741
728742
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.

docs/versioned_docs/version-8.0/background-jobs.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ By checking the `lastError` field in the database you can see what the last erro
705705
706706
## Deployment
707707
708-
For many use cases you may simply be able to rely on the job runner to start your job workers, which will run forever:
708+
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:
709709
710710
```bash
711711
yarn rw jobs start
@@ -727,6 +727,20 @@ Of course if you have a process monitor system watching your workers you'll want
727727
728728
:::
729729
730+
### NODE_ENV
731+
732+
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:
733+
734+
```bash
735+
NODE_ENV=production
736+
```
737+
738+
If you're using Docker, make sure you have an `ENV` declaration for it:
739+
740+
```docker
741+
ENV NODE_ENV="production"
742+
```
743+
730744
## Advanced Job Workers
731745
732746
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.

docs/versioned_docs/version-8.1/background-jobs.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ By checking the `lastError` field in the database you can see what the last erro
701701
702702
## Deployment
703703
704-
For many use cases you may simply be able to rely on the job runner to start your job workers, which will run forever:
704+
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:
705705
706706
```bash
707707
yarn rw jobs start
@@ -723,6 +723,20 @@ Of course if you have a process monitor system watching your workers you'll want
723723
724724
:::
725725
726+
### NODE_ENV
727+
728+
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:
729+
730+
```bash
731+
NODE_ENV=production
732+
```
733+
734+
If you're using Docker, make sure you have an `ENV` declaration for it:
735+
736+
```docker
737+
ENV NODE_ENV=production
738+
```
739+
726740
## Advanced Job Workers
727741
728742
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.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it, vi, afterEach } from 'vitest'
2+
3+
import { setupEnv } from '../setupEnv'
4+
5+
vi.mock('@redwoodjs/cli-helpers/loadEnvFiles', () => {
6+
return {
7+
loadEnvFiles: () => {},
8+
}
9+
})
10+
11+
const ORIGNAL_NODE_ENV = process.env.NODE_ENV
12+
13+
describe('setupEnv', () => {
14+
afterEach(() => {
15+
process.env.NODE_ENV = ORIGNAL_NODE_ENV
16+
})
17+
18+
it('if not called, NODE_ENV is not overridden in any way', () => {
19+
expect(process.env.NODE_ENV).toEqual(ORIGNAL_NODE_ENV)
20+
})
21+
22+
it('sets NODE_ENV to development if it starts undefined', () => {
23+
delete process.env.NODE_ENV
24+
25+
setupEnv()
26+
27+
expect(process.env.NODE_ENV).toEqual('development')
28+
})
29+
})

packages/jobs/src/bins/rw-jobs-worker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import process from 'node:process'
88
import { hideBin } from 'yargs/helpers'
99
import yargs from 'yargs/yargs'
1010

11-
import { loadEnvFiles } from '@redwoodjs/cli-helpers/loadEnvFiles'
12-
1311
import { PROCESS_TITLE_PREFIX } from '../consts.js'
1412
import type { Worker } from '../core/Worker.js'
1513
import { WorkerConfigIndexNotFoundError } from '../errors.js'
1614
import { loadJobsManager } from '../loaders.js'
15+
import { setupEnv } from '../setupEnv.js'
1716

18-
loadEnvFiles()
17+
setupEnv()
1918

2019
const parseArgs = (argv: string[]) => {
2120
return yargs(hideBin(argv))

packages/jobs/src/bins/rw-jobs.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import { setTimeout } from 'node:timers'
1313
import { hideBin } from 'yargs/helpers'
1414
import yargs from 'yargs/yargs'
1515

16-
import { loadEnvFiles } from '@redwoodjs/cli-helpers/loadEnvFiles'
17-
1816
import { DEFAULT_LOGGER, PROCESS_TITLE_PREFIX } from '../consts.js'
1917
import { loadJobsManager } from '../loaders.js'
18+
import { setupEnv } from '../setupEnv.js'
2019
import type {
2120
Adapters,
2221
BasicLogger,
@@ -26,7 +25,7 @@ import type {
2625

2726
export type NumWorkersConfig = [number, number][]
2827

29-
loadEnvFiles()
28+
setupEnv()
3029

3130
process.title = 'rw-jobs'
3231

packages/jobs/src/setupEnv.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { loadEnvFiles } from '@redwoodjs/cli-helpers/loadEnvFiles'
2+
3+
export const setupEnv = () => {
4+
loadEnvFiles()
5+
6+
// If even after loading `.env` we find that `NODE_ENV` is `undefined` default
7+
// to `development` to mimic what the other CLI tools to
8+
if (process.env.NODE_ENV === undefined) {
9+
process.env.NODE_ENV = 'development'
10+
}
11+
}

0 commit comments

Comments
 (0)