-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathoperation-id-cleaner-command.js
More file actions
101 lines (94 loc) · 3.29 KB
/
operation-id-cleaner-command.js
File metadata and controls
101 lines (94 loc) · 3.29 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
import Command from '../command.js';
import {
BYTES_IN_KILOBYTE,
OPERATION_ID_FILES_FOR_REMOVAL_MAX_NUMBER,
OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS,
OPERATION_ID_MEMORY_CLEANUP_TIME_MILLS,
OPERATION_ID_STATUS,
COMMAND_PRIORITY,
} from '../../constants/constants.js';
/**
* Increases approval for Bidding contract on blockchain
*/
class OperationIdCleanerCommand extends Command {
constructor(ctx) {
super(ctx);
this.logger = ctx.logger;
this.repositoryModuleManager = ctx.repositoryModuleManager;
this.fileService = ctx.fileService;
}
/**
* Executes command and produces one or more events
* @param command
*/
async execute() {
let memoryBytes = 0;
let fileBytes = 0;
try {
memoryBytes = this.operationIdService.getOperationIdMemoryCacheSizeBytes();
} catch (error) {
this.logger.warn(`Unable to read memory cache footprint: ${error.message}`);
}
try {
fileBytes = await this.operationIdService.getOperationIdFileCacheSizeBytes();
} catch (error) {
this.logger.warn(`Unable to read file cache footprint: ${error.message}`);
}
const bytesInMegabyte = 1024 * 1024;
this.logger.debug(
`Operation cache footprint before cleanup: memory=${(
memoryBytes / bytesInMegabyte
).toFixed(2)}MB, files=${(fileBytes / bytesInMegabyte).toFixed(2)}MB`,
);
this.logger.debug('Starting command for removal of expired cache files');
const timeToBeDeleted = Date.now() - OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS;
await this.repositoryModuleManager.removeOperationIdRecord(timeToBeDeleted, [
OPERATION_ID_STATUS.COMPLETED,
OPERATION_ID_STATUS.FAILED,
]);
let removed = await this.operationIdService.removeExpiredOperationIdMemoryCache(
OPERATION_ID_MEMORY_CLEANUP_TIME_MILLS,
);
if (removed) {
this.logger.debug(
`Successfully removed ${
removed / BYTES_IN_KILOBYTE
} Kbs expired cached operation entries from memory`,
);
}
removed = await this.operationIdService.removeExpiredOperationIdFileCache(
OPERATION_ID_COMMAND_CLEANUP_TIME_MILLS,
OPERATION_ID_FILES_FOR_REMOVAL_MAX_NUMBER,
);
if (removed) {
this.logger.debug(`Successfully removed ${removed} expired cached operation files`);
}
return Command.repeat();
}
/**
* Recover system from failure
* @param command
* @param error
*/
async recover(command) {
this.logger.warn(`Failed to clean operation ids table: error: ${command.message}`);
return Command.repeat();
}
/**
* Builds default command
* @param map
* @returns {{add, data: *, delay: *, deadline: *}}
*/
default(map) {
const command = {
name: 'operationIdCleanerCommand',
period: OPERATION_ID_MEMORY_CLEANUP_TIME_MILLS,
data: {},
transactional: false,
priority: COMMAND_PRIORITY.LOWEST,
};
Object.assign(command, map);
return command;
}
}
export default OperationIdCleanerCommand;