repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge branch 'master' of github.com:poolifier/poolifier
[poolifier.git]
/
src
/
pools
/
cluster
/
fixed.ts
diff --git
a/src/pools/cluster/fixed.ts
b/src/pools/cluster/fixed.ts
index e1caf252332f7d0103a77a9470b1526a62613e78..ed4f6fed2826078bfdeac466bcec7e8c4c80bef5 100644
(file)
--- a/
src/pools/cluster/fixed.ts
+++ b/
src/pools/cluster/fixed.ts
@@
-1,8
+1,9
@@
-import { fork, isMaster, setupMaster, Worker } from 'cluster'
+import type { ClusterSettings, Worker } from 'node:cluster'
+import cluster from 'node:cluster'
import type { MessageValue } from '../../utility-types'
import type { MessageValue } from '../../utility-types'
-import type { PoolOptions } from '../abstract-pool'
import { AbstractPool } from '../abstract-pool'
import { AbstractPool } from '../abstract-pool'
-import { PoolType } from '../pool-internal'
+import type { PoolOptions } from '../pool'
+import { PoolType } from '../pool'
/**
* Options for a poolifier cluster pool.
/**
* Options for a poolifier cluster pool.
@@
-15,6
+16,12
@@
export interface ClusterPoolOptions extends PoolOptions<Worker> {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
env?: any
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
env?: any
+ /**
+ * Cluster settings.
+ *
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
+ */
+ settings?: ClusterSettings
}
/**
}
/**
@@
-24,9
+31,8
@@
export interface ClusterPoolOptions extends PoolOptions<Worker> {
*
* This pool selects the workers in a round robin fashion.
*
*
* This pool selects the workers in a round robin fashion.
*
- * @template Data Type of data sent to the worker. This can only be serializable data.
- * @template Response Type of response of execution. This can only be serializable data.
- *
+ * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
@@
-37,9
+43,9
@@
export class FixedClusterPool<
/**
* Constructs a new poolifier fixed cluster pool.
*
/**
* Constructs a new poolifier fixed cluster pool.
*
- * @param numberOfWorkers Number of workers for this pool.
- * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
- * @param opts
Options for this fixed cluster pool. Default: `{}`
+ * @param numberOfWorkers
-
Number of workers for this pool.
+ * @param filePath
-
Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
+ * @param opts
- Options for this fixed cluster pool.
*/
public constructor (
numberOfWorkers: number,
*/
public constructor (
numberOfWorkers: number,
@@
-49,55
+55,58
@@
export class FixedClusterPool<
super(numberOfWorkers, filePath, opts)
}
super(numberOfWorkers, filePath, opts)
}
- /** @inherit
d
oc */
+ /** @inherit
D
oc */
protected setupHook (): void {
protected setupHook (): void {
- setupMaster({
- exec: this.filePath
- })
+ cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
}
}
- /** @inherit
d
oc */
+ /** @inherit
D
oc */
protected isMain (): boolean {
protected isMain (): boolean {
- return
isMaster
+ return
cluster.isPrimary
}
}
- /** @inherit
d
oc */
- p
ublic
destroyWorker (worker: Worker): void {
+ /** @inherit
D
oc */
+ p
rotected
destroyWorker (worker: Worker): void {
this.sendToWorker(worker, { kill: 1 })
worker.kill()
}
this.sendToWorker(worker, { kill: 1 })
worker.kill()
}
- /** @inherit
d
oc */
+ /** @inherit
D
oc */
protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
worker.send(message)
}
protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
worker.send(message)
}
- /** @inherit
d
oc */
- p
ublic registerWorkerMessageListener<Message extends Data | Response>
(
+ /** @inherit
D
oc */
+ p
rotected registerWorkerMessageListener<Message extends Data | Response>
(
worker: Worker,
listener: (message: MessageValue<Message>) => void
): void {
worker.on('message', listener)
}
worker: Worker,
listener: (message: MessageValue<Message>) => void
): void {
worker.on('message', listener)
}
- /** @inherit
d
oc */
+ /** @inherit
D
oc */
protected createWorker (): Worker {
protected createWorker (): Worker {
- return fork(this.opts.env)
+ return
cluster.
fork(this.opts.env)
}
}
- /** @inherit
d
oc */
+ /** @inherit
D
oc */
protected afterWorkerSetup (worker: Worker): void {
protected afterWorkerSetup (worker: Worker): void {
- // Listen worker messages.
+ // Listen
to
worker messages.
this.registerWorkerMessageListener(worker, super.workerListener())
}
this.registerWorkerMessageListener(worker, super.workerListener())
}
- /** @inherit
d
oc */
+ /** @inherit
D
oc */
public get type (): PoolType {
return PoolType.FIXED
}
public get type (): PoolType {
return PoolType.FIXED
}
- /** @inheritdoc */
- public get busy (): boolean {
- return this.internalGetBusyStatus()
+ /** @inheritDoc */
+ protected get full (): boolean {
+ return this.workerNodes.length === this.numberOfWorkers
+ }
+
+ /** @inheritDoc */
+ protected get busy (): boolean {
+ return this.internalBusy()
}
}
}
}