@live-change/task-service 0.8.136 → 0.8.137
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/index.js +1 -1
- package/package.json +4 -4
- package/{task.ts → task.js} +9 -70
package/index.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.137",
|
|
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.137",
|
|
26
|
+
"@live-change/relations-plugin": "^0.8.137"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "cb42255fbe7aa794dc5d7a6e57250ce8c959acf9"
|
|
29
29
|
}
|
package/{task.ts → task.js}
RENAMED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import App from '@live-change/framework'
|
|
2
|
-
// @ts-ignore:next-line
|
|
3
2
|
const app = App.app()
|
|
4
3
|
|
|
5
|
-
import
|
|
4
|
+
import crypto from 'crypto'
|
|
6
5
|
|
|
7
6
|
import PQueue from 'p-queue'
|
|
8
7
|
|
|
@@ -88,72 +87,12 @@ async function startTask(taskFunction, props, causeType, cause){
|
|
|
88
87
|
return { task: taskObject.id, taskObject, promise, causeType, cause }
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
|
|
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) {
|
|
90
|
+
export default function task(definition, serviceDefinition) {
|
|
148
91
|
if(!definition) throw new Error('Task definition is not defined')
|
|
149
92
|
if(!serviceDefinition) throw new Error('Service definition is not defined')
|
|
150
|
-
const taskFunction = async (props, context,
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if(!emit) {
|
|
154
|
-
emit = (events) =>
|
|
155
|
-
app.emitEvents(serviceDefinition.name, Array.isArray(events) ? events : [events], {})
|
|
156
|
-
}
|
|
93
|
+
const taskFunction = async (props, context, emit, reportProgress = () => {}) => {
|
|
94
|
+
if(!emit) emit = (events) =>
|
|
95
|
+
app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {})
|
|
157
96
|
|
|
158
97
|
let taskObject = context.taskObject
|
|
159
98
|
?? await createOrReuseTask(definition, props, context.causeType, context.cause)
|
|
@@ -188,7 +127,7 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
188
127
|
})
|
|
189
128
|
}
|
|
190
129
|
|
|
191
|
-
let selfProgress = { current: 0, total: 0
|
|
130
|
+
let selfProgress = { current: 0, total: 0 }
|
|
192
131
|
const subtasksProgress = []
|
|
193
132
|
let progressUpdateTimer, lastProgressUpdate = 0
|
|
194
133
|
const progressThrottleTime = 400
|
|
@@ -220,7 +159,7 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
220
159
|
...context,
|
|
221
160
|
task: {
|
|
222
161
|
id: taskObject.id,
|
|
223
|
-
async run(taskFunction
|
|
162
|
+
async run(taskFunction, props, progressFactor = 1) {
|
|
224
163
|
if(typeof taskFunction !== 'function') {
|
|
225
164
|
console.log("TASK FUNCTION", taskFunction)
|
|
226
165
|
throw new Error('Task function is not a function')
|
|
@@ -237,7 +176,7 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
237
176
|
causeType: 'task_Task',
|
|
238
177
|
cause: taskObject.id
|
|
239
178
|
},
|
|
240
|
-
(events) => app.emitEvents(
|
|
179
|
+
(events) => app.emitEvents(definition.name,
|
|
241
180
|
Array.isArray(events) ? events : [events], {}),
|
|
242
181
|
(current, total, action) => {
|
|
243
182
|
subtaskProgress.current = current
|
|
@@ -274,7 +213,7 @@ export default function task(definition:TaskDefinition, serviceDefinition) {
|
|
|
274
213
|
}
|
|
275
214
|
}
|
|
276
215
|
try {
|
|
277
|
-
const result = await definition.execute(props, runContext
|
|
216
|
+
const result = await definition.execute(props, runContext)
|
|
278
217
|
await updateTask({
|
|
279
218
|
state: 'done',
|
|
280
219
|
doneAt: new Date(),
|