-
Notifications
You must be signed in to change notification settings - Fork 2
/
TransactionManager.js
73 lines (60 loc) · 1.92 KB
/
TransactionManager.js
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
function TransactionsManager(){
const serverConfig = require("apihub").getServerConfig();
const config = serverConfig.componentsConfig["dsu-wizard"];
const WorkerPoolManager = require("./WorkerPoolManager.js");
const {persistTransaction, getTransaction, getWorkerScript} = require("./TransactionUtils");
const numberOfWorkers = config.workers || 5;
const poolManager = new WorkerPoolManager(getWorkerScript(), numberOfWorkers);
this.persistTransaction = persistTransaction;
this.getTransaction = getTransaction;
this.beginTransaction = function(req, callback){
const crypto = require("pskcrypto");
const randSize = require("./constants").transactionIdLength;
let transactionId = crypto.randomBytes(randSize).toString('hex');
let transaction = {
id: transactionId,
commands: [],
context: {
result: {},
dlDomain: req.params.domain,
domain: req.params.domain,
options: {useSSIAsIdentifier: false}
}
};
persistTransaction(transaction, (err) => {
if(err){
return callback(err);
}
callback(undefined, transactionId);
});
}
this.addCommandToTransaction = function(transactionId, command, callback){
getTransaction(transactionId, (err, transaction)=>{
if(!transaction || err){
callback('Transaction could not be found');
}
transaction.commands.push(command);
persistTransaction(transaction, (err)=>{
if(err){
return callback(err);
}
callback();
});
});
}
this.closeTransaction = function (transactionId, authorization, callback) {
getTransaction(transactionId, (err, transaction) => {
if (typeof transaction === "undefined" || err) {
return callback(Error('Transaction could not be found'));
}
poolManager.runTask({transactionId, authorization}, (err, taskResult)=>{
if(err){
return callback(err);
}
let {error, result} = taskResult;
callback(error, result);
});
});
}
}
module.exports = new TransactionsManager();