Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
179 lines (153 loc) · 4.75 KB
/
Copy pathserver.js
File metadata and controls
179 lines (153 loc) · 4.75 KB
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
/*********************/
/* Express Packages: */
/*********************/
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var aws = require('aws-sdk');
var datetime = require('node-datetime');
var request = require('request');
var app = express();
var serverPort = 8081;
//For extract the entire body portion of an incoming request
app.use(bodyParser.urlencoded({
extended: true
}));
//S3 connection conf
var s3 = new aws.S3({signatureVersion: 'v4'});
/******************/
/* Log Timestamp: */
/******************/
var dt, formatted;
var time = new Date().toISOString()
.replace(/T/, ' ') // replace T with a space
.replace(/\..+/, ''); // delete the dot and everything after
/*************************/
/* Environment Variables */
/*************************/
var S3_BUCKET_NAME = process.env.S3_BUCKET_NAME;
/**************/
/* Constants: */
/**************/
var BF_NAME = "/var/log/firehose/bigFileBuffer.txt";
var SF_NAME = "/var/log/firehose/smallFileBuffer.txt";
var BF_TO_DUMP = "/var/log/firehose/bigFileToDump.txt";
var SF_TO_DUMP = "/var/log/firehose/smallFileToDump.txt";
var FAILED_TO_POST = "/var/log/firehose/failedToPostBuffer.log";
var BIG_BUFFER_SIZE = 1073741824; //1GB
var SMALL_BUFFER_SIZE = 20971520; //20MB
//set the headers to the post request to django
var headers = { 'User-Agent' : 'FirehoseServer/0.0.1',
'Content-Type' : 'application/json'
};
/**************/
/* Functions: */
/**************
/**
* Dump fileToDump to the remoteFolder on the relevant bucket.
* @param {String} fileToDump
* @param {String} remoteFolder
*/
var dumpFileToS3 = function( fileToDump, remoteFolder){
var fileBuffer = fs.readFileSync(fileToDump);
var remoteFileName = remoteFolder + formatted + '.txt';
s3.putObject({
ACL : 'public-read',
Bucket : S3_BUCKET_NAME,
Key : remoteFileName,
Body : fileBuffer,
ContentType : "text/html"
}, function(error, res){
console.log("dumpFileToS3 FUNCTION: " + fileToDump);
if(error) {
console.log("Failed to upload big file to S3 ||| ERROR : " + error);
}
else{
fs.unlink(fileToDump, (err) => {
if (err) console.log('Failed to delete ' + fileToDump + ' ||| ERROR : ' + err);
});
}
});
}
/**
* Check the existance of file
* @param {String} file
* @param {Function} callback
*/
function checkIfFile(file, callback) {
fs.stat(file, function fsStat(err, stats) {
if (err) {
if (err.code === 'ENOENT') {
return callback(null, false);
} else {
return callback(err);
}
}
return callback(null, stats.isFile());
});
}
/**
* create watcher on fileToWatch and dump it to s3 when it exceed bufferSize.
* @param {String} fileToWatch
* @param {String} fileToDump
* @param {String} bufferSize
* @param {String} bucketPrefix
*/
var watchFile = function(fileToWatch, fileToDump, bufferSize, bucketPrefix ){
require('file-size-watcher').watch(fileToWatch).on('sizeChange', function callback(newSize, oldSize){
if(newSize > bufferSize){
checkIfFile(fileToDump, function(err, isFile) {
if (isFile) {
console.log("Concurrency ERROR - Failed to dump " + fileToDump + " to S3.");
dumpFileToS3(fileToDump, bucketPrefix);
}
else
fs.rename(fileToWatch, fileToDump, function(err) {
//callback - rename buffer file:
if ( err ) console.log('Failed to rename buffer file ||| ERROR: ' + err);
dumpFileToS3(fileToDump, bucketPrefix);
});
});
}
});
}
/*******************/
/* Files Watchers: */
/*******************/
//Raw-Data:
watchFile(SF_NAME, SF_TO_DUMP, SMALL_BUFFER_SIZE, "small-files/");
watchFile(BF_NAME, BF_TO_DUMP, BIG_BUFFER_SIZE, "big-files/");
/***********/
/* SERVER: */
/***********/
app.get("/", function (req, res) {
res.end("Firehose is up and running");
});
///sendjson url gets log as json, append it to small&big buffers and return response to the client:
app.post("/sendjson", function (req, res) {
dt = datetime.create();
formatted = dt.format('Y/m/d/H-M-S');
var body = '';
req.on('data', function(data) {
body += data;
});
//write logs to small and big buffers
req.on('end', function (){
fs.appendFile(BF_NAME, body + '\n', function(err){
if (err){
console.log("Failed to write log to big file buffer. ERR: " + err + " | " + time);
}
});
fs.appendFile(SF_NAME, body + '\n', function(err){
if (err){
console.log("Failed to write log to small file buffer. ERR: " + err + " | " + time);
}
});
res.end("The log inserted successfully");
//Response already get to the requested server.
//Continue with async tasks here.
});
});
app.listen(serverPort, function () {
console.log('Firehose is running on port ' + serverPort + ' from: ' + time);
});