Skip to content

Commit b3fd2a2

Browse files
committed
closes #42
1 parent 918affb commit b3fd2a2

6 files changed

Lines changed: 245 additions & 21 deletions

File tree

examples/tasks/status-display.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const debug = require('debug')('dataparty.task.status-update')
2+
3+
const ITask = require('../../src/service/itask')
4+
5+
class StatusDisplayTask extends ITask {
6+
7+
constructor(options){
8+
super({
9+
name: StatusDisplayTask.Name,
10+
background: true,
11+
...options
12+
})
13+
14+
debug('new')
15+
16+
this.duration = 5000
17+
this.timeout = null
18+
}
19+
20+
async exec(){
21+
22+
this.setTimer()
23+
24+
return this.detach()
25+
}
26+
27+
setTimer(){
28+
this.timeout = setTimeout(this.onTimeout.bind(this), this.duration)
29+
}
30+
31+
onTimeout(){
32+
this.timeout = null
33+
34+
this.context.serviceRunner.taskRunner.printTaskLists()
35+
debug('sleep complete')
36+
37+
this.setTimer()
38+
}
39+
40+
stop(){
41+
if(this.timeout !== null){
42+
clearTimeout(this.timeout)
43+
this.timeout = null
44+
}
45+
}
46+
47+
static get Name(){
48+
return 'status-display'
49+
}
50+
51+
static get Description(){
52+
return 'Status Display'
53+
}
54+
}
55+
56+
module.exports = StatusDisplayTask

examples/test-service-tasks.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const Path = require('path')
2+
const debug = require('debug')('test.server-db')
3+
const Dataparty = require('../src')
4+
5+
const BouncerServerModels = require('@dataparty/bouncer-model')
6+
const BouncerClientModels = require('@dataparty/bouncer-model/dist/bouncer-model.json')
7+
8+
class ExampleTaskService extends Dataparty.IService {
9+
constructor(opts){
10+
super(opts)
11+
12+
this.addTask(Path.join(__dirname,'./tasks/status-display.js'))
13+
}
14+
15+
}
16+
17+
async function main(){
18+
19+
20+
//const uri = 'mongodb://localhost:27017/server-party-test'
21+
//debug('db location', uri)
22+
23+
const path = '/data/datparty/srv-party'
24+
25+
let party = new Dataparty.TingoParty({
26+
path,
27+
model: BouncerClientModels,
28+
serverModels: BouncerServerModels,
29+
config: new Dataparty.Config.JsonFileConfig({basePath: '/data/datparty/'})
30+
})
31+
32+
const service = new ExampleTaskService({ name: '@dataparty/task-example', version: '0.0.1' })
33+
34+
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
35+
36+
debug('built', Object.keys(build))
37+
38+
const runner = new Dataparty.ServiceRunnerNode({
39+
party, service,
40+
sendFullErrors: false,
41+
useNative: true
42+
})
43+
44+
const host = new Dataparty.ServiceHost({
45+
runner,
46+
trust_proxy: true,
47+
wsEnabled: true,
48+
listenUri: 'http://localhost:8080'
49+
})
50+
51+
await party.start()
52+
await runner.start()
53+
await host.start()
54+
55+
console.log('started')
56+
57+
//process.exit()
58+
}
59+
60+
61+
62+
main().catch(err=>{
63+
console.error(err)
64+
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"dependencies": {
6060
"@dataparty/bouncer-db": "1.0.1",
6161
"@dataparty/crypto": "datapartyjs/dataparty-crypto#parcel-build",
62+
"@dataparty/tasker": "^0.0.2",
6263
"@hapi/joi": "^17.1.1",
6364
"@zeit/ncc": "^0.22.3",
6465
"ajv": "6.9.1",

src/service/iservice.js

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ module.exports = class IService {
2121
middleware: {
2222
pre: {},
2323
post: {}
24-
}
24+
},
25+
tasks: {}
2526
}
2627

2728
this.middleware_order = {
@@ -36,7 +37,8 @@ module.exports = class IService {
3637
middleware: {
3738
pre: {},
3839
post: {}
39-
}
40+
},
41+
tasks: {}
4042
}
4143

4244
this.compiled = {
@@ -55,7 +57,27 @@ module.exports = class IService {
5557
middleware_order: {
5658
pre: [],
5759
post: []
58-
}
60+
},
61+
tasks: {}
62+
}
63+
64+
this.compileSettings = {
65+
// provide a custom cache path or disable caching
66+
cache: false,
67+
// externals to leave as requires of the build
68+
externals: ['debug', '@dataparty/crypto', '@dataparty/tasker', '@hapi/joi', '@hapi/hoek'],
69+
// directory outside of which never to emit assets
70+
//filterAssetBase: process.cwd(), // default
71+
minify: false, // default
72+
sourceMap: true, // default
73+
//sourceMapBasePrefix: '../', // default treats sources as output-relative
74+
// when outputting a sourcemap, automatically include
75+
// source-map-support in the output file (increases output by 32kB).
76+
sourceMapRegister: false, // default
77+
watch: false, // default
78+
v8cache: false, // default
79+
quiet: false, // default
80+
debugLog: false // default
5981
}
6082
}
6183

@@ -112,6 +134,18 @@ module.exports = class IService {
112134
this.constructors.middleware[type][name] = middleware
113135
}
114136

137+
addTask(task_path){
138+
139+
debug('addTask', task_path)
140+
141+
const TaskClass = require(task_path)
142+
143+
const name = TaskClass.Name
144+
145+
this.sources.tasks[name] = task_path
146+
this.constructors.tasks[name] = TaskClass
147+
}
148+
115149

116150
async compile(outputPath, writeFile=true){
117151

@@ -131,11 +165,14 @@ module.exports = class IService {
131165
this.compileMiddleware('post'),
132166
this.compileList('documents'),
133167
this.compileList('endpoints'),
168+
this.compileList('tasks'),
134169
this.compileSchemas()
135170
])
136171

137172
this.compiled.middleware_order = this.middleware_order
138173

174+
this.compiled.compileSettings = this.compileSettings
175+
139176
if(writeFile){
140177
const buildOutput = outputPath+'/'+ this.compiled.package.name.replace('/', '-') +'.dataparty-service.json'
141178
fs.writeFileSync(buildOutput, JSON.stringify(this.compiled, null,2))
@@ -179,24 +216,7 @@ module.exports = class IService {
179216
}
180217

181218
async compileFileTo(input, output){
182-
const { code, map, assets } = await NCC(input, {
183-
// provide a custom cache path or disable caching
184-
cache: false,
185-
// externals to leave as requires of the build
186-
externals: ['debug', '@dataparty/crypto', '@hapi/joi', '@hapi/hoek'],
187-
// directory outside of which never to emit assets
188-
//filterAssetBase: process.cwd(), // default
189-
minify: true, // default
190-
sourceMap: true, // default
191-
//sourceMapBasePrefix: '../', // default treats sources as output-relative
192-
// when outputting a sourcemap, automatically include
193-
// source-map-support in the output file (increases output by 32kB).
194-
sourceMapRegister: false, // default
195-
watch: false, // default
196-
v8cache: false, // default
197-
quiet: false, // default
198-
debugLog: false // default
199-
})
219+
const { code, map, assets } = await NCC(input, this.compileSettings)
200220

201221
debug('compileFileTo', input, '->', output)
202222
debug('\t','code length', Math.round(code.length/1024), 'KB')

src/service/itask.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const debug = require('debug')('dataparty.service.ITask')
2+
const tasker = require('@dataparty/tasker')
3+
4+
5+
module.exports = class ITask extends tasker.Task {
6+
7+
static get Name(){
8+
throw new Error('not implemented')
9+
}
10+
11+
static get Description(){
12+
throw new Error('not implemented')
13+
}
14+
15+
static get info(){
16+
return {
17+
Name: this.Name,
18+
Description: this.Description
19+
}
20+
}
21+
}

src/service/service-runner-node.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const DeltaTime = require('../utils/delta-time')
99

1010
const Router = require('origin-router').Router
1111

12+
const Runner = require('@dataparty/tasker').Runner
13+
1214
class ServiceRunnerNode {
1315
constructor({service, party, sendFullErrors=false, useNative=true}){
1416
this.party = party
@@ -18,11 +20,31 @@ class ServiceRunnerNode {
1820

1921
this.middleware = { pre: {}, post: {} }
2022
this.endpoint = {}
23+
this.tasks = {}
2124

2225
this.router = new Router()
26+
this.taskRunner = new Runner()
2327
}
2428

2529
async start(){
30+
31+
debug('starting tasks')
32+
33+
const taskMap = Hoek.reach(this.service, 'compiled.tasks')
34+
//const endpointsLoading = []
35+
for(let name in taskMap){
36+
debug('\t',name)
37+
await this.loadTask(name)
38+
}
39+
40+
debug('tasks ready:')
41+
for(let name in this.tasks){
42+
debug('\t', name)
43+
}
44+
45+
await this.taskRunner.start()
46+
47+
2648
debug('starting endpoints')
2749

2850
const eps = Hoek.reach(this.service, 'compiled.endpoints')
@@ -40,6 +62,46 @@ class ServiceRunnerNode {
4062
}
4163
}
4264

65+
async loadTask(name){
66+
if(this.tasks[name]){
67+
return
68+
}
69+
70+
debug('loadTask', name)
71+
72+
let dt = new DeltaTime().start()
73+
74+
75+
"use strict"
76+
let task=null
77+
78+
let TaskClass = null
79+
80+
if(!this.useNative){
81+
const build = Hoek.reach(this.service, `compiled.tasks.${name}`)
82+
TaskClass = eval(build.code/*, build.map*/)
83+
}
84+
else{
85+
TaskClass = this.service.constructors.tasks[name]
86+
}
87+
88+
task = new TaskClass({
89+
context:{
90+
party: this.party,
91+
serviceRunner: this
92+
}
93+
})
94+
95+
96+
debug('task info', TaskClass.info)
97+
98+
this.tasks[name] = task
99+
100+
this.taskRunner.addTask(task)
101+
dt.end()
102+
debug('loaded task',name,'in',dt.deltaMs,'ms')
103+
}
104+
43105
async loadEndpoint(name){
44106
if(this.endpoint[name]){
45107
return

0 commit comments

Comments
 (0)