-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstances.service.ts
More file actions
241 lines (218 loc) · 7.23 KB
/
instances.service.ts
File metadata and controls
241 lines (218 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
BadRequestException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { WorkflowInstance } from '../schemas/instance.schema';
import { CreateWorkflowInstanceDto } from '../dto/create-instance.dto';
import { WorkflowsService } from './workflows.service';
import { TriggerSnapshot } from '../entities/trigger-snapshot.entity';
import { Workflow } from '../entities/workflow.entity';
@Injectable()
export class WorkflowInstancesService {
constructor(
@InjectModel(WorkflowInstance.name)
private readonly workflowInstanceModel: Model<WorkflowInstance>,
private readonly workflowsService: WorkflowsService,
) {}
// Create a new workflow instance
async create(
ownerId: string,
createWorkflowInstanceDto: CreateWorkflowInstanceDto,
): Promise<WorkflowInstance> {
const workflow = await this.workflowsService.findOne(
createWorkflowInstanceDto.workflowId,
);
const workflowInstance = await this.workflowInstanceModel.create({
...createWorkflowInstanceDto,
workflow: workflow._id,
owner: ownerId,
});
return this.workflowInstanceModel
.findById(workflowInstance._id)
.populate('workflow')
.exec();
}
// Find all workflow instances
async findAll(ownerId: any): Promise<WorkflowInstance[]> {
return this.workflowInstanceModel
.find({ owner: ownerId })
.populate('workflow')
.exec();
}
// Find a specific workflow instance by ID
async findOne(id: string): Promise<WorkflowInstance> {
return this.workflowInstanceModel.findById(id).populate('workflow').exec();
}
async findAndApply(id: string): Promise<Workflow> {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
const workflow = instance.workflow;
const appliedWorkflow = traverseAndInterpolate(
workflow.toJSON(),
instance.params,
);
return appliedWorkflow;
}
// Update a workflow instance
async storeTriggerSnapsot(
id: string,
snapshot: TriggerSnapshot,
): Promise<TriggerSnapshot> {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
// console.log('instance.triggerSnapshots', instance.triggerSnapshots);
if (!instance.triggerSnapshots) {
instance.triggerSnapshots = new Map<string, TriggerSnapshot>();
}
console.log('instance.triggerSnapshots', instance.triggerSnapshots);
const oldSnapshot = instance.triggerSnapshots.get(snapshot.name);
instance.triggerSnapshots.set(snapshot.name, snapshot);
await instance.save();
return oldSnapshot;
}
async start(id: string): Promise<WorkflowInstance> {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
if (instance.status === 'running') {
throw new BadRequestException(
`WorkflowInstance with ID ${id} is already running`,
);
}
instance.status = 'running';
instance.startedAt = new Date();
await instance.save();
return instance;
}
async stop(id: string): Promise<WorkflowInstance> {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
if (instance.status !== 'running') {
throw new BadRequestException(
`WorkflowInstance with ID ${id} is not running`,
);
}
instance.status = 'stopped';
instance.completedAt = new Date();
await instance.save();
return instance;
}
async reset(id: any) {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
if (
instance.status == 'stopped' ||
instance.status === 'completed' ||
instance.status === 'pending'
) {
throw new BadRequestException(
`WorkflowInstance with ID ${id} cannot be reset with status ${instance.status}`,
);
}
// reset the instance to the initial state
instance.currentStepIndex = 0;
instance.triggerSnapshots = new Map<string, TriggerSnapshot>();
await instance.save();
console.log('instance', instance);
return instance;
}
async findByStatus(status: string): Promise<WorkflowInstance[]> {
return this.workflowInstanceModel
.find({ status })
.populate('workflow')
.exec();
}
// Update a workflow instance
async update(id: string, updateWorkflowInstanceDto: any) {
return `This action updates a #${id} workflow instance`;
}
// Remove a workflow instance
async remove(id: string) {
return `This action removes a #${id} workflow instance`;
}
async startProcessing(id: string) {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
if (instance.isProcessing) {
return false;
}
instance.isProcessing = true;
instance.startProcessingAt = new Date();
instance.save();
return true;
}
async stopProcessing(id: string) {
const instance = await this.findOne(id);
if (!instance) {
throw new NotFoundException(`WorkflowInstance with ID ${id} not found`);
}
instance.isProcessing = false;
instance.currentStepIndex++;
if (instance.currentStepIndex >= instance.workflow.steps.length) {
console.log(
`Instance ${instance.id} has completed all steps. Setting status to completed.`,
);
// instance.status = 'completed';
instance.currentStepIndex = 0;
instance.completedAt = new Date();
}
instance.save();
return true;
}
async isSafeToStop(id: string): Promise<boolean> {
const PROCESSING_TIMEOUT = 50000;
const instance = await this.findOne(id);
if (!instance.startProcessingAt || !instance.isProcessing) {
return true;
}
const processingDuration =
new Date().getTime() - instance.startProcessingAt.getTime();
const isSafe = processingDuration > PROCESSING_TIMEOUT;
console.log(
`Processing duration: ${processingDuration} ms, timeout is ${PROCESSING_TIMEOUT} ms. Is safe to stop: ${isSafe}`,
);
return isSafe;
}
}
function traverseAndInterpolate(obj: any, params: Map<string, string>): any {
if (typeof obj === 'string') {
// console.log('obj', obj);
return obj.replace(/\$\w+/g, (match) => {
// console.log('match', match);
const key = match.slice(1); // Remove the $ sign
// console.log('key', key);
// console.log('key', params.get(key));
// console.log(params[key] !== undefined ? params[key] : match)
return params.has(key) ? params.get(key) : match;
});
}
if (Array.isArray(obj)) {
return obj.map((item) => traverseAndInterpolate(item, params));
}
if (typeof obj === 'object' && obj !== null) {
const result: any = {};
for (const [key, value] of Object.entries(obj)) {
if (key === '_id') {
result[key] = value;
} else {
result[key] = traverseAndInterpolate(value, params);
}
}
return result;
}
return obj;
}