@live-change/task-service 0.8.24 → 0.8.26

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 (5) hide show
  1. package/LICENSE.md +11 -0
  2. package/index.js +1 -0
  3. package/model.js +125 -0
  4. package/package.json +4 -4
  5. package/task.js +26 -100
package/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019-2024 Michał Łaszczewski
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/index.js CHANGED
@@ -3,6 +3,7 @@ const app = App.app()
3
3
 
4
4
  import definition from './definition.js'
5
5
 
6
+ import './model.js'
6
7
  import './task.js'
7
8
 
8
9
  export default definition
package/model.js ADDED
@@ -0,0 +1,125 @@
1
+ import App from '@live-change/framework'
2
+ const app = App.app()
3
+
4
+ import definition from './definition.js'
5
+
6
+ const taskProperties = {
7
+ name: {
8
+ type: String,
9
+ validation: ['nonEmpty']
10
+ },
11
+ definition: {
12
+ type: Object
13
+ },
14
+ properties: {
15
+ type: Object
16
+ },
17
+ result: {
18
+ type: Object
19
+ },
20
+ hash: {
21
+ type: String,
22
+ },
23
+ state: {
24
+ type: String
25
+ },
26
+ startedAt: {
27
+ type: Date
28
+ },
29
+ createdAt: {
30
+ type: Date,
31
+ default: () => new Date()
32
+ },
33
+ doneAt: {
34
+ type: Date
35
+ },
36
+ retries: {
37
+ type: Array,
38
+ of: {
39
+ type: Object,
40
+ properties: {
41
+ startedAt: {
42
+ type: Date
43
+ },
44
+ failedAt: {
45
+ type: Date
46
+ },
47
+ error: {
48
+ type: String
49
+ }
50
+ }
51
+ },
52
+ default: []
53
+ },
54
+ progress: {
55
+ type: Object,
56
+ properties: {
57
+ current: {
58
+ type: Number
59
+ },
60
+ total: {
61
+ type: Number
62
+ }
63
+ }
64
+ }
65
+ }
66
+
67
+ const Task = definition.model({
68
+ name: 'Task',
69
+ itemOfAny: {
70
+ to: 'cause',
71
+ readAccess: () => true,
72
+ },
73
+ properties: {
74
+ ...taskProperties
75
+ },
76
+ indexes: {
77
+ byCauseAndHash: {
78
+ property: ['causeType', 'cause', 'hash']
79
+ },
80
+ byCauseAndState: {
81
+ property: ['causeType', 'cause', 'state']
82
+ }
83
+ }
84
+ })
85
+
86
+ definition.view({
87
+ name: 'tasksByCauseAndHash',
88
+ internal: true,
89
+ properties: {
90
+ causeType: {
91
+ type: String
92
+ },
93
+ cause: {
94
+ type: String
95
+ },
96
+ hash: {
97
+ type: String
98
+ }
99
+ },
100
+ returns: {
101
+ type: Array,
102
+ of: {
103
+ type: Task
104
+ }
105
+ },
106
+ async daoPath({ hash }) {
107
+ return Task.indexRangePath('byCauseAndHash', [causeType, cause, hash], { limit: 23 })
108
+ }
109
+ })
110
+
111
+ definition.view({
112
+ name: 'task',
113
+ internal: true,
114
+ properties: {
115
+ task: {
116
+ type: String
117
+ }
118
+ },
119
+ returns: {
120
+ type: Task
121
+ },
122
+ async daoPath({ task }) {
123
+ return Task.path(task)
124
+ }
125
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/task-service",
3
- "version": "0.8.24",
3
+ "version": "0.8.26",
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.24",
26
- "@live-change/relations-plugin": "^0.8.24"
25
+ "@live-change/framework": "^0.8.26",
26
+ "@live-change/relations-plugin": "^0.8.26"
27
27
  },
28
- "gitHead": "63e942caccbcb1c9bfbd1a3ef1d097124514c5a7"
28
+ "gitHead": "e62696aa9fa6e5b86312dfb64c97efa300d41147"
29
29
  }
package/task.js CHANGED
@@ -1,89 +1,6 @@
1
1
  import App from '@live-change/framework'
2
2
  const app = App.app()
3
3
 
4
- import definition from './definition.js'
5
- import crypto from 'crypto'
6
-
7
- const taskProperties = {
8
- name: {
9
- type: String,
10
- validation: ['nonEmpty']
11
- },
12
- definition: {
13
- type: Object
14
- },
15
- properties: {
16
- type: Object
17
- },
18
- result: {
19
- type: Object
20
- },
21
- hash: {
22
- type: String,
23
- },
24
- state: {
25
- type: String
26
- },
27
- startedAt: {
28
- type: Date
29
- },
30
- createdAt: {
31
- type: Date,
32
- default: () => new Date()
33
- },
34
- doneAt: {
35
- type: Date
36
- },
37
- retries: {
38
- type: Array,
39
- of: {
40
- type: Object,
41
- properties: {
42
- startedAt: {
43
- type: Date
44
- },
45
- failedAt: {
46
- type: Date
47
- },
48
- error: {
49
- type: String
50
- }
51
- }
52
- },
53
- default: []
54
- },
55
- progress: {
56
- type: Object,
57
- properties: {
58
- current: {
59
- type: Number
60
- },
61
- total: {
62
- type: Number
63
- }
64
- }
65
- }
66
- }
67
-
68
- const Task = definition.model({
69
- name: 'Task',
70
- itemOfAny: {
71
- to: 'cause',
72
- readAccess: () => true,
73
- },
74
- properties: {
75
- ...taskProperties
76
- },
77
- indexes: {
78
- byCauseAndHash: {
79
- property: ['causeType', 'cause', 'hash']
80
- },
81
- byCauseAndState: {
82
- property: ['causeType', 'cause', 'state']
83
- }
84
- }
85
- })
86
-
87
4
  const task = (taskDefinition) => { /// TODO: modify to use triggers
88
5
  return async (props, context, emit) => {
89
6
  if(!emit) emit = (events) => app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {})
@@ -93,36 +10,44 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
93
10
  .update(taskDefinition.name + ':' + propertiesJson)
94
11
  .digest('hex')
95
12
 
96
- const similarTasks = Task.indexRangeGet('byCauseAndHash',
97
- [context.causeType, context.cause, hash],
98
- { limit: 23 } // sha256 collisions are very unlikely
99
- )
100
-
13
+ const similarTasks = App.serviceViewGet('task', 'tasksByCauseAndHash', {
14
+ causeType: context.causeType,
15
+ cause: context.cause,
16
+ hash
17
+ })
101
18
  const oldTask = similarTasks.find(similarTask => similarTask.name === taskDefinition.name
102
19
  && JSON.stringify(similarTask.properties) === propertiesJson)
103
20
 
104
- let taskObject = oldTask ? await Task.get(oldTask.id) : {
105
- id: app.generateUid(),
106
- name: taskDefinition.name,
107
- properties: props,
108
- hash,
109
- state: 'created'
110
- }
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
+ }
111
30
 
112
31
  if(!oldTask) {
113
32
  /// app.emitEvents
114
- await Task.create({
33
+ await App.triggerService('task', 'task_createCaseOwnedTask', {
115
34
  ...taskObject,
116
35
  causeType: context.causeType,
117
- cause: context.cause
36
+ cause: context.cause,
37
+ task: taskObject.id
118
38
  })
119
39
  }
120
40
 
121
41
  const maxRetries = taskDefinition.maxRetries ?? 5
122
42
 
123
43
  async function updateTask(data) {
124
- await Task.update(taskObject.id, data)
125
- taskObject = await Task.get(taskObject.id)
44
+ await App.triggerService('task', 'task_updateCaseOwnedTask', {
45
+ ...data,
46
+ causeType: context.causeType,
47
+ cause: context.cause,
48
+ task: taskObject.id
49
+ })
50
+ taskObject = await App.serviceViewGet('task', 'task', { task: oldTask.to })
126
51
  }
127
52
 
128
53
  const runTask = async () => {
@@ -139,7 +64,8 @@ const task = (taskDefinition) => { /// TODO: modify to use triggers
139
64
  ...context,
140
65
  causeType: definition.name + '_Task',
141
66
  cause: taskObject.id
142
- }, (events) => app.emitEvents(definition.name, Array.isArray(events) ? events : [events], {}))
67
+ }, (events) => app.emitEvents(definition.name,
68
+ Array.isArray(events) ? events : [events], {}))
143
69
  },
144
70
  async progress(current, total) {
145
71
  await updateTask({