@live-change/task-service 0.8.28 → 0.8.30
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/definition.js +1 -1
- package/index.js +7 -1
- package/model.js +3 -0
- package/package.json +4 -4
- package/task.js +103 -38
package/definition.js
CHANGED
|
@@ -2,7 +2,7 @@ import App from '@live-change/framework'
|
|
|
2
2
|
const app = App.app()
|
|
3
3
|
|
|
4
4
|
import relationsPlugin from '@live-change/relations-plugin'
|
|
5
|
-
|
|
5
|
+
import accessControlService from '@live-change/access-control-service'
|
|
6
6
|
|
|
7
7
|
const definition = app.createServiceDefinition({
|
|
8
8
|
name: "task",
|
package/index.js
CHANGED
|
@@ -4,6 +4,12 @@ const app = App.app()
|
|
|
4
4
|
import definition from './definition.js'
|
|
5
5
|
|
|
6
6
|
import './model.js'
|
|
7
|
-
import './task.js'
|
|
7
|
+
import task from './task.js'
|
|
8
|
+
|
|
9
|
+
export { task }
|
|
10
|
+
|
|
11
|
+
definition.beforeStart(() => {
|
|
12
|
+
/// TODO: restart stopped tasks
|
|
13
|
+
})
|
|
8
14
|
|
|
9
15
|
export default definition
|
package/model.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/task-service",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.30",
|
|
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.8.
|
|
26
|
-
"@live-change/relations-plugin": "^0.8.
|
|
25
|
+
"@live-change/framework": "^0.8.30",
|
|
26
|
+
"@live-change/relations-plugin": "^0.8.30"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "aec2a3ede7fe490dcb7db40ee6c2963cd0c62af1"
|
|
29
29
|
}
|
package/task.js
CHANGED
|
@@ -1,53 +1,107 @@
|
|
|
1
1
|
import App from '@live-change/framework'
|
|
2
2
|
const app = App.app()
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
return async (props, context, emit) => {
|
|
6
|
-
if(!emit) emit = (events) => app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {})
|
|
7
|
-
const propertiesJson = JSON.stringify(props)
|
|
8
|
-
const hash = crypto
|
|
9
|
-
.createHash('sha256')
|
|
10
|
-
.update(taskDefinition.name + ':' + propertiesJson)
|
|
11
|
-
.digest('hex')
|
|
12
|
-
|
|
13
|
-
const similarTasks = App.serviceViewGet('task', 'tasksByCauseAndHash', {
|
|
14
|
-
causeType: context.causeType,
|
|
15
|
-
cause: context.cause,
|
|
16
|
-
hash
|
|
17
|
-
})
|
|
18
|
-
const oldTask = similarTasks.find(similarTask => similarTask.name === taskDefinition.name
|
|
19
|
-
&& JSON.stringify(similarTask.properties) === propertiesJson)
|
|
20
|
-
|
|
21
|
-
let taskObject = oldTask
|
|
22
|
-
? await App.serviceViewGet('task', 'task', { task: oldTask.to })
|
|
23
|
-
: {
|
|
24
|
-
id: app.generateUid(),
|
|
25
|
-
name: taskDefinition.name,
|
|
26
|
-
properties: props,
|
|
27
|
-
hash,
|
|
28
|
-
state: 'created'
|
|
29
|
-
}
|
|
4
|
+
import crypto from 'crypto'
|
|
30
5
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
6
|
+
function upperFirst(string) {
|
|
7
|
+
return string.charAt(0).toUpperCase() + string.slice(1)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function triggerOnTaskStateChange(taskObject, causeType, cause) {
|
|
11
|
+
await app.trigger({
|
|
12
|
+
...taskObject,
|
|
13
|
+
type: 'task'+upperFirst(taskObject.state),
|
|
14
|
+
task: taskObject.id,
|
|
15
|
+
causeType,
|
|
16
|
+
cause
|
|
17
|
+
})
|
|
18
|
+
await app.trigger({
|
|
19
|
+
...taskObject,
|
|
20
|
+
type: 'task'+upperFirst(taskObject.name)+upperFirst(taskObject.state),
|
|
21
|
+
task: taskObject.id,
|
|
22
|
+
causeType,
|
|
23
|
+
cause
|
|
24
|
+
})
|
|
25
|
+
await app.trigger({
|
|
26
|
+
...taskObject,
|
|
27
|
+
type: `${taskObject.causeType}_${taksObject.cause}OwnedTask`
|
|
28
|
+
+`${upperFirst(taskObject.name)}${upperFirst(taskObject.state)}`,
|
|
29
|
+
task: taskObject.id,
|
|
30
|
+
causeType,
|
|
31
|
+
cause
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function createOrReuseTask(taskDefinition, props, causeType, cause) {
|
|
36
|
+
|
|
37
|
+
const propertiesJson = JSON.stringify(props)
|
|
38
|
+
const hash = crypto
|
|
39
|
+
.createHash('sha256')
|
|
40
|
+
.update(taskDefinition.name + ':' + propertiesJson)
|
|
41
|
+
.digest('hex')
|
|
42
|
+
|
|
43
|
+
const similarTasks = await app.serviceViewGet('task', 'tasksByCauseAndHash', {
|
|
44
|
+
causeType,
|
|
45
|
+
cause,
|
|
46
|
+
hash
|
|
47
|
+
})
|
|
48
|
+
const oldTask = similarTasks.find(similarTask => similarTask.name === taskDefinition.name
|
|
49
|
+
&& JSON.stringify(similarTask.properties) === propertiesJson)
|
|
50
|
+
|
|
51
|
+
let taskObject = oldTask
|
|
52
|
+
? await app.serviceViewGet('task', 'task', { task: oldTask.to })
|
|
53
|
+
: {
|
|
54
|
+
id: app.generateUid(),
|
|
55
|
+
name: taskDefinition.name,
|
|
56
|
+
properties: props,
|
|
57
|
+
hash,
|
|
58
|
+
state: 'created'
|
|
39
59
|
}
|
|
40
60
|
|
|
41
|
-
|
|
61
|
+
if(!oldTask) {
|
|
62
|
+
/// app.emitEvents
|
|
63
|
+
await app.triggerService('task', {
|
|
64
|
+
...taskObject,
|
|
65
|
+
type: 'task_createCaseOwnedTask',
|
|
66
|
+
causeType,
|
|
67
|
+
cause,
|
|
68
|
+
task: taskObject.id
|
|
69
|
+
})
|
|
70
|
+
await triggerOnTaskStateChange(taskObject, causeType, cause)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function startTask(taskFunction, props, causeType, cause){
|
|
76
|
+
const taskObject = createOrReuseTask(taskFunction.definition, props, causeType, cause)
|
|
77
|
+
const context = {
|
|
78
|
+
causeType,
|
|
79
|
+
cause,
|
|
80
|
+
taskObject
|
|
81
|
+
}
|
|
82
|
+
const promise = taskFunction(props, context)
|
|
83
|
+
return { task: taskObject.id, taskObject, promise }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export default function task(definition) {
|
|
87
|
+
const taskFunction = async (props, context, emit) => {
|
|
88
|
+
if(!emit) emit = (events) =>
|
|
89
|
+
app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {})
|
|
90
|
+
|
|
91
|
+
let taskObject = context.taskObject
|
|
92
|
+
?? await createOrReuseTask(taskDefinition, props, context.causeType, context.cause)
|
|
93
|
+
|
|
94
|
+
const maxRetries = definition.maxRetries ?? 5
|
|
42
95
|
|
|
43
96
|
async function updateTask(data) {
|
|
44
|
-
await
|
|
97
|
+
await app.triggerService('task', {
|
|
45
98
|
...data,
|
|
99
|
+
type: 'task_updateCaseOwnedTask',
|
|
46
100
|
causeType: context.causeType,
|
|
47
101
|
cause: context.cause,
|
|
48
102
|
task: taskObject.id
|
|
49
103
|
})
|
|
50
|
-
taskObject = await
|
|
104
|
+
taskObject = await app.serviceViewGet('task', 'task', { task: oldTask.to })
|
|
51
105
|
}
|
|
52
106
|
|
|
53
107
|
const runTask = async () => {
|
|
@@ -55,8 +109,9 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
|
|
|
55
109
|
state: 'running',
|
|
56
110
|
startedAt: new Date()
|
|
57
111
|
})
|
|
112
|
+
await triggerOnTaskStateChange(taskObject, context.causeType, context.cause)
|
|
58
113
|
try {
|
|
59
|
-
const result = await
|
|
114
|
+
const result = await definition.execute(props, {
|
|
60
115
|
...context,
|
|
61
116
|
task: {
|
|
62
117
|
async run(taskFunction, props) {
|
|
@@ -79,6 +134,7 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
|
|
|
79
134
|
doneAt: new Date(),
|
|
80
135
|
result
|
|
81
136
|
})
|
|
137
|
+
await triggerOnTaskStateChange(taskObject, context.causeType, context.cause)
|
|
82
138
|
} catch(error) {
|
|
83
139
|
if(taskObject.retries.length >= maxRetries) {
|
|
84
140
|
await updateTask({
|
|
@@ -86,6 +142,7 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
|
|
|
86
142
|
doneAt: new Date(),
|
|
87
143
|
error: error.message
|
|
88
144
|
})
|
|
145
|
+
await triggerOnTaskStateChange(taskObject, context.causeType, context.cause)
|
|
89
146
|
}
|
|
90
147
|
await updateTask(taskObject.id, {
|
|
91
148
|
state: 'retrying',
|
|
@@ -95,6 +152,7 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
|
|
|
95
152
|
error: error.message
|
|
96
153
|
}]
|
|
97
154
|
})
|
|
155
|
+
await triggerOnTaskStateChange(taskObject, context.causeType, context.cause)
|
|
98
156
|
}
|
|
99
157
|
}
|
|
100
158
|
|
|
@@ -105,4 +163,11 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
|
|
|
105
163
|
|
|
106
164
|
return taskObject.result
|
|
107
165
|
}
|
|
166
|
+
|
|
167
|
+
taskFunction.definition = definition
|
|
168
|
+
taskFunction.start = async (props, causeType, cause) => {
|
|
169
|
+
return await startTask(taskFunction, props, causeType, cause)
|
|
170
|
+
}
|
|
171
|
+
return taskFunction
|
|
172
|
+
|
|
108
173
|
}
|