-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3upload.js
203 lines (175 loc) · 4.83 KB
/
s3upload.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var express = require('express'),
extend = require('extend'),
fs = require('fs'),
winston = require('winston'),
config = require('./config'),
model = require('./model');
var app = module.exports = express();
// Ripple client
var ripple = require('ripple-lib');
var utils = require('ripple-lib').utils;
var remote = ripple.Remote.from_config(config.remote);
// MySQL
var mysql = require('mysql');
var db = mysql.createConnection({
host: config.db.mysql_ip,
user: config.db.mysql_user,
password: config.db.mysql_pass,
database: config.db.mysql_db,
multipleStatements: true
});
//Amazon S3
var knox = require('knox');
var client = knox.createClient({
key: config.s3.key,
secret: config.s3.secret,
bucket: config.s3.bucket
});
// Configuration
var http_config = {};
app.configure(function(){
extend(http_config, {
ssl: false,
port: 3000
});
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
extend(http_config, {
ssl: {
key: fs.readFileSync('./ssl/server.key'),
cert: fs.readFileSync('./ssl/server.crt')
},
port: 443
});
app.use(express.errorHandler());
});
var server = http_config.ssl ?
require('https').createServer(http_config.ssl, app) :
require('http').createServer(app);
remote.on('error', function (err) {
winston.error(err);
});
remote.on('connected', function(connection) {
winston.info('WebSocket client connected');
model.apply({
status_connected: true
});
getLedgerIndex();
});
remote.on('disconnected', function(connection) {
model.apply({
status_connected: false
});
});
startupHttp();
function startupHttp()
{
server.listen(http_config.port, function(){
winston.info("Express server listening on port %d in %s mode",
this.address().port, app.settings.env);
startupMysql();
});
}
function startupMysql()
{
db.connect(function (err) {
if (err) {
winston.error(err);
}
winston.info("Connected to MySQL server");
remote.connect();
});
}
function getLedgerIndex() {
db.query('SELECT value FROM config WHERE `key` = ?', ['ledger_processed'],
function (err, rows)
{
if (err) {
winston.error(err);
return;
}
var latest = Math.max(+(rows[0] && rows[0].value) || 0, config.net.genesis_ledger);
checkS3Upload(latest+1);
});
}
function checkS3Upload (ledger_index) {
var s3_filename = '/ledger/'+ledger_index+'.json';
winston.info(s3_filename);
client.get(s3_filename).on('response', function(res){
if(res.statusCode != 200)
requestLedger(ledger_index);
else
checkS3Upload(ledger_index+1);
}).end();
}
function requestLedger(ledger_index) {
try {
var replied = false;
remote.request_ledger(undefined, { transactions: true, expand: true })
.ledger_index(ledger_index)
.on('error', function (err) {
if (replied) return;
console.log("REQUEST LEDGER ERROR");
})
.on('success', function (m) {
if (replied) return;
replied = true;
uploadToS3(m, ledger_index);
})
.request()
;
// As of writing this, ripple-lib does not handle the server connection
// being lost while waiting for a ledger request response.
//
// This can get the whole processing pipeline stuck, so we add a timeout
// so we can recover from this condition.
setTimeout(function () {
if (replied) return;
console.log("REQUEST LEDGER TIMEOUT");
replied = true;
}, 10000);
} catch(e) { callback(e); }
}
//Upload each ledger data to the S3.
function uploadToS3 (e, ledger_index) {
var ledger = e;
var s3_filename = '/ledger/'+ledger_index+'.json';
var ledger_string = JSON.stringify(ledger);
var req = client.put(s3_filename, {
'Content-Length': ledger_string.length,
'Content-Type': 'application/json'
});
req.on('response', function(res){
if (200 == res.statusCode) {
console.log('saved to %s', req.url);
uploadLastLedger(ledger_index);
} else {
checkS3Upload(ledger_index);
}
});
req.end(ledger_string);
}
//Upload the latest ledger index to the S3.
function uploadLastLedger (ledger_index) {
var s3_filename = '/meta/ledger-manifest.json';
var last_ledger = {latest: ledger_index};
var str_last_ledger = JSON.stringify(last_ledger);
var req_last_ledger = client.put(s3_filename, {
'Content-Length': str_last_ledger.length,
'Content-Type': 'application/json'
});
req_last_ledger.on('response', function(res){
if (200 == res.statusCode) {
console.log('saved to %s', req_last_ledger.url);
}
checkS3Upload(ledger_index+1);
});
req_last_ledger.end(str_last_ledger);
}