forked from bitcoinz-sites/BTCz-Pay
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker3.js
86 lines (73 loc) · 3.04 KB
/
worker3.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
/**
* ==============================================================================
* BTCz-Pay
* ==============================================================================
*
* Version 0.2.1 (production v1.0)
*
* Self-hosted bitcoinZ payment gateway
* https://github.com/MarcelusCH/BTCz-Pay
*
* ------------------------------------------------------------------------------
* worker3.js Independent nodejs worker
* ------------------------------------------------------------------------------
*
* worker iterates through all addresses,
* marks expired after 10 min. more than expired
* in case of client is away
*
* ==============================================================================
*/
let rp = require('request-promise')
let storage = require('./models/storage') // Load db call functions
let blockchain = require('./models/blockchain') // Load blockchain functions
let config = require('./config') // Load configuration file
let logger = require('./utils/logger') // Load the logger module
require('./smoke-test') // Checking DB & BtcZ node RPC
;(async () => {
while (1) {
let wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let job = await storage.getUnprocessedAdressesNewerThanPromise(Date.now() - (config.process_unpaid_for_period*100000))
await processJob(job)
await wait(60100)
}
})()
async function processJob (rows) {
try {
console.log('WORKER 3', ['Check for expired gateway...'])
rows = rows || {}
rows.rows = rows.rows || []
for (const row of rows.rows) {
let json = row.doc
// If not expired and time 10.min. more than expired
if ((json.state==0 || json.state==1) && (json.timestamp+(config.max_payment_valid*60000)+(10*60000))<=Date.now() ){
let received = await blockchain.getReceivedByAddress(json.address)
// Log if expired
logger.log('WORKER 3', [json._id, 'address: '+json.address, ''
+'expect: '+json.btc_to_ask, ''
+'confirmed: '+received[config.confirmation_before_forward].result, ''
+'unconfirmed: '+received[0].result, ''
+'speed_sweep: '+json.speed_sweep])
json.state=2
json.processed = 'expired'
logger.log('WORKER 3', [json._id, 'Mark expired ! '])
await storage.saveJobResultsPromise(json)
// Set URL parameter
let URLset = json.callback_url
if (URLset.indexOf('?') !== -1) {
URLset = URLset +'&secret='+json.secret+'&state=2'
} else {
URLset = URLset +'?secret='+json.secret+'&state=2'
}
// Fire server side pingback
rp({uri: URLset}).then((result) => {
logger.log('WORKER 3', [json._id, 'Pingback expired done: ' , URLset])
}).catch((error) => {
logger.error('WORKER 3', [json._id, 'Pingback expired fail: ' , URLset, error.message, error.stack])
})
} // end if
} // end for
} catch (error) {
logger.error('WORKER 3', [ error.message, error.stack ])
}
}