Skip to content

Commit 9f1a97c

Browse files
committed
closes #85 and #86
1 parent c28a243 commit 9f1a97c

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/service/service-runner-node.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class ServiceRunnerNode {
4040
this.taskRunner = new Runner()
4141

4242
this.started = false
43+
44+
this.taskCounter = 0
4345
}
4446

4547
async start(){
@@ -80,6 +82,12 @@ class ServiceRunnerNode {
8082
}
8183
}
8284

85+
assertTaskIsValid(name){
86+
if(!this.tasks[name]){
87+
throw new Error('invalid task ['+name+']')
88+
}
89+
}
90+
8391
async loadTask(name){
8492
if(this.tasks[name]){
8593
return
@@ -124,12 +132,56 @@ class ServiceRunnerNode {
124132
debug('loaded task',name,'in',dt.deltaMs,'ms')
125133
}
126134

135+
async spawnTask(type, context){
136+
this.assertTaskIsValid(type)
137+
138+
debug('spawnTask', type, 'useNative =',this.useNative)
139+
140+
let dt = new DeltaTime().start()
141+
142+
143+
"use strict"
144+
let task=null
145+
146+
let TaskClass = null
147+
148+
if(!this.useNative){
149+
const build = Hoek.reach(this.service, `compiled.tasks.${type}`)
150+
TaskClass = eval(build.code/*, build.map*/)
151+
}
152+
else{
153+
TaskClass = this.service.constructors.tasks[type]
154+
}
155+
156+
task = new TaskClass({
157+
context:{
158+
party: this.party,
159+
serviceRunner: this,
160+
...context
161+
}
162+
})
163+
164+
task.name = task.name + '-' + this.taskCounter++
165+
166+
167+
debug('task info', TaskClass.info)
168+
169+
this.taskRunner.addTask(task)
170+
171+
172+
dt.end()
173+
debug('spawned task',task.name,'in',dt.deltaMs,'ms')
174+
175+
return task
176+
}
177+
127178
/**
128179
* Add a named task to the run queue
129180
* @see https://github.com/datapartyjs/tasker
130181
* @param {string} name
131182
*/
132183
runTask(name){
184+
this.assertTaskIsValid(name)
133185
const task = this.tasks[name]
134186

135187
this.taskRunner.addTask(task)

0 commit comments

Comments
 (0)