This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
forked from shopglobal/turtlecoind-ha
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.js
127 lines (107 loc) · 3.15 KB
/
service.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
// Copyright (c) 2018-2019, Brandon Lehmann, The TurtleCoin Developers
//
// Please see the included LICENSE file for more information.
'use strict'
const Plenteumd = require('./')
const util = require('util')
const metrics = []
try {
const pm2Metrics = require('@pm2/io')
log('@pm2/io module installed, enabling custom metrics...')
const metricSet = [
{ name: 'Status', unit: false },
{ name: 'Progress', unit: 'percent' },
{ name: 'Blockheight', unit: 'blocks' },
{ name: 'Net hash', unit: 'h/s' },
{ name: 'Difficulty', unit: false }
]
metricSet.forEach((metric) => {
metrics.push(pm2Metrics.metric({
name: metric.name,
unit: metric.unit
}))
})
} catch (error) {
log('@pm2/io module not installed, ignoring...')
}
var daemon = new Plenteumd({
loadCheckpoints: './checkpoints.csv'
// Load additional daemon parameters here
})
function log (message) {
console.log(util.format('%s: %s', (new Date()).toUTCString(), message))
}
function resetMetrics (metrics) {
metrics.forEach((metric) => {
metric.set(undefined)
})
}
daemon.on('start', (args) => {
log(util.format('Plenteumd has started... %s', args))
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set('starting')
}
})
daemon.on('started', () => {
log('Plenteumd is attempting to synchronize with the network...')
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set('started')
}
})
daemon.on('syncing', (info) => {
log(util.format('Plenteumd has synchronized %s out of %s blocks [%s%]', info.height, info.network_height, info.percent))
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set('synchronizing')
metrics[1].set(`${info.height}/${info.network_height} (${info.percent}%)`)
}
})
daemon.on('synced', () => {
log('Plenteumd is synchronized with the network...')
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set('synchronized')
}
})
daemon.on('ready', (info) => {
log(util.format('Plenteumd is waiting for connections at %s @ %s - %s H/s', info.height, info.difficulty, info.globalHashRate))
if (metrics.length !== 0) {
metrics[0].set('waiting for connections')
metrics[2].set(info.height)
metrics[3].set(info.globalHashRate)
metrics[4].set(info.difficulty)
}
})
daemon.on('desync', (daemon, network, deviance) => {
log(util.format('Plenteumd is currently off the blockchain by %s blocks. Network: %s Daemon: %s', deviance, network, daemon))
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set('desynchronized')
metrics[1].set(`${daemon}/${network}`)
}
})
daemon.on('down', () => {
log('Plenteumd is not responding... stopping process...')
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set('down')
}
daemon.stop()
})
daemon.on('stopped', (exitcode) => {
log(util.format('Plenteumd has closed (exitcode: %s)... restarting process...', exitcode))
if (metrics.length !== 0) {
resetMetrics(metrics)
metrics[0].set(`stopped (code: ${exitcode})`)
}
daemon.start()
})
daemon.on('info', (info) => {
log(info)
})
daemon.on('error', (err) => {
log(err)
})
daemon.start()