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

Added default value to maximumUsage in Scheduler #507

Conversation

BenzeneAlcohol
Copy link

Added default value to maximumUsage

Fixes: #497

Something to do:

What if maximumUsage > os.availableParallelism() ?

Added default value to maximumUsage

Fixes: piscinajs#497
@BenzeneAlcohol BenzeneAlcohol changed the title src: scheduler Added default value to maximumUsage in Scheduler Feb 6, 2024
@BenzeneAlcohol
Copy link
Author

Also, os.availableParallelism() is available only on node versions > v18.14.0

@@ -66,10 +67,14 @@ class DefaultTaskScheduler extends TaskScheduler {
#maximumUsage: number;
#onAvailableListeners: ((item: PiscinaWorker) => void)[];

constructor (maximumUsage: number) {
constructor (maximumUsage: number = os.availableParallelism()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we do not have the matrix anymore, we still officially support v16; so feel free to do a check over the Node version running and only get the value if the os.availableParallelism is available.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in case the version is less than 16, what should this default to? There is os.cpus() that almost does the same, do we default to the length of that, or some other number?

super(maximumUsage);
this.#maximumUsage = maximumUsage;
this.#onAvailableListeners = [];

if (maximumUsage > os.availableParallelism()) {
console.warn(`Warning: maximumUsage (${maximumUsage}) is greater than available CPU cores (${os.availableParallelism()}).`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to use https://www.npmjs.com/package/process-warning instead, has better ergonomics for these kind of situations

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

@metcoder95
Copy link
Member

Let's also add a note in the documentation about this 👍

@metcoder95
Copy link
Member

Oh, my bad, I didn't notice you were working on top of my PR; let's aim first for the current version and later we can port it to the PR 👍

So, for the current version, we should seek to use it whenever we want to get the max amount of threads if this option has not been passed through the constructor; so we can change it here:

const cpuCount : number = (() => {

If the function is not available because it's being run under Node v16, let's keep the cpus.length.

The maximumUsage, is calculated based on the concurrentTasksPerWorker, which should be kept as default to the same number as os.availableParallelism() or cpu.length.

The warning, let's keep it but just port it to how is used within the current version of Piscina 👍

@BenzeneAlcohol
Copy link
Author

Oh, my bad, I didn't notice you were working on top of my PR; let's aim first for the current version and later we can port it to the PR 👍

So, for the current version, we should seek to use it whenever we want to get the max amount of threads if this option has not been passed through the constructor; so we can change it here:

const cpuCount : number = (() => {

If the function is not available because it's being run under Node v16, let's keep the cpus.length.

The maximumUsage, is calculated based on the concurrentTasksPerWorker, which should be kept as default to the same number as os.availableParallelism() or cpu.length.

The warning, let's keep it but just port it to how is used within the current version of Piscina 👍

Right, got it!

Copy link

github-actions bot commented May 5, 2024

This issue has been marked as stale because it has been opened 45 days without activity. Remove stale label or comment or this will be closed in 10 days.

@github-actions github-actions bot added the stale label May 5, 2024
Copy link

This PR was closed because it has been stalled for 10 days with no activity.

@github-actions github-actions bot closed this May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants