-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (176 loc) · 5.09 KB
/
Copy pathindex.js
File metadata and controls
196 lines (176 loc) · 5.09 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const debugFactory = require('debug');
const httpClient = require('./lib/http-client');
const util = require('util');
const _ = require('lodash');
const CronJob = require('cron').CronJob;
const storage = require('./lib/storage');
const EventEmitter = require('events').EventEmitter;
const assert = require('assert');
const Index = require('./lib/indexes');
const cronJobs = [];
const os = require('os');
var instance;
module.exports = function factory (config) {
var self;
function buildConfig (config) {
const debug = debugFactory('node-http-cache:buildConfig');
debug('config >> %j', config);
const _config = {
location: config.location || os.tmpdir()
};
_config.services=[];
_.forEach(config.services,function (service, key) {
assert(service.name, util.format('services[%s].name must be defined', key));
assert(service.cronExpression, util.format('service[%s].cronExpression must be defined', key));
assert(service.httpOptions, util.format('service[%s].httpOptions must be defined', key));
_config.services.push({
name: service.name,
cronExpression: service.cronExpression,
itemsPath: service.itemsPath,
indexes: service.indexes,
httpOptions: service.httpOptions
});
});
return _config;
}
function updateService (service) {
return function (){
const debug = debugFactory('node-http-cache:updateService:' + service.name);
debug('Updating service...');
return httpClient.downloadData(service.httpOptions)
.then(
function indexes (response) {
const body = service.itemsPath ? response.body[service.itemsPath] : response.body;
if(service.indexes){
return Index.build({
name: service.name,
data: body,
indexes: service.indexes
}).
then(function (indexes) {
return {
config: service,
body: body,
headers: response.headers,
indexes: indexes
};
});
}
return {
config: service,
body: body,
headers: response.headers
};
}
).then(
function saveToStorage (object){
debug('Saving to DB "%s"',object.config.name);
return storage.put(object.config.name,object)
.then(
function emitEvent(savedObject) {
self.emit('updateData',{name:savedObject.config.name,data:savedObject.body});
}
);
}
).fail(function (err) {
debug('error >> %s >> %s',service.name, err.stack);
var error = new Error(util.format('%s >> %s',service.name,err.stack));
self.emit('updateError',error);
throw error;
});
};
}
function serviceUpdated (service) {
var debug = debugFactory('node-http-cache:serviceUpdated:' + service.name);
return function () {
debug('Service updated.');
};
}
function exists (key) {
return storage.getKeys()
.then(
function (keys) {
var foundKey = _.find(keys,function (storageKey) {
return storageKey === key;
});
return typeof foundKey !== 'undefined';
}
);
}
function init (config) {
var debug = debugFactory('node-http-cache:init');
debug('config >> %j',config);
storage.init(config);
_.forEach(config.services,function (service) {
debug('Scheduling service "%s" with expression "%s"', service.name, service.cronExpression);
debug('Checking if snapshot for "%s" already exists', service.name);
exists(service.name)
.then(
function callback(exists) {
debug('exists >> %j',exists);
return !exists;
},
function error(){
return true;
}
).then(
function startCron (runOnInit) {
debug('runOnInit >> %s', runOnInit);
cronJobs.push(new CronJob({
cronTime: service.cronExpression,
onTick: updateService(service),
onComplete: serviceUpdated(service),
start: true,
timeZone: service.timezone || config.timezone || 'GMT-0',
runOnInit: runOnInit
}));
}
);
});
return function () {
EventEmitter.call(this);
self = this;
};
}
function stop(){
_.forEach(cronJobs, function (cron) {
cron.stop();
});
storage.close();
}
const debug = debugFactory('node-http-cache:factory');
const _config = buildConfig(config);
debug('_config >> %j', _config);
const NodeHttpCache = init(_config);
util.inherits(NodeHttpCache,EventEmitter);
NodeHttpCache.prototype.get = function (config) {
const serviceName = config.name;
const indexKey = config.indexKey;
const debug = debugFactory('node-http-cache:get:' + serviceName);
const indexValue = config.indexValue;
debug('indexKey >> %s', indexKey);
return storage.get(serviceName)
.then(
function debugLog(loadedData){
var data = (indexKey && indexValue) ? loadedData.indexes[indexKey][indexValue] : loadedData;
self.emit('getData',{
name: serviceName,
data: data
});
return data;
}
).fail(function (err) {
debug('error >> %s >> %s', err.message, err.stack);
const error = new Error(util.format('%s >> %s',serviceName,err.stack));
self.emit('getError', error);
throw error;
});
};
NodeHttpCache.prototype.exists = exists;
NodeHttpCache.prototype.stop = function() {
stop();
};
instance = new NodeHttpCache();
return instance;
};