-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathoperation-service.js
More file actions
146 lines (124 loc) · 4.51 KB
/
operation-service.js
File metadata and controls
146 lines (124 loc) · 4.51 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
import {
OPERATION_ID_STATUS,
OPERATION_REQUEST_STATUS,
OPERATION_STATUS,
} from '../constants/constants.js';
class OperationService {
constructor(ctx) {
this.logger = ctx.logger;
this.repositoryModuleManager = ctx.repositoryModuleManager;
this.operationIdService = ctx.operationIdService;
this.commandExecutor = ctx.commandExecutor;
}
getOperationName() {
return this.operationName;
}
getNetworkProtocols() {
return this.networkProtocols;
}
async getOperationStatus(operationId) {
return this.repositoryModuleManager.getOperationStatus(
this.getOperationName(),
operationId,
);
}
async getResponsesStatuses(responseStatus, errorMessage, operationId) {
let responses = 0;
const self = this;
await this.operationMutex.runExclusive(async () => {
await self.repositoryModuleManager.createOperationResponseRecord(
responseStatus,
this.operationName,
operationId,
errorMessage,
);
responses = await self.repositoryModuleManager.getOperationResponsesStatuses(
this.operationName,
operationId,
);
});
const operationIdStatuses = {};
for (const response of responses) {
if (!operationIdStatuses[operationId])
operationIdStatuses[operationId] = { failedNumber: 0, completedNumber: 0 };
if (response.status === OPERATION_REQUEST_STATUS.FAILED) {
operationIdStatuses[operationId].failedNumber += 1;
} else {
operationIdStatuses[operationId].completedNumber += 1;
}
}
return operationIdStatuses;
}
async markOperationAsCompleted(
operationId,
blockchain,
responseData,
endStatuses,
options = {},
) {
const { reuseExistingCache = false } = options;
this.logger.info(`Finalizing ${this.operationName} for operationId: ${operationId}`);
await this.repositoryModuleManager.updateOperationStatus(
this.operationName,
operationId,
OPERATION_STATUS.COMPLETED,
);
if (responseData === null) {
await this.operationIdService.removeOperationIdCache(operationId);
} else {
await this.operationIdService.cacheOperationIdDataToMemory(operationId, responseData);
if (!reuseExistingCache) {
await this.operationIdService.cacheOperationIdDataToFile(operationId, responseData);
}
}
for (let i = 0; i < endStatuses.length; i += 1) {
const status = endStatuses[i];
const response = {
status,
};
this.operationIdService.emitChangeEvent(status, operationId, blockchain);
if (i === endStatuses.length - 1) {
// eslint-disable-next-line no-await-in-loop
await this.repositoryModuleManager.updateOperationIdRecord(response, operationId);
}
}
}
async markOperationAsFailed(operationId, blockchain, message, errorType) {
this.logger.info(`${this.operationName} for operationId: ${operationId} failed.`);
await this.operationIdService.removeOperationIdCache(operationId);
await this.repositoryModuleManager.updateOperationStatus(
this.operationName,
operationId,
OPERATION_STATUS.FAILED,
);
await this.operationIdService.updateOperationIdStatus(
operationId,
blockchain,
OPERATION_ID_STATUS.FAILED,
message,
errorType,
);
}
async scheduleOperationForLeftoverNodes(commandData, leftoverNodes) {
await this.commandExecutor.add({
name: `${this.operationName}ScheduleMessagesCommand`,
delay: 0,
data: { ...commandData, leftoverNodes },
transactional: false,
});
}
logResponsesSummary(completedNumber, failedNumber) {
this.logger.info(
`Total number of responses: ${
failedNumber + completedNumber
}, failed: ${failedNumber}, completed: ${completedNumber}`,
);
}
getBatchSize() {
throw Error('getBatchSize not implemented');
}
getMinAckResponses() {
throw Error('getMinAckResponses not implemented');
}
}
export default OperationService;