forked from rohenaz/bmap-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
133 lines (116 loc) · 3.56 KB
/
index.ts
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
import { JungleBusClient } from '@gorillapool/js-junglebus'
import chalk from 'chalk'
import { fork } from 'child_process'
import net from 'net'
import redline from 'readline'
import { crawler, setCurrentBlock } from './crawler.js'
import { closeDb, getDbo } from './db.js'
import { ensureEnvVars } from './env.js'
import { getCurrentBlock } from './state.js'
/* Bitsocket runs in a child process */
// const bitsocket = fork('./build/bitsocket')
/* Planarium (API Server Process) */
const api = fork('./build/api')
export const enum ConnectionStatus {
Connecting = 0,
Connected,
Disconnected,
Error,
}
export var socket: net.Socket
let connectionStatus = ConnectionStatus.Disconnected
// Open up the server and send RPC socket to child. Use pauseOnConnect to prevent
// the sockets from being read before they are sent to the child process.
// const server = net.createServer({ pauseOnConnect: true })
// server.on('connection', (s) => {
// api.send({ type: 'socket', socket: s })
// socket = s
// process.on('message', (data: any) => {
// console.log('message received by parent!', data)
// switch (data.type) {
// case '':
// case 'tx':
// try {
// processTransaction(data.rawTx)
// } catch (e) {
// console.error('Failed to ingest tx', e)
// }
// break
// }
// })
// })
// server.listen(1336)
const start = async () => {
await ensureEnvVars()
await getDbo() // warm up db connection
try {
// Should really start with latest blk from ANY collection, not only video like this
let currentBlock = await getCurrentBlock()
setCurrentBlock(currentBlock)
console.log(chalk.cyan('crawling from', currentBlock))
const s = 'junglebus.gorillapool.io'
console.log('CRAWLING', s)
const jungleBusClient = new JungleBusClient(s, {
debug: true,
protocol: 'protobuf',
onConnected(ctx) {
// add your own code here
connectionStatus = ConnectionStatus.Connected
api.send({ status: connectionStatus, type: 'status' })
console.log(ctx)
},
onConnecting(ctx) {
// add your own code here
connectionStatus = ConnectionStatus.Connecting
api.send({ status: connectionStatus, type: 'status' })
console.log(ctx)
},
onDisconnected(ctx) {
// add your own code here
connectionStatus = ConnectionStatus.Disconnected
api.send({ status: connectionStatus, type: 'status' })
console.log(ctx)
},
onError(ctx) {
// add your own code here
console.error(ctx)
connectionStatus = ConnectionStatus.Error
api.send({ status: connectionStatus, type: 'status' })
// reject(ctx)
},
})
await crawler(jungleBusClient)
} catch (e) {
console.error(e)
}
}
// Handle interrupt
if (process.platform === 'win32') {
let rl = redline.createInterface({
input: process.stdin,
output: process.stdout,
})
rl.on('SIGINT', function () {
// ToDo - TS hates this
// process.emit('SIGINT')
})
}
process.on('SIGINT', async function () {
// graceful shutdown
console.log('close from shutdown')
await closeDb()
// server.close()
process.exit()
})
console.log(
chalk.yellow(`
::::::::: :::: :::: ::: :::::::::
:+: :+: +:+:+: :+:+:+ :+: :+: :+: :+:
+:+ +:+ +:+ +:+:+ +:+ +:+ +:+ +:+ +:+
+#++:++#+ +#+ +:+ +#+ +#++:++#++: +#++:++#+
+#+ +#+ +#+ +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+# #+# #+#
######### ### ### ### ### ###
`)
)
setTimeout(start, 1000)