/ tracker / index.js
index.js
 1  /*!
 2   * tracker/index.js
 3   * Copyright © 2019 – Katana Cryptographic Ltd. All Rights Reserved.
 4   */
 5  
 6  
 7  import { waitForBitcoindRpcApi } from '../lib/bitcoind-rpc/rpc-client.js'
 8  import network from '../lib/bitcoin/network.js'
 9  import keysFile from '../keys/index.js'
10  import db from '../lib/db/mysql-db-wrapper.js'
11  import Logger from '../lib/logger.js'
12  import HttpServer from '../lib/http-server/http-server.js'
13  import Tracker from './tracker.js'
14  import TrackerRestApi from './tracker-rest-api.js'
15  
16  const keys = keysFile[network.key]
17  
18  try {
19      Logger.info(`Tracker : Process ID: ${process.pid}`)
20      Logger.info('Tracker : Preparing the tracker')
21  
22      // Wait for Bitcoind RPC API
23      // being ready to process requests
24      await waitForBitcoindRpcApi()
25  
26      // Initialize the db wrapper
27      const dbConfig = {
28          connectionLimit: keys.db.connectionLimitTracker,
29          acquireTimeout: keys.db.acquireTimeout,
30          host: keys.db.host,
31          user: keys.db.user,
32          password: keys.db.pass,
33          database: keys.db.database
34      }
35  
36      db.connect(dbConfig)
37  
38      // Initialize the tracker
39      const tracker = new Tracker()
40  
41      // Initialize the http server
42      const host = keys.apiBind
43      const port = keys.ports.trackerApi
44      const httpServer = new HttpServer(port, host)
45  
46      // Initialize the rest api endpoints
47      new TrackerRestApi(httpServer, tracker)
48  
49      // Start the http server
50      httpServer.start()
51  
52      // Start the tracker
53      tracker.start()
54  
55      // Signal that the process is ready
56      process.send('ready')
57  
58      const exit = async () => {
59          httpServer.stop()
60          await tracker.stop()
61          await db.disconnect()
62          process.exit(0)
63      }
64  
65      process.on('SIGTERM', async () => {
66          await exit()
67      })
68  
69      process.on('SIGINT', async () => {
70          await exit()
71      })
72  
73  } catch (error) {
74      Logger.error(error, 'Tracker : Unhandled error, exiting...')
75      process.exit(1)
76  }