@live-change/task-service 0.8.140 → 0.8.142
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.js → task.ts} +70 -9
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.142",
|
|
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.142",
|
|
26
|
+
"@live-change/relations-plugin": "^0.8.142"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "6c6de9015268e09f3517dc2dbb28e673ea19fe74"
|
|
29
29
|
}
|
package/{task.js → task.ts}
RENAMED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import App from '@live-change/framework'
|
|
2
|
+
// @ts-ignore:next-line
|
|
2
3
|
const app = App.app()
|
|
3
4
|
|
|
4
|
-
import crypto from 'crypto'
|
|
5
|
+
import * as crypto from 'crypto'
|
|
5
6
|
|
|
6
7
|
import PQueue from 'p-queue'
|
|
7
8
|
|
|
@@ -87,12 +88,72 @@ async function startTask(taskFunction, props, causeType, cause){
|
|
|
87
88
|
return { task: taskObject.id, taskObject, promise, causeType, cause }
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
interface TaskExecuteApi {
|
|
92
|
+
id: string,
|
|
93
|
+
run: (taskFunction, props, progressFactor) => Promise<any>,
|
|
94
|
+
progress: (current, total, action, opts) => void,
|
|
95
|
+
trigger: (trigger, props) => Promise<any>,
|
|
96
|
+
triggerService: (trigger, props, returnArray) => Promise<any>
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface TaskExecuteContext {
|
|
100
|
+
task: TaskExecuteApi,
|
|
101
|
+
trigger: (trigger, props) => Promise<any>,
|
|
102
|
+
triggerService: (trigger, props, returnArray) => Promise<any>,
|
|
103
|
+
causeType: string,
|
|
104
|
+
cause: string
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface TaskDefinition {
|
|
108
|
+
/**
|
|
109
|
+
* Task name
|
|
110
|
+
*/
|
|
111
|
+
name: string,
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Maximum number of retries
|
|
115
|
+
*/
|
|
116
|
+
maxRetries?: number,
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Task execution function
|
|
120
|
+
* @param props - task properties/parameters
|
|
121
|
+
* @param context - task context
|
|
122
|
+
* @param emit - event emitter function
|
|
123
|
+
* @returns {Promise<any>} - task result promise
|
|
124
|
+
*/
|
|
125
|
+
execute: (props, context: TaskExecuteContext, emit) => Promise<any>,
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Cleanup function
|
|
129
|
+
* @param props - task properties/parameters
|
|
130
|
+
* @param context - task context
|
|
131
|
+
* @returns {Promise<void>} - cleanup result promise
|
|
132
|
+
*/
|
|
133
|
+
cleanup?: (props, context: TaskExecuteContext) => Promise<void>,
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Fallback function
|
|
137
|
+
* @param props - task properties/parameters
|
|
138
|
+
* @param context - task context
|
|
139
|
+
* @param error - error object
|
|
140
|
+
* @returns {Promise<any>} - fallback result
|
|
141
|
+
*/
|
|
142
|
+
fallback?: (props, context: TaskExecuteContext, error) => any
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type TaskFunction = (props, context: TaskExecuteContext, emit, reportProgress) => Promise<any>
|
|
146
|
+
|
|
147
|
+
export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
91
148
|
if(!definition) throw new Error('Task definition is not defined')
|
|
92
149
|
if(!serviceDefinition) throw new Error('Service definition is not defined')
|
|
93
|
-
const taskFunction = async (props, context,
|
|
94
|
-
|
|
95
|
-
|
|
150
|
+
const taskFunction = async (props, context,
|
|
151
|
+
emit = events => app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {}),
|
|
152
|
+
reportProgress = (current, total, selfProgress) => {}) => {
|
|
153
|
+
if(!emit) {
|
|
154
|
+
emit = (events) =>
|
|
155
|
+
app.emitEvents(serviceDefinition.name, Array.isArray(events) ? events : [events], {})
|
|
156
|
+
}
|
|
96
157
|
|
|
97
158
|
let taskObject = context.taskObject
|
|
98
159
|
?? await createOrReuseTask(definition, props, context.causeType, context.cause)
|
|
@@ -127,7 +188,7 @@ export default function task(definition, serviceDefinition) {
|
|
|
127
188
|
})
|
|
128
189
|
}
|
|
129
190
|
|
|
130
|
-
let selfProgress = { current: 0, total: 0 }
|
|
191
|
+
let selfProgress = { current: 0, total: 0, action: undefined }
|
|
131
192
|
const subtasksProgress = []
|
|
132
193
|
let progressUpdateTimer, lastProgressUpdate = 0
|
|
133
194
|
const progressThrottleTime = 400
|
|
@@ -159,7 +220,7 @@ export default function task(definition, serviceDefinition) {
|
|
|
159
220
|
...context,
|
|
160
221
|
task: {
|
|
161
222
|
id: taskObject.id,
|
|
162
|
-
async run(taskFunction, props, progressFactor = 1) {
|
|
223
|
+
async run(taskFunction: TaskFunction, props, progressFactor = 1) {
|
|
163
224
|
if(typeof taskFunction !== 'function') {
|
|
164
225
|
console.log("TASK FUNCTION", taskFunction)
|
|
165
226
|
throw new Error('Task function is not a function')
|
|
@@ -176,7 +237,7 @@ export default function task(definition, serviceDefinition) {
|
|
|
176
237
|
causeType: 'task_Task',
|
|
177
238
|
cause: taskObject.id
|
|
178
239
|
},
|
|
179
|
-
(events) => app.emitEvents(
|
|
240
|
+
(events) => app.emitEvents(serviceDefinition.name,
|
|
180
241
|
Array.isArray(events) ? events : [events], {}),
|
|
181
242
|
(current, total, action) => {
|
|
182
243
|
subtaskProgress.current = current
|
|
@@ -213,7 +274,7 @@ export default function task(definition, serviceDefinition) {
|
|
|
213
274
|
}
|
|
214
275
|
}
|
|
215
276
|
try {
|
|
216
|
-
const result = await definition.execute(props, runContext)
|
|
277
|
+
const result = await definition.execute(props, runContext, emit)
|
|
217
278
|
await updateTask({
|
|
218
279
|
state: 'done',
|
|
219
280
|
doneAt: new Date(),
|