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

[Bug?]: (Jobs) Environment is not correctly set in the Job worker #11569

Closed
1 task
dthyresson opened this issue Sep 15, 2024 · 2 comments · Fixed by #11572
Closed
1 task

[Bug?]: (Jobs) Environment is not correctly set in the Job worker #11569

dthyresson opened this issue Sep 15, 2024 · 2 comments · Fixed by #11572
Assignees
Labels
bug/confirmed We have confirmed this is a bug topic/jobs
Milestone

Comments

@dthyresson
Copy link
Contributor

dthyresson commented Sep 15, 2024

What's not working?

Demonstrate that the environment is not correctly set in the job worker.

Knowing that the environment is correctly set the job will let a developer use different storage adapters per environment. For example, in the development environment, the job will use the file storage adapter, but in the production environment, the job will use the s3 storage adapter.

How do we reproduce the bug?

Steps to reproduce

See: https://github.com/dthyresson/jobs-environment for an example app

  1. Run yarn dev
  2. Start the jobs by running yarn jobs work
  3. Open the browser and navigate to http://localhost:8910/graphql
  4. Run the following query:
query TestJobsEnvironment {
  logEnvironment
}

Can see that the environment is correctly set in the service to development when yarn rw dev is running.

api | 11:51:33 🐛 Processing GraphQL Parameters
api | 11:51:33 🐛 graphql-server GraphQL execution started: TestJobsEnvironment
api | 11:51:33 🌲 The environment in the service
api | 🗒 Custom
api | {
api |   "environment": "development"
api | }
api | 11:51:33 🌲 LogEnvironmentJob [RedwoodJob] Scheduling LogEnvironmentJob
api | 🗒 Custom
api | {
api |   "path": "LogEnvironmentJob/LogEnvironmentJob",
api |   "args": [],
api |   "runAt": "2024-09-15T15:51:33.280Z",
api |   "queue": "default",
api |   "priority": 50
api | }
api | 11:51:33 🐛 graphql-server GraphQL execution completed: TestJobsEnvironment
api | 11:51:33 🐛 Processing GraphQL Parameters done.

However, when the jobs are running, the environment is undefined.

api | 11:52:03 🐛 Processing GraphQL Parameters
11:51:32 🐛 [rw-jobs-worker.*.0] Checking for jobs in all (*) queues...
11:51:32 🌲 [RedwoodJob] Started job 8 (LogEnvironmentJob/LogEnvironmentJob:LogEnvironmentJob)
11:51:32 🌲 LogEnvironmentJob is performing...
11:51:32 🌲 The environment in the service
11:51:32 🚦  LogEnvironmentJob is undefined
11:51:32 🌲 LogEnvironmentJob is done
11:51:32 🐛 [RedwoodJob] Job 8 success

However, if you run the jobs with NODE_ENV=development yarn rw jobs work the environment is correctly set to development.

11:53:51 🐛 [rw-jobs-worker.*.0] Checking for jobs in all (*) queues...
11:53:51 🌲 [RedwoodJob] Started job 10 (LogEnvironmentJob/LogEnvironmentJob:LogEnvironmentJob)
11:53:51 🌲 LogEnvironmentJob is performing...
🗒 Custom
{
  "environment": "development"
}
11:53:51 🌲 LogEnvironmentJob is development
11:53:51 🌲 LogEnvironmentJob is done
11:53:51 🐛 [RedwoodJob] Job 10 success
11:53:51 🐛 [rw-jobs-worker.*.0] Checking for jobs in all (*) queues...

Note:

There is checking in the jobs cli commands for the environment to add the logger formatter, but that must > have a different way of getting the environment.

What's your environment? (If it applies)

No response

Are you interested in working on this?

  • I'm interested in working on this
@dthyresson dthyresson added bug/confirmed We have confirmed this is a bug topic/jobs labels Sep 15, 2024
@dthyresson dthyresson changed the title [Bug?]: environment is not correctly set in the job worker [Bug?]: (Jobs) Environment is not correctly set in the Job worker Sep 15, 2024
@cannikin
Copy link
Member

Hmm, the log statement in both the service and the job is 'The environment in the service' so I'm slight confused which one is showing in the log snippets! To be clear, the job isn't calling the service, you're just logging the ENV in the job itself, looks like.

Jobs will load your .env when you yarn rw jobs work so if you have NODE_ENV defined there, it'll use it. But I don't do any special overriding that says something like if NODE_ENV is undefined, set it to development which is what we tend to do in other places in the framework. But I guess we should be doing that here to have parity with at least the yarn rw dev command. I'll get into it on Monday!

@cannikin cannikin added this to the next-release milestone Sep 16, 2024
@dthyresson
Copy link
Contributor Author

dthyresson commented Sep 16, 2024

the log statement in both the service and the job is 'The environment in the service'

Ah, my bad copy/paste on:

https://github.com/dthyresson/jobs-environment/blob/09a4181cae13d409edfca3c91ad91b01103a8d2e/api/src/jobs/LogEnvironmentJob/LogEnvironmentJob.ts#L9

The job itself reports:

11:51:32 🌲 [RedwoodJob] Started job 8 (LogEnvironmentJob/LogEnvironmentJob:LogEnvironmentJob)
11:51:32 🌲 LogEnvironmentJob is performing...
11:51:32 🌲 The environment in the service <---- this is really the job
11:51:32 🚦  LogEnvironmentJob is undefined
11:51:32 🌲 LogEnvironmentJob is done

After my typo fix with just yarn rw dev:

02:11:19 🐛 [rw-jobs-worker.*.0] Checking for jobs in all (*) queues...
02:11:19 🌲 [RedwoodJob] Started job 11 (LogEnvironmentJob/LogEnvironmentJob:LogEnvironmentJob)
02:11:19 🌲 LogEnvironmentJob is performing...
02:11:19 🌲 The environment in the job
02:11:19 🚦  LogEnvironmentJob is undefined in job
02:11:19 🌲 LogEnvironmentJob is done
02:11:19 🐛 [RedwoodJob] Job 11 success
02:11:19 🐛 [rw-jobs-worker.*.0] Checking for jobs in all (*) queues...
02:11:24 🐛 [rw-jobs-worker.*.0] Checking for jobs in all (*) queues...

Have updated reproduction: https://github.com/dthyresson/jobs-environment

cannikin added a commit that referenced this issue Sep 16, 2024
Josh-Walker-GM pushed a commit that referenced this issue Sep 19, 2024
…obs 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/confirmed We have confirmed this is a bug topic/jobs
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants