/
error_handling.js
234 lines (227 loc) · 8.3 KB
/
error_handling.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var alerter = require("./Alert.js");
var fs = require('fs');
var path = require("path");
var readBatFile = require("./readBatchbatfile.js");
var request = require('request');
var batc_bat_name = 'D:/Customer_Blast_to_Asc/Blast Batch 200 YmdHms.bat';
///module variables and constants///////////////////////////////////////////////////////////////////////////////////////
var rootDir;
var konyvtar;
var lastkonyvtar;
var timeSeconds;
var emails;
var kuszob;
var currentFilePath;
var lastModifiedFileDateTime;
var lastModifiedFileSize;
var previousLastModifiedDateTime;
var previousLastModifiedFileSize;
var writeFirst = false;
var restartSuccess = true;
console.log(restartSuccess);
checkDataAcquisition();
///data sender wrapper function/////////////////////////////////////////////////////////////////////////////////////////
function checkDataAcquisition() {
var timer = fs.readFileSync('config.txt', "utf-8").toString().split('\n');
timeSeconds = timer[1].slice(8, timer[1].length);
rootDir = timer[2].slice(12, timer[2].length);
kuszob = parseInt(timer[3].slice(15, timer[3].length));
emails = timer[4].slice(13, timer[4].length);
konyvtar = readBatFile.readbatfile(batc_bat_name);
if (writeFirst == true) {
restartWasSuccesfull();
if(restartSuccess == false){
restartAcquisition();
}
console.log(restartSuccess);
}
else if (restartSuccess == true){
currentFilePath = rootDir.trim() + '/' + konyvtar;
console.log(currentFilePath);
lastkonyvtar = konyvtar;
if (getLastCreatedDataFileName() == null) {
//console.log(currentFilePath+" ?könyvtár nem létezik");
restartAcquisition();
//checkDataAcquisition();
}
else {
console.log(currentFilePath + "/" + getLastCreatedDataFileName());
// process.exit();
var stat = fs.statSync(currentFilePath + "/" + getLastCreatedDataFileName());
previousLastModifiedDateTime = lastModifiedFileDateTime;
previousLastModifiedFileSize = lastModifiedFileSize;
lastModifiedFileSize = stat.size;
lastModifiedFileDateTime = stat.mtime.toISOString().replace(/T/, ' ').// replace T with a space
replace(/\..+/, '');
console.log("a fájl mérete: " + lastModifiedFileSize + " byte" + "\nlegutoljára módosítás dátuma : " + lastModifiedFileDateTime);
if (writeFirst == true) {
fs.appendFile('error_log.txt', 'Adatgyűjtés újraindítása után első futási eredmények: ' + currentFilePath + "/" + getLastCreatedDataFileName() + '\n', function (err, data) {
if (err) {
return console.log(err);
}
});
writeFirst = false;
}
/* request({
uri: "http://nyme.ggki.hu/dataSavers/saveIntoDatabase.php",
method: "POST",
form: {
konyvtar: konyvtar,
currentFilePath: konyvtar+'/'+getLastCreatedDataFileName(),
lastModifiedFileSize:lastModifiedFileSize,
lastModifiedFileDateTime: lastModifiedFileDateTime
}
}, function (error, response, body) {
//console.log(body);
});*/
compareFileParameters();
}
//
}
setTimeout(function () {
checkDataAcquisition();
}, timeSeconds);
}
function compareFileParameters() {
var date = new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '');
if(previousLastModifiedFileSize == lastModifiedFileSize){
fs.appendFile('error_log.txt',date+': Adatgyűjtés leállt, újraindítási kísérlet. Utolsó adatfájl : '+ currentFilePath+"/"+getLastCreatedDataFileName()+'\n', function (err,data) {
if (err) {
return console.log(err);
}
});
//console.log("9+9+");
restartAcquisition();
}
}
function restartWasSuccesfull() {
var fileType=".fw4";
var temp = [];
if (konyvtar == null || lastkonyvtar == konyvtar) {
console.log("nem sikerült az újraindítás");
fs.appendFileSync('error_log.txt', 'Sikertelen újraindítás: ' + '\n', function (err, data) {
if (err) {
return console.log(err);
}
});
//console.log("lastkonyvtar: "+lastkonyvtar +"\n"+"aktkonyvtar: "+konyvtar);
restartSuccess=false;
return restartSuccess;
} else {
konyvtar= readBatFile.readbatfile(batc_bat_name);
currentFilePath = rootDir.trim() + '/' + konyvtar;
var list = fs.readdirSync(currentFilePath);
// létezik-e fájl a könyvtárban
//console.log(list);
if (list.length != 0) {
for (var i = 0; i < list.length; i++) {
if (path.extname(list[i]) === fileType) {
temp.push(list[i]);
}
}
var last_element = temp[temp.length - 1];
var stat = fs.statSync(currentFilePath + "/" + last_element);
console.log(stat.size);
if(stat.size > 1000){
console.log("sikeres restart");
restartSuccess=true;
return restartSuccess;}
else{
console.log("sikertelen restart");
restartSuccess=false;
return restartSuccess;
}
} else if(list.length == 0) {
console.log("Sikertelen újraindítás");
fs.appendFileSync('error_log.txt', ' Sikertelen újraindítás Nincs fájl a könyvtárban: ' + currentFilePath + '\n', function (err, data) {
if (err) {
return console.log(err);
}
});
console.log("++");
//restartAcquisition();
restartSuccess=false;
return restartSuccess;
}
// end for loop
return (temp[temp.length - 1]);
}
}
//getLastModifiendFileName
function getLastCreatedDataFileName() {
var fileType = '.fw4'; //file extension
var temp = [];
if (fs.existsSync(currentFilePath)) {
var list = fs.readdirSync(currentFilePath);
if (list.length != 0) {
for (var i = 0; i < list.length; i++) {
if (path.extname(list[i]) === fileType) {
temp.push(list[i]);
}
}
} else if(list.length ==0) {
console.log("Nincsen .fw4 fájl a könyvtárban");
fs.appendFileSync('error_log.txt', 'Nincs fájl a könyvtárban: ' + currentFilePath + '\n', function (err, data) {
if (err) {
return console.log(err);
}
});
return null;
}
// end for loop
return (temp[temp.length - 1]);
} else {
console.log("Nem létezik ilyen könyvtár a fájlok keresésére.");
fs.appendFileSync('error_log.txt', 'Nem létezeik ilyen könyvtár a fájlok keresésére: ' + currentFilePath+ '\n', function (err, data) {
if (err) {
return console.log(err);
}
});
// console.log("nincs ilyen directory");
return null;
}
}
function restartAcquisition(callback) {
//console.log("restart call");
require('child_process').exec("restart2.bat", function (err, stdout, stderr) {
if (err) {
// Ooops.
console.log(stderr);
return console.log(err);
}
// Done.
console.log(stdout);
});
writeFirst = true;
}
/* var datum = new Date().toISOString().replace(/T/, ' '). // replace T with a space
replace(/\..+/, '');
var lastfile = getLastCreatedDataFileName();
lastfile = parseInt(lastfile.substr(0,lastfile.indexOf(".")));
if(lastfile>kuszob){
request({
uri: "http://nyme.ggki.hu/dataSavers/saveIntoDatabaseSuccessRestart.php",
method: "POST",
form: {
lastModifiedFile: currentFilePath+"/"+getLastCreatedDataFileName(),
date: datum
}
}, function (error, response, body) {
// console.log(body);
});
}else{
request({
uri: "http://nyme.ggki.hu/dataSavers/saveIntoDatabaseRestart.php",
method: "POST",
form: {
lastModifiedFile: currentFilePath+"/"+getLastCreatedDataFileName(),
date: datum
}
}, function (error, response, body) {
// console.log(body);
});
alerter.sendEmail();
}
} */