@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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. 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.35",
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.35",
26
- "@live-change/relations-plugin": "^0.8.35"
25
+ "@live-change/framework": "^0.8.37",
26
+ "@live-change/relations-plugin": "^0.8.37"
27
27
  },
28
- "gitHead": "90fbb746dc7270895daf17b437ca48c0b0a01c01"
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
- try {
159
- const result = await definition.execute(props, {
160
- ...context,
161
- task: {
162
- id: taskObject.id,
163
- async run(taskFunction, props, progressFactor = 1) {
164
- if(typeof taskFunction !== 'function') {
165
- console.log("TASK FUNCTION", taskFunction)
166
- throw new Error('Task function is not a function')
167
- }
168
- //console.log("SUBTASK RUN", taskFunction.definition.name, props)
169
- const subtaskProgress = { current: 0, total: 1, factor: progressFactor }
170
- subtasksProgress.push(subtaskProgress)
171
- const result = await taskFunction(
172
- props,
173
- {
174
- ...context,
175
- taskObject: undefined,
176
- task: taskObject.id,
177
- causeType: 'task_Task',
178
- cause: taskObject.id
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
- (current, total, action) => {
183
- subtaskProgress.current = current
184
- subtaskProgress.total = total
185
- updateProgress()
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
- updateProgress()
199
- }
200
- },
201
- async trigger(trigger, props) {
202
- return await app.trigger({
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 triggerService(trigger, props, returnArray = false) {
209
- return await app.triggerService(trigger, props, returnArray)
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({