@live-change/task-service 0.8.35 → 0.8.37
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 +60 -51
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.37",
|
|
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.37",
|
|
26
|
+
"@live-change/relations-plugin": "^0.8.37"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "8b97a83258a3eccd08534d0376015781c4eefdec"
|
|
29
29
|
}
|
package/task.js
CHANGED
|
@@ -155,60 +155,61 @@ export default function task(definition, serviceDefinition) {
|
|
|
155
155
|
startedAt: new Date()
|
|
156
156
|
})
|
|
157
157
|
await triggerOnTaskStateChange(taskObject, context.causeType, context.cause)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
(events) => app.emitEvents(definition.name,
|
|
158
|
+
const runContext = {
|
|
159
|
+
...context,
|
|
160
|
+
task: {
|
|
161
|
+
id: taskObject.id,
|
|
162
|
+
async run(taskFunction, props, progressFactor = 1) {
|
|
163
|
+
if(typeof taskFunction !== 'function') {
|
|
164
|
+
console.log("TASK FUNCTION", taskFunction)
|
|
165
|
+
throw new Error('Task function is not a function')
|
|
166
|
+
}
|
|
167
|
+
//console.log("SUBTASK RUN", taskFunction.definition.name, props)
|
|
168
|
+
const subtaskProgress = { current: 0, total: 1, factor: progressFactor }
|
|
169
|
+
subtasksProgress.push(subtaskProgress)
|
|
170
|
+
const result = await taskFunction(
|
|
171
|
+
props,
|
|
172
|
+
{
|
|
173
|
+
...context,
|
|
174
|
+
taskObject: undefined,
|
|
175
|
+
task: taskObject.id,
|
|
176
|
+
causeType: 'task_Task',
|
|
177
|
+
cause: taskObject.id
|
|
178
|
+
},
|
|
179
|
+
(events) => app.emitEvents(definition.name,
|
|
181
180
|
Array.isArray(events) ? events : [events], {}),
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
)
|
|
188
|
-
//console.log("SUBTASK DONE", taskFunction.definition.name, props, '=>', result)
|
|
189
|
-
subtaskProgress.current = subtaskProgress.total
|
|
190
|
-
updateProgress()
|
|
191
|
-
return result
|
|
192
|
-
},
|
|
193
|
-
async progress(current, total, action, opts) {
|
|
194
|
-
selfProgress = {
|
|
195
|
-
...opts,
|
|
196
|
-
current, total, action
|
|
181
|
+
(current, total, action) => {
|
|
182
|
+
subtaskProgress.current = current
|
|
183
|
+
subtaskProgress.total = total
|
|
184
|
+
updateProgress()
|
|
197
185
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
return
|
|
203
|
-
causeType: 'task_Task',
|
|
204
|
-
cause: taskObject.id,
|
|
205
|
-
...trigger,
|
|
206
|
-
}, props)
|
|
186
|
+
)
|
|
187
|
+
//console.log("SUBTASK DONE", taskFunction.definition.name, props, '=>', result)
|
|
188
|
+
subtaskProgress.current = subtaskProgress.total
|
|
189
|
+
updateProgress()
|
|
190
|
+
return result
|
|
207
191
|
},
|
|
208
|
-
async
|
|
209
|
-
|
|
192
|
+
async progress(current, total, action, opts) {
|
|
193
|
+
selfProgress = {
|
|
194
|
+
...opts,
|
|
195
|
+
current, total, action
|
|
196
|
+
}
|
|
197
|
+
updateProgress()
|
|
210
198
|
}
|
|
211
|
-
}
|
|
199
|
+
},
|
|
200
|
+
async trigger(trigger, props) {
|
|
201
|
+
return await app.trigger({
|
|
202
|
+
causeType: 'task_Task',
|
|
203
|
+
cause: taskObject.id,
|
|
204
|
+
...trigger,
|
|
205
|
+
}, props)
|
|
206
|
+
},
|
|
207
|
+
async triggerService(trigger, props, returnArray = false) {
|
|
208
|
+
return await app.triggerService(trigger, props, returnArray)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
const result = await definition.execute(props, runContext)
|
|
212
213
|
await updateTask({
|
|
213
214
|
state: 'done',
|
|
214
215
|
doneAt: new Date(),
|
|
@@ -218,7 +219,7 @@ export default function task(definition, serviceDefinition) {
|
|
|
218
219
|
} catch(error) {
|
|
219
220
|
console.log("TASK ERROR", error.message, error.stack)
|
|
220
221
|
/*console.log("RETRIES", taskObject.retries?.length, maxRetries)*/
|
|
221
|
-
if((taskObject.retries?.length || 0) >= taskObject.maxRetries) {
|
|
222
|
+
if((taskObject.retries?.length || 0) >= taskObject.maxRetries - 1) {
|
|
222
223
|
await updateTask({
|
|
223
224
|
state: 'failed',
|
|
224
225
|
doneAt: new Date(),
|
|
@@ -229,6 +230,14 @@ export default function task(definition, serviceDefinition) {
|
|
|
229
230
|
error: error.stack ?? error.message ?? error
|
|
230
231
|
}]
|
|
231
232
|
})
|
|
233
|
+
console.error("TASK", taskObject.id, "OF TYPE", definition.name,
|
|
234
|
+
"WITH PARAMETERS", props, "FAILED WITH ERROR", error.stack ?? error.message ?? error)
|
|
235
|
+
if(definition.fallback) {
|
|
236
|
+
if(typeof definition.fallback !== 'function') return definition.fallback
|
|
237
|
+
return await definition.fallback(props, runContext, error.message ?? error)
|
|
238
|
+
} else {
|
|
239
|
+
throw error
|
|
240
|
+
}
|
|
232
241
|
} else {
|
|
233
242
|
const retriesCount = (taskObject.retries || []).length
|
|
234
243
|
await updateTask({
|