-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
98 lines (83 loc) · 3.22 KB
/
server.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
'use strict';
/**
* Module dependencies.
*/
var app = require('./config/lib/app');
var path = require('path');
var config = require(path.resolve('./config/config'));
var debug = require('debug')('vault');
var vault = require(path.resolve('./config/lib/vault'));
var request = require('request');
var _ = require('lodash');
/**
* Model initialization
*/
var mongoose = require('mongoose'),
Vault = mongoose.model('Vault');
/**
* vault scan interval, will be scanning for a file every interval
* @type {Number}
*/
var scanInterval = config.vault.scanInterval;
var server = app.start(function(app, db, config) {
function maintainVault() {
setTimeout(function() {
Vault.findOneAndUpdate({ scanStatus: false }, { scanStatus: true }, function (err, document) {
if (!document) {
debug('vault: did not find any vault data document to process');
return maintainVault();
}
if (err) {
// on error, we log it and get back to the wait on fetching another item from the queue
debug('vault: error fetching and updating document from vaults');
return maintainVault();
}
debug('vault: %s - processing document: %s', new Date().toJSON(), document._id);
var vaultDocument = {
url: document.url,
api: document.api,
id: document.id,
status: document.status,
scanStatus: document.scanStatus
};
vault(vaultDocument, function(err, vaultData) {
// on error we want to return the file we took for scanning to
// the files-to-scan queue so we can take it again later
if (err) {
debug('vault: error processing document: %s with error %s: ', document._id, err);
Vault.findOneAndUpdate({ _id: document._id }, { scanStatus: false });
return maintainVault();
} else {
// if the scanning completed, we will update the files queue with These
// details and ping back the API with the result
Vault.findOneAndUpdate({ _id: document._id }, { $set: { status: vaultData.local.status, msg: vaultData.local.msg } }, null, function(err, doc) {
request({
method: 'POST',
uri: document.api,
json: {
url: document.url,
api: document.api,
status: vaultData.local.status,
msg: vaultData.local.msg
}
},
function(err, httpIncoming, response) {
if (err) {
// on error, we log it and get back to the wait on fetching another item from the queue
debug('vault: unable to ping back server api for %s: %s', document._id, err.message);
Vault.findOneAndUpdate({ _id: document._id }, { scanStatus: false });
return maintainVault();
} else {
debug('vault: successfully pinged api server %s and processed %s', document.api, document._id);
return maintainVault();
}
});
});
}
});
});
}, scanInterval);
}
// initialize the forever-loop
maintainVault();
});