@mbc-cqrs-serverless/task 0.1.49-beta.0 → 0.1.50-beta.0
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/README.md +199 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -2,9 +2,207 @@
|
|
|
2
2
|
|
|
3
3
|
# MBC CQRS serverless framework Task package
|
|
4
4
|
|
|
5
|
+
## Description
|
|
6
|
+
|
|
7
|
+
The Task package provides comprehensive task management functionality in the MBC CQRS Serverless framework. It enables:
|
|
8
|
+
|
|
9
|
+
- Asynchronous task execution
|
|
10
|
+
- Task status tracking
|
|
11
|
+
- Progress monitoring
|
|
12
|
+
- Error handling and retries
|
|
13
|
+
- Task queue management
|
|
14
|
+
- Task history and logging
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @mbc-cqrs-serverless/task
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Setup
|
|
25
|
+
|
|
26
|
+
1. Import and configure the task module:
|
|
27
|
+
```typescript
|
|
28
|
+
import { TaskModule } from '@mbc-cqrs-serverless/task';
|
|
29
|
+
import { Module } from '@nestjs/common';
|
|
30
|
+
|
|
31
|
+
@Module({
|
|
32
|
+
imports: [
|
|
33
|
+
TaskModule.forRoot({
|
|
34
|
+
queueUrl: process.env.TASK_QUEUE_URL,
|
|
35
|
+
region: 'ap-northeast-1',
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
})
|
|
39
|
+
export class AppModule {}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Creating Tasks
|
|
43
|
+
|
|
44
|
+
1. Define a task:
|
|
45
|
+
```typescript
|
|
46
|
+
import { Task, TaskMetadata } from '@mbc-cqrs-serverless/task';
|
|
47
|
+
|
|
48
|
+
@Task({
|
|
49
|
+
name: 'ProcessOrder',
|
|
50
|
+
maxRetries: 3,
|
|
51
|
+
timeout: 300, // 5 minutes
|
|
52
|
+
})
|
|
53
|
+
export class ProcessOrderTask {
|
|
54
|
+
async execute(
|
|
55
|
+
data: any,
|
|
56
|
+
metadata: TaskMetadata
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
// Task implementation
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
2. Schedule a task:
|
|
64
|
+
```typescript
|
|
65
|
+
import { TaskService } from '@mbc-cqrs-serverless/task';
|
|
66
|
+
|
|
67
|
+
@Injectable()
|
|
68
|
+
export class OrderService {
|
|
69
|
+
constructor(
|
|
70
|
+
private readonly taskService: TaskService
|
|
71
|
+
) {}
|
|
72
|
+
|
|
73
|
+
async processOrder(orderId: string): Promise<void> {
|
|
74
|
+
await this.taskService.schedule('ProcessOrder', {
|
|
75
|
+
orderId,
|
|
76
|
+
items: [],
|
|
77
|
+
// ... other data
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Task Status Tracking
|
|
84
|
+
|
|
85
|
+
1. Monitor task status:
|
|
86
|
+
```typescript
|
|
87
|
+
@Injectable()
|
|
88
|
+
export class TaskMonitor {
|
|
89
|
+
constructor(
|
|
90
|
+
private readonly taskService: TaskService
|
|
91
|
+
) {}
|
|
92
|
+
|
|
93
|
+
async checkTaskStatus(taskId: string): Promise<TaskStatus> {
|
|
94
|
+
const task = await this.taskService.getTask(taskId);
|
|
95
|
+
return task.status;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async getTaskProgress(taskId: string): Promise<number> {
|
|
99
|
+
const task = await this.taskService.getTask(taskId);
|
|
100
|
+
return task.progress || 0;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Progress Updates
|
|
106
|
+
|
|
107
|
+
1. Update task progress:
|
|
108
|
+
```typescript
|
|
109
|
+
@Task({
|
|
110
|
+
name: 'BatchProcess',
|
|
111
|
+
})
|
|
112
|
+
export class BatchProcessTask {
|
|
113
|
+
async execute(
|
|
114
|
+
data: any,
|
|
115
|
+
metadata: TaskMetadata
|
|
116
|
+
): Promise<void> {
|
|
117
|
+
const total = data.items.length;
|
|
118
|
+
|
|
119
|
+
for (let i = 0; i < total; i++) {
|
|
120
|
+
await this.processItem(data.items[i]);
|
|
121
|
+
await metadata.updateProgress((i + 1) / total * 100);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Error Handling and Retries
|
|
128
|
+
|
|
129
|
+
1. Configure retry behavior:
|
|
130
|
+
```typescript
|
|
131
|
+
@Task({
|
|
132
|
+
name: 'SendEmail',
|
|
133
|
+
maxRetries: 3,
|
|
134
|
+
retryDelay: 60, // 1 minute
|
|
135
|
+
retryStrategy: 'exponential',
|
|
136
|
+
})
|
|
137
|
+
export class SendEmailTask {
|
|
138
|
+
async execute(
|
|
139
|
+
data: any,
|
|
140
|
+
metadata: TaskMetadata
|
|
141
|
+
): Promise<void> {
|
|
142
|
+
try {
|
|
143
|
+
await this.emailService.send(data);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (error.retryable) {
|
|
146
|
+
throw new RetryableError(error.message);
|
|
147
|
+
}
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Queue Management
|
|
155
|
+
|
|
156
|
+
1. Work with multiple queues:
|
|
157
|
+
```typescript
|
|
158
|
+
@Injectable()
|
|
159
|
+
export class WorkflowService {
|
|
160
|
+
constructor(
|
|
161
|
+
private readonly taskService: TaskService
|
|
162
|
+
) {}
|
|
163
|
+
|
|
164
|
+
async scheduleWorkflow(): Promise<void> {
|
|
165
|
+
// High priority queue
|
|
166
|
+
await this.taskService.schedule('CriticalTask', data, {
|
|
167
|
+
queueUrl: process.env.HIGH_PRIORITY_QUEUE_URL,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Default queue
|
|
171
|
+
await this.taskService.schedule('NormalTask', data);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Task History and Logging
|
|
177
|
+
|
|
178
|
+
1. Access task history:
|
|
179
|
+
```typescript
|
|
180
|
+
@Injectable()
|
|
181
|
+
export class AuditService {
|
|
182
|
+
constructor(
|
|
183
|
+
private readonly taskService: TaskService
|
|
184
|
+
) {}
|
|
185
|
+
|
|
186
|
+
async getTaskHistory(taskId: string): Promise<TaskHistory[]> {
|
|
187
|
+
const history = await this.taskService.getTaskHistory(taskId);
|
|
188
|
+
return history;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async getTaskLogs(taskId: string): Promise<TaskLog[]> {
|
|
192
|
+
const logs = await this.taskService.getTaskLogs(taskId);
|
|
193
|
+
return logs;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
5
198
|
## Documentation
|
|
6
199
|
|
|
7
|
-
Visit https://mbc-cqrs-serverless.mbc-net.com/ to view the full documentation
|
|
200
|
+
Visit https://mbc-cqrs-serverless.mbc-net.com/ to view the full documentation, including:
|
|
201
|
+
- Task configuration
|
|
202
|
+
- Queue management
|
|
203
|
+
- Error handling strategies
|
|
204
|
+
- Monitoring and logging
|
|
205
|
+
- API reference
|
|
8
206
|
|
|
9
207
|
## License
|
|
10
208
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mbc-cqrs-serverless/task",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.50-beta.0",
|
|
4
4
|
"description": "long-running task",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mbc",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"fargate",
|
|
16
16
|
"step-functions",
|
|
17
17
|
"sqs",
|
|
18
|
-
"
|
|
18
|
+
"typescript"
|
|
19
19
|
],
|
|
20
20
|
"main": "./dist/index.js",
|
|
21
21
|
"types": "./dist/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"access": "public"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@mbc-cqrs-serverless/core": "^0.1.
|
|
44
|
+
"@mbc-cqrs-serverless/core": "^0.1.50-beta.0"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "d61475400eb1b00a1c427769875d2f056e48c73c"
|
|
47
47
|
}
|