Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit ed430f3

Browse files
author
exoego
committed
Overhaul cluster module
1 parent d0a46ce commit ed430f3

File tree

10 files changed

+82
-58
lines changed

10 files changed

+82
-58
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.scalajs.nodejs
2+
3+
import scala.scalajs.js
4+
5+
trait HasFileDescriptor extends js.Object {
6+
def fd: FileDescriptor
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.scalajs.nodejs
2+
3+
import scala.scalajs.js
4+
5+
trait HasHandle extends js.Object {
6+
def _handle: js.Any
7+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package io.scalajs.nodejs.cluster
22

33
import scala.scalajs.js
4+
import scala.scalajs.js.|
45

5-
/**
6-
* Address
7-
*/
86
@js.native
97
trait Address extends js.Object {
108

119
def address: String = js.native
1210

13-
def port: Integer = js.native
11+
def port: Int = js.native
1412

1513
/**
1614
* The addressType is one of:
@@ -19,6 +17,6 @@ trait Address extends js.Object {
1917
* -1 (unix domain socket)
2018
* "udp4" or "udp6" (UDP v4 or v6)
2119
*/
22-
def addressType: js.Any = js.native
20+
def addressType: String | Int = js.native
2321

2422
}

app/current/src/main/scala/io/scalajs/nodejs/cluster/Cluster.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ trait Cluster extends IEventEmitter {
8585
* This can only be called from the master process.
8686
* @example cluster.disconnect([callback])
8787
*/
88-
def disconnect(callback: js.Function = null): Unit = js.native
88+
def disconnect(callback: js.Function = js.native): Unit = js.native
8989

9090
/**
9191
* Spawn a new worker process.
9292
* This can only be called from the master process.
9393
* @return a new worker
9494
* @example cluster.fork([env])
9595
*/
96-
def fork(env: js.Any = null): Worker = js.native
96+
def fork(env: js.Any = js.native): Worker = js.native
9797

9898
/**
9999
* setupMaster is used to change the default 'fork' behavior. Once called, the settings will be present in cluster.settings.
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package io.scalajs.nodejs.cluster
22

3+
import com.thoughtworks.enableIf
4+
import io.scalajs.JsNumber
5+
import io.scalajs.nodejs.{GID, UID}
36
import io.scalajs.util.ScalaJsHelper._
47

58
import scala.scalajs.js
9+
import scala.scalajs.js.|
610

711
/**
812
* Cluster Settings
@@ -11,35 +15,55 @@ import scala.scalajs.js
1115
trait ClusterSettings extends js.Object {
1216

1317
/** <Array> list of string arguments passed to the Node.js executable. (Default=process.execArgv) */
14-
var execArgv: js.Array[js.Any] = js.native
18+
var execArgv: js.Array[String] = js.native
1519

1620
/** <String> file path to worker file. (Default=process.argv[1]) */
1721
var exec: String = js.native
1822

1923
/** <Array> string arguments passed to worker. (Default=process.argv.slice(2)) */
20-
var args: js.Array[js.Any] = js.native
24+
var args: js.Array[String] = js.native
2125

2226
/** <Boolean> whether or not to send output to parent's stdio. (Default=false) */
2327
var silent: Boolean = js.native
2428

2529
/** <Number> Sets the user identity of the process. (See setuid(2).) */
26-
var uid: Integer = js.native
30+
var uid: UID = js.native
2731

28-
/* <Number> Sets the group identity of the process. (See setgid(2).) */
29-
var gid: Integer = js.native
32+
/** <Number> Sets the group identity of the process. (See setgid(2).) */
33+
var gid: GID = js.native
34+
35+
var stdio: js.Array[js.Any] = js.native
36+
37+
var inspectPort: JsNumber | js.Function = js.native
38+
39+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
40+
var cwd: String = js.native
41+
42+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
43+
var windowsHide: Boolean = js.native
3044
}
3145

3246
/**
3347
* Cluster Settings Companion
3448
*/
3549
object ClusterSettings {
36-
37-
def apply(exec: String = null, args: js.Array[js.Any] = null, silent: Boolean = false): ClusterSettings = {
38-
val settings = New[ClusterSettings]
50+
def apply(execArgv: js.Array[String] = null,
51+
exec: String = null,
52+
args: js.Array[String] = null,
53+
silent: Boolean = false,
54+
stdio: js.Array[js.Any] = null,
55+
inspectPort: JsNumber | js.Function = null,
56+
cwd: String = null,
57+
windowsHide: Boolean = false): ClusterSettings = {
58+
val settings = js.Dynamic.literal()
59+
settings.execArgv = execArgv
3960
settings.exec = exec
4061
settings.args = args
4162
settings.silent = silent
42-
settings
63+
settings.stdio = stdio
64+
settings.inspectPort = inspectPort
65+
settings.cwd = cwd
66+
settings.windowsHide = windowsHide
67+
settings.asInstanceOf[ClusterSettings]
4368
}
44-
4569
}

app/current/src/main/scala/io/scalajs/nodejs/cluster/Worker.scala

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.scalajs.nodejs.cluster
22

3-
import io.scalajs.nodejs.Process
3+
import io.scalajs.nodejs.Handle
4+
import io.scalajs.nodejs.child_process.ChildProcess
45
import io.scalajs.nodejs.events.IEventEmitter
56

67
import scala.scalajs.js
@@ -22,13 +23,13 @@ trait Worker extends IEventEmitter {
2223
* The boolean worker.exitedAfterDisconnect lets you distinguish between voluntary and accidental exit, the master
2324
* may choose not to respawn a worker based on this value.
2425
*/
25-
def exitedAfterDisconnect: Boolean = js.native
26+
def exitedAfterDisconnect: js.UndefOr[Boolean] = js.native
2627

2728
/**
2829
* Each new worker is given its own unique id, this id is stored in the id.
2930
* While a worker is alive, this is the key that indexes it in cluster.workers
3031
*/
31-
def id: Integer = js.native
32+
def id: Int = js.native
3233

3334
/**
3435
* All workers are created using child_process.fork(), the returned object from this function is stored as .process.
@@ -37,7 +38,7 @@ trait Worker extends IEventEmitter {
3738
* Note that workers will call process.exit(0) if the 'disconnect' event occurs on process and .exitedAfterDisconnect
3839
* is not true. This protects against accidental disconnection.
3940
*/
40-
def process: Process = js.native
41+
def process: ChildProcess = js.native
4142

4243
/**
4344
* An alias to worker.exitedAfterDisconnect.
@@ -100,30 +101,14 @@ trait Worker extends IEventEmitter {
100101
* @param signal the name of the kill signal to send to the worker process.
101102
* @example kill([signal='SIGTERM'])
102103
*/
103-
def kill(signal: String = null): Unit = js.native
104+
def kill(signal: String = js.native): Unit = js.native
104105

105106
/**
106107
* Send a message to a worker or master, optionally with a handle.
107108
* In the master this sends a message to a specific worker. It is identical to ChildProcess.send().
108109
* In a worker this sends a message to the master. It is identical to process.send().
109110
* @example worker.send(message[, sendHandle][, callback])
110111
*/
111-
def send(message: Message, sendHandle: js.Function, callback: js.Function): Unit = js.native
112-
113-
/**
114-
* Send a message to a worker or master, optionally with a handle.
115-
* In the master this sends a message to a specific worker. It is identical to ChildProcess.send().
116-
* In a worker this sends a message to the master. It is identical to process.send().
117-
* @example worker.send(message[, sendHandle][, callback])
118-
*/
119-
def send(message: Message, callback: js.Function): Unit = js.native
120-
121-
/**
122-
* Send a message to a worker or master, optionally with a handle.
123-
* In the master this sends a message to a specific worker. It is identical to ChildProcess.send().
124-
* In a worker this sends a message to the master. It is identical to process.send().
125-
* @example worker.send(message[, sendHandle][, callback])
126-
*/
127-
def send(message: Message): Unit = js.native
112+
def send(message: Message, sendHandle: Handle = js.native, callback: js.Function = js.native): Unit = js.native
128113

129114
}

app/current/src/main/scala/io/scalajs/nodejs/cluster/package.scala

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.scalajs.nodejs
22

33
import io.scalajs.nodejs
4+
import io.scalajs.nodejs.cluster.Worker
45
import io.scalajs.util.PromiseHelper._
56

67
import scala.concurrent.Future
@@ -21,7 +22,7 @@ package object cluster {
2122
* Cluster Events
2223
* @param cluster the given [[Cluster cluster]]
2324
*/
24-
implicit class ClusterEvents(val cluster: Cluster) extends AnyVal {
25+
implicit final class ClusterEvents(private val cluster: Cluster) extends AnyVal {
2526

2627
/**
2728
* Emitted after the worker IPC channel has disconnected. This can occur when a worker exits gracefully, is killed,
@@ -32,7 +33,7 @@ package object cluster {
3233
* @param callback the event handler
3334
*/
3435
@inline
35-
def onDisconnect(callback: Worker => Any): cluster.type = cluster.on("disconnect", callback)
36+
def onDisconnect(callback: Worker => Any): Cluster = cluster.on("disconnect", callback)
3637

3738
/**
3839
* Similar to the cluster.on('exit') event, but specific to this worker.
@@ -44,15 +45,15 @@ package object cluster {
4445
* </ul>
4546
*/
4647
@inline
47-
def onExit(callback: (Worker, Int, String) => Any): cluster.type = cluster.on("exit", callback)
48+
def onExit(callback: (Worker, Int, String) => Any): Cluster = cluster.on("exit", callback)
4849

4950
/**
5051
* When a new worker is forked the cluster module will emit a 'fork' event. This can be used to log worker activity,
5152
* and create your own timeout.
5253
* @param callback the event handler
5354
*/
5455
@inline
55-
def onFork(callback: Worker => Any): cluster.type = cluster.on("fork", callback)
56+
def onFork(callback: Worker => Any): Cluster = cluster.on("fork", callback)
5657

5758
/**
5859
* After calling listen() from a worker, when the 'listening' event is emitted on the server, a 'listening' event
@@ -64,7 +65,7 @@ package object cluster {
6465
* @param callback the event handler
6566
*/
6667
@inline
67-
def onListening(callback: (Worker, Address) => Any): cluster.type = cluster.on("listening", callback)
68+
def onListening(callback: (Worker, Address) => Any): Cluster = cluster.on("listening", callback)
6869

6970
/**
7071
* Emitted when any worker receives a message.
@@ -75,7 +76,7 @@ package object cluster {
7576
* @param callback the event handler
7677
*/
7778
@inline
78-
def onMessage(callback: (Worker, Message, js.Any) => Any): cluster.type = cluster.on("message", callback)
79+
def onMessage(callback: (Worker, Message, js.Any) => Any): Cluster = cluster.on("message", callback)
7980

8081
/**
8182
* After forking a new worker, the worker should respond with an online message. When the master receives an online
@@ -84,7 +85,7 @@ package object cluster {
8485
* @param callback the event handler
8586
*/
8687
@inline
87-
def onOnline(callback: Worker => Any): cluster.type = cluster.on("online", callback)
88+
def onOnline(callback: Worker => Any): Cluster = cluster.on("online", callback)
8889

8990
/**
9091
* Emitted every time .setupMaster() is called.
@@ -95,7 +96,7 @@ package object cluster {
9596
* @param callback the event handler
9697
*/
9798
@inline
98-
def onSetup(callback: ClusterSettings => Any): cluster.type = cluster.on("setup", callback)
99+
def onSetup(callback: ClusterSettings => Any): Cluster = cluster.on("setup", callback)
99100

100101
}
101102

@@ -107,7 +108,7 @@ package object cluster {
107108
* Worker Events and Extensions
108109
* @param worker the given [[Worker worker]]
109110
*/
110-
implicit class WorkerEvents(val worker: Worker) extends AnyVal {
111+
implicit final class WorkerEvents(private val worker: Worker) extends AnyVal {
111112

112113
/////////////////////////////////////////////////////////////////////////////////
113114
// Worker Extensions
@@ -131,15 +132,15 @@ package object cluster {
131132
* @param callback the event handler
132133
*/
133134
@inline
134-
def onDisconnect(callback: () => Any): worker.type = worker.on("disconnect", callback)
135+
def onDisconnect(callback: () => Any): Worker = worker.on("disconnect", callback)
135136

136137
/**
137138
* This event is the same as the one provided by child_process.fork().
138139
* In a worker you can also use process.on('error').
139140
* @param callback the error handler
140141
*/
141142
@inline
142-
def onError(callback: nodejs.Error => Any): worker.type = worker.on("error", callback)
143+
def onError(callback: nodejs.Error => Any): Worker = worker.on("error", callback)
143144

144145
/**
145146
* Similar to the cluster.on('exit') event, but specific to this worker.
@@ -151,14 +152,14 @@ package object cluster {
151152
* </ul>
152153
*/
153154
@inline
154-
def onExit(callback: (Worker, Int, String) => Any): worker.type = worker.on("exit", callback)
155+
def onExit(callback: (Worker, Int, String) => Any): Worker = worker.on("exit", callback)
155156

156157
/**
157158
* Similar to the cluster.on('listening') event, but specific to this worker.
158159
* @param callback the event handler
159160
*/
160161
@inline
161-
def onListening(callback: Address => Any): worker.type = worker.on("listening", callback)
162+
def onListening(callback: Address => Any): Worker = worker.on("listening", callback)
162163

163164
/**
164165
* Similar to the cluster.on('message') event, but specific to this worker.
@@ -167,14 +168,14 @@ package object cluster {
167168
* @param callback the event handler
168169
*/
169170
@inline
170-
def onMessage(callback: Message => Any): worker.type = worker.on("message", callback)
171+
def onMessage(callback: Message => Any): Worker = worker.on("message", callback)
171172

172173
/**
173174
* Similar to the cluster.on('online') event, but specific to this worker.
174175
* @param callback the event handler
175176
*/
176177
@inline
177-
def onOnline(callback: () => Any): worker.type = worker.on("online", callback)
178+
def onOnline(callback: () => Any): Worker = worker.on("online", callback)
178179

179180
}
180181

app/current/src/main/scala/io/scalajs/nodejs/net/Socket.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import scala.scalajs.js.|
1515
*/
1616
@js.native
1717
@JSImport("net", "Socket")
18-
class Socket(options: SocketOptions | RawOptions = js.native) extends IDuplex {
18+
class Socket(options: SocketOptions | RawOptions = js.native) extends IDuplex with HasHandle {
1919

2020
/////////////////////////////////////////////////////////////////////////////////
2121
// Properties
@@ -218,6 +218,8 @@ class Socket(options: SocketOptions | RawOptions = js.native) extends IDuplex {
218218
*/
219219
def unref(): this.type = js.native
220220

221+
// TODO: test me
222+
override def _handle: js.Any = js.native
221223
}
222224

223225
/**

app/current/src/main/scala/io/scalajs/nodejs/package.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ package object nodejs {
4545

4646
type UID = Int
4747

48+
// The handle object can be either a server, a socket (anything with an underlying _handle member), or an object with an fd member that is a valid file descriptor.
49+
type Handle = js.Function | HasHandle | HasFileDescriptor
50+
4851
/////////////////////////////////////////////////////////////////////////////////
4952
// Built-in Properties
5053
/////////////////////////////////////////////////////////////////////////////////

core/src/main/scala/io/scalajs/util/ScalaJsHelper.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ object ScalaJsHelper {
2222
@inline
2323
def isDefined(obj: js.Any): Boolean = obj != null && !js.isUndefined(obj)
2424

25-
@inline
26-
def New[T <: js.Any]: T = new js.Object().asInstanceOf[T]
27-
2825
////////////////////////////////////////////////////////////////////////
2926
// Implicit Definitions and Classes
3027
////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)