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

Detect if code is running in a Bull process or not? #1951

Open
cjdell opened this issue Jan 12, 2021 · 7 comments
Open

Detect if code is running in a Bull process or not? #1951

cjdell opened this issue Jan 12, 2021 · 7 comments

Comments

@cjdell
Copy link

cjdell commented Jan 12, 2021

Is there a reliable way to detect whether the code is running in the main thread or in a Bull process?

I want to use this to avoid making unnecessary requires when the code is running in Bull to keep the process size down.

Thanks

@cjdell
Copy link
Author

cjdell commented Jan 12, 2021

This is what I've gone with for now:

/** Is this code executing in a Bull process? */
export const isBullProcess = process.argv.some(a => a.includes('bull'))

@manast
Copy link
Member

manast commented Jan 13, 2021

I am not really sure what you are trying to figure out. Bull does only spawn new processes if you put your process in a separate file as explained here: https://github.com/optimalbits/bull#separate-processes

@gabegorelick
Copy link
Contributor

I think the request is for something like a BULL_SANDBOX_PROCESS=1 environment variable so that arbitrary code can easily tell whether they're running in a sandbox or not.

Based on how Bull forks sandbox processes today, I think all you can do is check argv similar to what's described in #1951 (comment). But this isn't as reliable or stable as something like an env variable.

child = fork(path.join(__dirname, './master.js'), {
execArgv
});

@manast
Copy link
Member

manast commented Jan 14, 2021

I think the request is for something like a BULL_SANDBOX_PROCESS=1 environment variable so that arbitrary code can easily tell whether they're running in a sandbox or not.

@gabegorelick but that can easily be added in the processor file, just set a global variable or similar...

@gabegorelick
Copy link
Contributor

but that can easily be added in the processor file, just set a global variable or similar...

I don't want to speak for what @cjdell needs, but that means you processor file can't be used when not running in a sandbox.

Take the processor example from the README:

module.exports = function(job){
  // Do some heavy work

  return Promise.resolve(result);
}

When using a sandbox process, I can call queue.process('/path/to/my/processor.js'); But what if I want to reuse the same processing logic without using a separate process? That would look something like

queue.process(require('/path/to/my/processor.js'));

But if the processor needs to be aware of whether it's running in sandbox mode, then you'd need to add some indirection like so:

// jobProcessor.js
module.exports = function(job){
  if (job.isSandboxed) { ... }

  return Promise.resolve(result);
}

// sandbox.js
module.exports = function(job){
  job.isSandboxed = true;  // or alternatively, set a global variable
  return require('./jobProcessor')(job);
}

Then the queue processing code can decide whether to use jobProcessor.js directly or via sandbox.js. But you'd only need 1 of those if Bull provided a standard way to see if you were sandboxed.

Anyway, I'll shut up now and let others provide real use cases :)

@bkniffler
Copy link

My use case would be using the same entry file in cases where you'd build your scripts with something like webpack. Ofc you can have your webpack setup create multiple bundles (index.js, worker.js). Instead though, we could only have 1 bundle (index.js) and let that file decide what to do using an environment var (IS_BULL_SANDBOXED=true).

// index.js
if (process.env.IS_BULL_SANDBOXED) {
   module.exports = function(job) { .... }
} else {
   runMainCode();
}

@stale
Copy link

stale bot commented Jul 12, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants