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

Commit 1b3aaba

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

File tree

10 files changed

+84
-62
lines changed

10 files changed

+84
-62
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: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.scalajs.nodejs.cluster
22

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

57
import scala.scalajs.js
8+
import scala.scalajs.js.|
69

710
/**
811
* Cluster Settings
@@ -11,35 +14,55 @@ import scala.scalajs.js
1114
trait ClusterSettings extends js.Object {
1215

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

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

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

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

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

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

3245
/**
3346
* Cluster Settings Companion
3447
*/
3548
object ClusterSettings {
36-
37-
def apply(exec: String = null, args: js.Array[js.Any] = null, silent: Boolean = false): ClusterSettings = {
38-
val settings = New[ClusterSettings]
39-
settings.exec = exec
40-
settings.args = args
41-
settings.silent = silent
42-
settings
49+
def apply(execArgv: js.Array[String] = null,
50+
exec: String = null,
51+
args: js.Array[String] = null,
52+
silent: Boolean = false,
53+
stdio: js.Array[js.Any] = null,
54+
inspectPort: JsNumber | js.Function = null,
55+
cwd: String = null,
56+
windowsHide: Boolean = false): ClusterSettings = {
57+
val settings = js.Dynamic.literal()
58+
settings.updateDynamic("execArgv")(execArgv)
59+
settings.updateDynamic("exec")(exec)
60+
settings.updateDynamic("args")(args)
61+
settings.updateDynamic("silent")(silent)
62+
settings.updateDynamic("stdio")(stdio)
63+
settings.updateDynamic("inspectPort")(inspectPort.asInstanceOf[js.Any])
64+
settings.updateDynamic("cwd")(cwd)
65+
settings.updateDynamic("windowsHide")(windowsHide)
66+
settings.asInstanceOf[ClusterSettings]
4367
}
44-
4568
}

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: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package object cluster {
2121
* Cluster Events
2222
* @param cluster the given [[Cluster cluster]]
2323
*/
24-
implicit class ClusterEvents(val cluster: Cluster) extends AnyVal {
24+
implicit final class ClusterEvents(private val cluster: Cluster) extends AnyVal {
2525

2626
/**
2727
* Emitted after the worker IPC channel has disconnected. This can occur when a worker exits gracefully, is killed,
@@ -32,7 +32,7 @@ package object cluster {
3232
* @param callback the event handler
3333
*/
3434
@inline
35-
def onDisconnect(callback: Worker => Any): cluster.type = cluster.on("disconnect", callback)
35+
def onDisconnect(callback: Worker => Any): Cluster = cluster.on("disconnect", callback)
3636

3737
/**
3838
* Similar to the cluster.on('exit') event, but specific to this worker.
@@ -44,15 +44,15 @@ package object cluster {
4444
* </ul>
4545
*/
4646
@inline
47-
def onExit(callback: (Worker, Int, String) => Any): cluster.type = cluster.on("exit", callback)
47+
def onExit(callback: (Worker, Int, String) => Any): Cluster = cluster.on("exit", callback)
4848

4949
/**
5050
* When a new worker is forked the cluster module will emit a 'fork' event. This can be used to log worker activity,
5151
* and create your own timeout.
5252
* @param callback the event handler
5353
*/
5454
@inline
55-
def onFork(callback: Worker => Any): cluster.type = cluster.on("fork", callback)
55+
def onFork(callback: Worker => Any): Cluster = cluster.on("fork", callback)
5656

5757
/**
5858
* After calling listen() from a worker, when the 'listening' event is emitted on the server, a 'listening' event
@@ -64,7 +64,7 @@ package object cluster {
6464
* @param callback the event handler
6565
*/
6666
@inline
67-
def onListening(callback: (Worker, Address) => Any): cluster.type = cluster.on("listening", callback)
67+
def onListening(callback: (Worker, Address) => Any): Cluster = cluster.on("listening", callback)
6868

6969
/**
7070
* Emitted when any worker receives a message.
@@ -75,7 +75,7 @@ package object cluster {
7575
* @param callback the event handler
7676
*/
7777
@inline
78-
def onMessage(callback: (Worker, Message, js.Any) => Any): cluster.type = cluster.on("message", callback)
78+
def onMessage(callback: (Worker, Message, js.Any) => Any): Cluster = cluster.on("message", callback)
7979

8080
/**
8181
* After forking a new worker, the worker should respond with an online message. When the master receives an online
@@ -84,7 +84,7 @@ package object cluster {
8484
* @param callback the event handler
8585
*/
8686
@inline
87-
def onOnline(callback: Worker => Any): cluster.type = cluster.on("online", callback)
87+
def onOnline(callback: Worker => Any): Cluster = cluster.on("online", callback)
8888

8989
/**
9090
* Emitted every time .setupMaster() is called.
@@ -95,7 +95,7 @@ package object cluster {
9595
* @param callback the event handler
9696
*/
9797
@inline
98-
def onSetup(callback: ClusterSettings => Any): cluster.type = cluster.on("setup", callback)
98+
def onSetup(callback: ClusterSettings => Any): Cluster = cluster.on("setup", callback)
9999

100100
}
101101

@@ -107,7 +107,7 @@ package object cluster {
107107
* Worker Events and Extensions
108108
* @param worker the given [[Worker worker]]
109109
*/
110-
implicit class WorkerEvents(val worker: Worker) extends AnyVal {
110+
implicit final class WorkerEvents(private val worker: Worker) extends AnyVal {
111111

112112
/////////////////////////////////////////////////////////////////////////////////
113113
// Worker Extensions
@@ -131,15 +131,15 @@ package object cluster {
131131
* @param callback the event handler
132132
*/
133133
@inline
134-
def onDisconnect(callback: () => Any): worker.type = worker.on("disconnect", callback)
134+
def onDisconnect(callback: () => Any): Worker = worker.on("disconnect", callback)
135135

136136
/**
137137
* This event is the same as the one provided by child_process.fork().
138138
* In a worker you can also use process.on('error').
139139
* @param callback the error handler
140140
*/
141141
@inline
142-
def onError(callback: nodejs.Error => Any): worker.type = worker.on("error", callback)
142+
def onError(callback: nodejs.Error => Any): Worker = worker.on("error", callback)
143143

144144
/**
145145
* Similar to the cluster.on('exit') event, but specific to this worker.
@@ -151,14 +151,14 @@ package object cluster {
151151
* </ul>
152152
*/
153153
@inline
154-
def onExit(callback: (Worker, Int, String) => Any): worker.type = worker.on("exit", callback)
154+
def onExit(callback: (Worker, Int, String) => Any): Worker = worker.on("exit", callback)
155155

156156
/**
157157
* Similar to the cluster.on('listening') event, but specific to this worker.
158158
* @param callback the event handler
159159
*/
160160
@inline
161-
def onListening(callback: Address => Any): worker.type = worker.on("listening", callback)
161+
def onListening(callback: Address => Any): Worker = worker.on("listening", callback)
162162

163163
/**
164164
* Similar to the cluster.on('message') event, but specific to this worker.
@@ -167,14 +167,14 @@ package object cluster {
167167
* @param callback the event handler
168168
*/
169169
@inline
170-
def onMessage(callback: Message => Any): worker.type = worker.on("message", callback)
170+
def onMessage(callback: Message => Any): Worker = worker.on("message", callback)
171171

172172
/**
173173
* Similar to the cluster.on('online') event, but specific to this worker.
174174
* @param callback the event handler
175175
*/
176176
@inline
177-
def onOnline(callback: () => Any): worker.type = worker.on("online", callback)
177+
def onOnline(callback: () => Any): Worker = worker.on("online", callback)
178178

179179
}
180180

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)