-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
135 lines (103 loc) · 4.34 KB
/
index.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
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
/**
* Middleware service for handling user balance.
* Update balances for accounts, which addresses were specified
* in received transactions from blockParser via amqp
*
* @module Chronobank/nem-balance-processor
* @requires config
* @requires models/accountModel
*
* Copyright 2017–2018, LaborX PTY
* Licensed under the AGPL Version 3 license.
* @author Kirill Sergeev <cloudkserg11@gmail.com>
*/
const _ = require('lodash'),
Promise = require('bluebird'),
mongoose = require('mongoose'),
bunyan = require('bunyan'),
amqp = require('amqplib'),
AmqpService = require('middleware_common_infrastructure/AmqpService'),
InfrastructureInfo = require('middleware_common_infrastructure/InfrastructureInfo'),
InfrastructureService = require('middleware_common_infrastructure/InfrastructureService'),
config = require('./config'),
getUpdatedBalance = require('./utils/balance/getUpdatedBalance'),
converters = require('./utils/converters/converters'),
models = require('./models'),
providerService = require('./services/providerService'),
log = bunyan.createLogger({name: 'core.balanceProcessor', level: config.logs.level});
const TX_QUEUE = `${config.rabbit.serviceName}_transaction`;
mongoose.Promise = Promise;
mongoose.connect(config.mongo.accounts.uri, {useMongoClient: true});
const runSystem = async function () {
const rabbit = new AmqpService(
config.systemRabbit.url,
config.systemRabbit.exchange,
config.systemRabbit.serviceName
);
const info = new InfrastructureInfo(require('./package.json'), config.system.waitTime);
const system = new InfrastructureService(info, rabbit, {checkInterval: 10000});
await system.start();
system.on(system.REQUIREMENT_ERROR, (requirement, version) => {
log.error(`Not found requirement with name ${requirement.name} version=${requirement.version}.` +
` Last version of this middleware=${version}`);
process.exit(1);
});
await system.checkRequirements();
system.periodicallyCheck();
};
const init = async () => {
if (config.checkSystem)
await runSystem();
mongoose.connection.on('disconnected', () => {
throw new Error('mongo disconnected!');
});
models.init();
let conn = await amqp.connect(config.rabbit.url);
let channel = await conn.createChannel();
channel.on('close', () => {
throw new Error('rabbitmq process has finished!');
});
await channel.assertExchange('events', 'topic', {durable: false});
await channel.assertExchange('internal', 'topic', {durable: false});
await channel.assertQueue(`${config.rabbit.serviceName}.balance_processor`);
await channel.bindQueue(`${config.rabbit.serviceName}.balance_processor`, 'events', `${TX_QUEUE}.*`);
await channel.bindQueue(`${config.rabbit.serviceName}.balance_processor`, 'internal', `${config.rabbit.serviceName}_user.created`);
await providerService.setRabbitmqChannel(channel, config.rabbit.serviceName);
channel.prefetch(2);
channel.consume(`${config.rabbit.serviceName}.balance_processor`, async (data) => {
try {
if (!data)
return false;
const parsedData = JSON.parse(data.content.toString());
const addr = data.fields.routingKey.slice(TX_QUEUE.length + 1) || parsedData.address;
let account = await models.accountModel.findOne({address: addr});
if (!account)
return channel.ack(data);
const updatedBalance = await getUpdatedBalance(account.address, _.get(account, 'mosaics', {}), parsedData.hash ? data : null);
if (updatedBalance.balance)
account.balance = updatedBalance.balance;
if (updatedBalance.mosaics)
account.mosaics = updatedBalance.mosaics;
account.markModified('mosaics');
account.save();
let convertedBalance = converters.convertBalanceWithDivisibility(updatedBalance.balance);
let convertedMosaics = converters.convertMosaicsWithDivisibility(updatedBalance.mosaics);
let message = {
address: addr,
balance: convertedBalance,
mosaics: convertedMosaics
};
if (parsedData.hash)
message.tx = parsedData;
await channel.publish('events', `${config.rabbit.serviceName}_balance.${addr}`, new Buffer(JSON.stringify(message)));
} catch (e) {
log.error(e);
return channel.nack(data);
}
channel.ack(data);
});
};
module.exports = init().catch(err => {
log.error(err);
process.exit(0);
});