-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
100 lines (93 loc) · 3.49 KB
/
test.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
let fs = require('fs');
let path = require('path');
let video = require('./lib/engine.js');
let filePath = "/home/neel/WebstormProjects/test/testFiles/testVideo1.mp4";
let options = {
token: '123',
id: '321',
stream: filePath
};
// path for storing logs
let retryObj = {status: false, path: null, uniqId: null};
let uploadCacheFilePath = path.join(__dirname, 'cache'); // path for storing uploading data cache
fs.readdir(uploadCacheFilePath, function (err, files) { // reading dir to check any process already pending in cache
console.log("\n Pending cache list", files);
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
if (files && files.length) { // If any file exist we should retry
files.forEach(function (file) {
let logFilePath = path.join(uploadCacheFilePath,file);
console.log("\n\n ",logFilePath);
fs.readFile(logFilePath, (err, res) => {
try {
let result = JSON.parse(res.toString());
if (checkValidRetryData(result)) {
retryObj.status = true;
options = {
token: result.args.token,
id: result.args.id,
stream: result.args.stream
};
retryObj.ids = result.ids;
retryObj.start_offset = result.start_offset;
retryObj.end_offset = result.end_offset;
retryObj.path = logFilePath;
if (result.uniqId) {
retryObj.uniqId = result.uniqId
}
retry(options, retryObj);
} else {
console.log("Data missing", result);
}
} catch (err) {
console.log("\n Error : \n", err);
}
})
})
} else {
console.log("\n No pending files to upload. Uploading New Video...");
setTimeout(() => {
// stop process for testing retry
process.exit()
}, 40);
retryObj.status = false;
retryObj.path = `${uploadCacheFilePath}/${Date.now()}.txt`;
retryObj.uniqId = 7;
uploadWithRetry(options, retryObj) // uploading new file and will stop within 40 ms for testing...
}
});
function checkValidRetryData(data) {
return data && data.args && data.args.token && data.args.id && data.args.stream && data.ids && data.start_offset && data.end_offset;
}
function uploadWithRetry(options, retryObj) {
console.log("\n Uploading With log...");
video(options, retryObj) // retryObj status : false and path : '/log.txt' to store log
.then((res) => {
console.log(res);
})
.catch(err => {
console.log("Error :", err);
});
}
function retry(options, retryObj) {
console.log("\n Retrying With Cache data...");
video(options, retryObj) // retryObj status : true and path : '/log.txt' to store log and resume upload
.then((res) => {
console.log(res);
})
.catch(err => {
console.log("Error :", err);
});
}
function normalUpload() {
console.log("Uploading normal");
video(options) //
.then((res) => {
console.log(res);
})
.catch(err => {
console.log("Error :", err);
});
}