This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ObjectActionHandler.js
74 lines (67 loc) · 1.9 KB
/
ObjectActionHandler.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
const { AbstractActionHandler } = require("demux")
const mongoose = require('mongoose')
const STOP_AT = parseInt(process.env.STOP_AT)
// Initial state
let state = {
from: '',
to: '',
quantity: '',
currency: '',
memo: '',
trx_id: '',
blockNumber: 0,
blockHash: '',
handlerVersionName: 'v1'
}
function stopAt(blockNumber) {
// Function stop the service when meet the STOP_AT block number
if (blockNumber >= STOP_AT) {
console.log("\n####################\n# STOP AT: ", blockNumber)
console.log("####################\n")
process.exit(1)
}
}
class ObjectActionHandler extends AbstractActionHandler {
constructor([handleVersion], uri, username, password) {
super([handleVersion])
let options = {
useNewUrlParser: true,
useCreateIndex: true,
user: username,
pass: password
}
mongoose.connect(uri, options)
mongoose.set('debug', true);
mongoose.connection.on('connected', () => {
console.info(`Mongoose default connection open to ${uri}`)
})
mongoose.connection.on('error', console.error.bind(console, 'Mongoose default connection error:'))
mongoose.connection.on('disconnected', () => {
console.info('Mongoose default connection disconnected')
})
process.on('SIGINT', () => {
mongoose.connection.close(() => {
console.info('Mongoose default connection disconnected through app termination')
process.exit(0)
})
})
}
async handleWithState (handle) {
try {
await handle(state)
} catch (err) {
console.error(err)
}
}
async loadIndexState() {
return state
}
async updateIndexState(stateObj, block, isReplay, handlerVersionName) {
console.log("Processing block: ", block.blockInfo.blockNumber)
stateObj.handlerVersionName = handlerVersionName
if (STOP_AT) {
stopAt(block.blockInfo.blockNumber)
}
}
}
module.exports = ObjectActionHandler