git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit aebb51fa47b38ca825a49a00b5a83afca13ba3e3

let containers handle idle state

wanderer committed on 7/21/2017, 6:55:43 PM
Parent: 735d3d8cc175f8325e66554fc9319634910c9ee0

Files changed

examples/index.jschanged
kernel.jschanged
portManager.jschanged
tests/index.jschanged
examples/index.jsView
@@ -31,8 +31,12 @@
3131 if (message.data === 'bindPort') {
3232 this.kernel.ports.bind('channel', message.ports[0])
3333 }
3434 }
35+
36+ onIdle () {
37+ this.kernel.shutdown()
38+ }
3539 }
3640
3741 // wait untill the ipfs node is ready
3842 node.on('ready', async() => {
kernel.jsView
@@ -17,8 +17,9 @@
1717 this.state = opts.state
1818 this.hypervisor = opts.hypervisor
1919 this.id = opts.id
2020 this.container = new opts.container.Constructor(this, opts.container.args)
21+ this.timeout = 0
2122
2223 this.ticks = 0
2324 this.containerState = 'idle'
2425
@@ -49,9 +50,9 @@
4950 if (this.containerState !== 'running') {
5051 this.containerState = 'running'
5152
5253 while (1) {
53- const message = await this.ports.getNextMessage()
54+ const message = await this.ports.getNextMessage(this.timeout)
5455 if (!message) break
5556
5657 // dequqe message
5758 message.fromPort.messages.shift()
@@ -63,44 +64,47 @@
6364 }
6465 // run the next message
6566 await this.run(message)
6667 }
67- // no more messages; shut down
68- this.hypervisor.scheduler.done(this.id)
68+
69+ this.containerState = 'idle'
70+ this.container.onIdle()
6971 }
7072 }
7173
74+ shutdown () {
75+ this.hypervisor.scheduler.done(this.id)
76+ }
77+
7278 /**
7379 * run the kernels code with a given enviroment
7480 * @param {object} message - the message to run
7581 * @param {boolean} init - whether or not to run the intialization routine
7682 * @returns {Promise}
7783 */
7884 async run (message, method = 'run') {
79- let result
80-
81- const responsePort = message.responsePort
82- delete message.responsePort
83-
84- this.ports.addReceivedPorts(message)
85-
8685 if (message.constructor === DeleteMessage) {
8786 this.ports._delete(message.fromName)
8887 } else {
88+ const responsePort = message.responsePort
89+ delete message.responsePort
90+
91+ this.ports.addReceivedPorts(message)
92+ let result
8993 try {
9094 result = await this.container[method](message) || {}
9195 } catch (e) {
9296 result = {
9397 exception: true,
9498 exceptionError: e
9599 }
96100 }
97- }
98101
99- if (responsePort) {
100- this.send(responsePort, new Message({
101- data: result
102- }))
102+ if (responsePort) {
103+ this.send(responsePort, new Message({
104+ data: result
105+ }))
106+ }
103107 }
104108
105109 this.ports.clearUnboundedPorts()
106110 }
portManager.jsView
@@ -242,9 +242,9 @@
242242
243243 // returns a promise that resolve when a message older then the given message
244244 // is recived
245245 _olderMessage (message) {
246- this._messageTickThreshold = message ? message._fromTicks : 0
246+ this._messageTickThreshold = message ? message._fromTicks : -1
247247 return this._oldestMessagePromise
248248 }
249249
250250 removeSentPorts (message) {
tests/index.jsView
@@ -17,8 +17,12 @@
1717 if (port) {
1818 this.kernel.ports.bind('root', port)
1919 }
2020 }
21+
22+ onIdle () {
23+ this.kernel.shutdown()
24+ }
2125 }
2226
2327 node.on('ready', () => {
2428 tape('basic', async t => {

Built with git-ssb-web