Skip to content

Add scheduling policy to cluster.settings

As reported in https://github.com/nodejs/node/issues/49240, when using ESM it is quite hard to modify cluster.schedulingPolicy, since the property becomes immutable on import and the environment variable cannot be set in code since the import is hoisted to the top. Setting the env variable in the console is often not desirable. The only workaround I found so far is to do a dynamic import() after setting the env variable in code, which is cumbersome.

The solution implemented here, as suggested by @bnoordhuis in a comment, is to add schedulingPolicy to the cluster.settings object:

import * as cluster from 'cluster';
cluster.setupPrimary({ schedulingPolicy: cluster.SCHED_NONE });

This code now works properly as expected.

@bnoordhuis also recommended that a cluster.getSettings() function was not desirable at this point; instead an internal method should be used. I'm not sure however how this internal method is marked, so I added it without documenting it for now.

Documented in doc/api/cluster.md. Test added in test/known_issues/test-cluster-import-scheduling-policy.mjs. All tests running.

Possible improvements:

  • Add test with CJS.
  • Document cluster.getSettings() so that it can be used in ESM, where cluster.settings will also be immutable on import and the user will not be able to tell what the final settings are.
  • Mark cluster.schedulingPolicy as deprecated since it is now redundant.

RFC, please review.

Fixes: https://github.com/nodejs/node/issues/49240

Merge request reports

Loading