@live-change/task-service 0.9.87 → 0.9.88
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/task.ts +90 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/task-service",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.88",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
},
|
|
23
23
|
"type": "module",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@live-change/framework": "^0.9.
|
|
26
|
-
"@live-change/relations-plugin": "^0.9.
|
|
25
|
+
"@live-change/framework": "^0.9.88",
|
|
26
|
+
"@live-change/relations-plugin": "^0.9.88"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "c042d36f63a6cc50158c3b3385f7f21ea98b847d"
|
|
29
29
|
}
|
package/task.ts
CHANGED
|
@@ -52,6 +52,9 @@ async function createOrReuseTask(taskDefinition, props, causeType, cause, expire
|
|
|
52
52
|
expireDate
|
|
53
53
|
})
|
|
54
54
|
|
|
55
|
+
console.log("HASH", hash)
|
|
56
|
+
console.log("SIMILAR TASKS", similarTasks)
|
|
57
|
+
|
|
55
58
|
const oldTask = similarTasks.find(similarTask => similarTask.name === taskDefinition.name
|
|
56
59
|
&& JSON.stringify(similarTask.properties) === propertiesJson
|
|
57
60
|
&& (!expireDate || new Date(similarTask.startedAt).getTime() > expireDate.getTime()))
|
|
@@ -172,7 +175,7 @@ interface TaskDefinition {
|
|
|
172
175
|
* create action for task, action will return object with task property,
|
|
173
176
|
* @param action - true, or action name
|
|
174
177
|
*/
|
|
175
|
-
action?:
|
|
178
|
+
action?: { name: string } // TODO: create ActionDefinition type
|
|
176
179
|
|
|
177
180
|
/**
|
|
178
181
|
* Task service name
|
|
@@ -293,7 +296,7 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
293
296
|
updateProgress()
|
|
294
297
|
return result
|
|
295
298
|
},
|
|
296
|
-
async progress(current, total, action, opts) {
|
|
299
|
+
async progress(current, total, action, opts) { // throttle this
|
|
297
300
|
selfProgress = {
|
|
298
301
|
...opts,
|
|
299
302
|
current, total, action
|
|
@@ -314,6 +317,65 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
314
317
|
cause: taskObject.id,
|
|
315
318
|
...trigger
|
|
316
319
|
}, props, returnArray)
|
|
320
|
+
},
|
|
321
|
+
async triggerTask(trigger, data, progressFactor = 1) {
|
|
322
|
+
const tasks = await app.trigger({
|
|
323
|
+
causeType: 'task_Task',
|
|
324
|
+
cause: taskObject.id,
|
|
325
|
+
...trigger
|
|
326
|
+
}, data)
|
|
327
|
+
const fullProgressSum = tasks.length * progressFactor
|
|
328
|
+
const task = this
|
|
329
|
+
const taskWatchers = tasks.map(task => {
|
|
330
|
+
const observable = Task.observable(task)
|
|
331
|
+
if(!observable) {
|
|
332
|
+
console.error("SUBTASK OBSERVABLE NOT FOUND", task)
|
|
333
|
+
throw new Error("SUBTASK OBSERVABLE NOT FOUND")
|
|
334
|
+
}
|
|
335
|
+
const watcher: any = {
|
|
336
|
+
observable,
|
|
337
|
+
observer(signal, value) {
|
|
338
|
+
if(signal !== 'set') return
|
|
339
|
+
if(value) {
|
|
340
|
+
if(value.progress) {
|
|
341
|
+
watcher.progress = value.progress
|
|
342
|
+
updateProgress()
|
|
343
|
+
}
|
|
344
|
+
if(value.state === 'done') {
|
|
345
|
+
watcher.resolve(value.result)
|
|
346
|
+
} else if(value.state === 'failed') {
|
|
347
|
+
watcher.reject(value)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
progress: {
|
|
352
|
+
current: 0,
|
|
353
|
+
total: 1,
|
|
354
|
+
factor: progressFactor/tasks.length
|
|
355
|
+
},
|
|
356
|
+
run(resolve, reject) {
|
|
357
|
+
watcher.resolve = resolve
|
|
358
|
+
watcher.reject = reject
|
|
359
|
+
//console.log("SUBTASK WATCHER", watcher, "TASK OBSERVABLE", watcher.observable)
|
|
360
|
+
subtasksProgress.push(watcher.progress)
|
|
361
|
+
watcher.observable.observe(watcher.observer)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return watcher
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
const promises = taskWatchers.map(watcher => new Promise((resolve, reject) => watcher.run(resolve, reject)))
|
|
368
|
+
await Promise.all(promises)
|
|
369
|
+
//console.log("TASK WATCHERS PROMISES FULLFILLED", taskWatchers)
|
|
370
|
+
const results = taskWatchers.map(watcher => {
|
|
371
|
+
//console.log("WATCHER OBSERVABLE", watcher.observable)
|
|
372
|
+
watcher.observable.getValue().result
|
|
373
|
+
})
|
|
374
|
+
for(const watcher of taskWatchers) {
|
|
375
|
+
//console.log("UNOBSERVING WATCHER", watcher)
|
|
376
|
+
watcher.observable.unobserve(watcher.observer)
|
|
377
|
+
}
|
|
378
|
+
return results
|
|
317
379
|
}
|
|
318
380
|
}
|
|
319
381
|
try {
|
|
@@ -444,6 +506,7 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
444
506
|
|
|
445
507
|
const Task = serviceDefinition.foreignModel('task', 'Task')
|
|
446
508
|
|
|
509
|
+
|
|
447
510
|
if(definition.trigger) {
|
|
448
511
|
serviceDefinition.trigger({
|
|
449
512
|
name: definition.trigger === true ? definition.name : definition.trigger,
|
|
@@ -454,18 +517,34 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
454
517
|
},
|
|
455
518
|
async execute(props, context, emit) {
|
|
456
519
|
const startResult =
|
|
457
|
-
await startTask(taskFunction, props,
|
|
520
|
+
await startTask(taskFunction, props,
|
|
521
|
+
context.reaction.causeType ?? 'trigger',
|
|
522
|
+
context.reaction.cause ?? context.reaction.id,
|
|
523
|
+
props.taskExpire) // Must be done that way, so subtasks can be connected to the parent task
|
|
458
524
|
return startResult.task
|
|
459
525
|
}
|
|
460
526
|
})
|
|
527
|
+
} else {
|
|
528
|
+
serviceDefinition.trigger({
|
|
529
|
+
name: 'runTask_'+serviceDefinition.name+'_'+definition.name,
|
|
530
|
+
properties: definition.properties,
|
|
531
|
+
returnsTask: definition.name,
|
|
532
|
+
returns: {
|
|
533
|
+
type: Task
|
|
534
|
+
},
|
|
535
|
+
async execute(props, context, emit) {
|
|
536
|
+
const startResult =
|
|
537
|
+
await startTask(taskFunction, props,
|
|
538
|
+
context.reaction.causeType ?? 'trigger',
|
|
539
|
+
context.reaction.cause ?? context.reaction.id,
|
|
540
|
+
props.taskExpire) // Must be done that way, so subtasks can be connected to the parent task
|
|
541
|
+
return startResult.task
|
|
542
|
+
}
|
|
543
|
+
})
|
|
461
544
|
}
|
|
462
545
|
|
|
463
|
-
if(definition.action) {
|
|
464
|
-
const name =
|
|
465
|
-
(definition.action === true && definition.name)
|
|
466
|
-
|| (typeof definition.action === 'string' && definition.action)
|
|
467
|
-
|| (typeof definition.action === 'object' && definition.action.name)
|
|
468
|
-
|| definition.name
|
|
546
|
+
if(definition.action) {
|
|
547
|
+
const name = definition.action.name || definition.name
|
|
469
548
|
serviceDefinition.action({
|
|
470
549
|
name,
|
|
471
550
|
properties: definition.properties,
|
|
@@ -484,6 +563,8 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
484
563
|
},
|
|
485
564
|
...(typeof definition.action === 'object' && definition.action)
|
|
486
565
|
})
|
|
566
|
+
|
|
567
|
+
|
|
487
568
|
}
|
|
488
569
|
|
|
489
570
|
taskFunction.definition = definition
|