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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { PiscinaWorker, PiscinaTask, RunTaskOptions } from '.';
import * as os from 'os';

function isTaskSchedulerLike (obj: {}): obj is TaskScheduler {
if (Object.getPrototypeOf(obj) === TaskScheduler) return true;
Expand Down Expand Up @@ -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.

}
}

add (item: PiscinaWorker) {
Expand Down
Loading