-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContainerSync.js
181 lines (141 loc) · 5.12 KB
/
ContainerSync.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
var P = require('bluebird');
var _ = require('lodash');
var azure = require('azure-storage');
var fs = P.promisifyAll(require("fs"));
var path = require('path');
var mkdirp = require('mkdirp');
function ContainerSync(container, destinationPath, options) {
this.container = container;
this.destinationPath = destinationPath;
var defaults = {
// refresh interval in ms.
interval: 5 * 60 * 1000,
// remove local files that no longer exist in container?
deleteOrphans: false,
// switches for logging
log: {
start: true,
download: true,
skip: false,
delete: true
},
autoStart: false,
};
this.options = _.extend(defaults, options);
mkdirp.sync(this.destinationPath);
this.lastRunTime = 0;
this.started = false;
this.timeoutId = null;
if (this.options.autoStart) {
this.start();
}
}
ContainerSync.prototype.start = function() {
if (!this.started) {
this.started = true;
this.run();
}
};
ContainerSync.prototype.stop = function() {
if (this.started) {
clearTimeout(this.timeoutId);
this.started = false;
}
};
var syncEntry = function(sync, entry) {
var blobName = entry.name;
var azureLastModified = new Date(entry.lastModified);
var destinationFilename = path.join(sync.destinationPath, blobName);
return fs.statAsync(destinationFilename).then(function(stats) {
// see if the last modified time stamps match
var localLastModified = new Date(stats.mtime);
if (localLastModified.getTime() === azureLastModified.getTime()) {
if (sync.options.log.skip) {
console.log('skipping download of ' + blobName + ' with same last modified');
}
return false;
}
return true;
})
.catch(function(err) {
// error occurs if file doesn't exist, so we need to download
return true;
})
.then(function(shouldDownload) {
if (!shouldDownload) {
return destinationFilename;
}
// ensure the target directory exists
var lastSlashIndex = destinationFilename.lastIndexOf('/');
var destinationDirectory = destinationFilename.substring(0, lastSlashIndex + 1);
mkdirp.sync(destinationDirectory);
if (sync.options.log.download) {
console.log('start download of ' + blobName);
}
return sync.container.getBlobToFile(blobName, destinationFilename)
.then(function(blob) {
if (sync.options.log.download) {
console.log('completed download of ' + blobName);
}
// we need to set last modified time to match azure
fs.utimesSync(destinationFilename, new Date(), azureLastModified);
return destinationFilename;
});
});
};
var removeOrphans = function(path, keepFiles, logDelete) {
return fs.statAsync(path)
.then(function(stats) {
if (stats.isDirectory()) {
return;
}
// TODO: implement delete
});
};
ContainerSync.prototype.ensureHasRun = function() {
if (this.lastRunTime > 0) {
return P.resolve();
}
return this.run();
};
ContainerSync.prototype.run = function() {
// ensure we only have one sync running at a time
if (!this.currentRun) {
var sync = this;
this.currentRun = this.container.listAllBlobs()
.then(function(blobEntries) {
if (sync.options.log.start) {
console.log('Sync ' + blobEntries.length + ' blobs from ' +
sync.container.name + ' to ' + sync.destinationPath);
}
// download each of the blobs
var promises = _.map(blobEntries, function(entry) { return syncEntry(sync, entry); });
return P.all(promises);
})
.then(function(destinationFilenames) {
// delete orphaned files
if (sync.options.deleteOrphans) {
return removeOrphans(
sync.destinationPath,
destinationFilenames,
sync.options.log.delete);
}
//console.log(destinationFilenames);
})
.then(function() {
this.lastRunTime = Date.now();
this.currentRun = null;
// schedule next run
if (sync.started) {
sync.timeoutId = setTimeout(function() {
sync.run()
.catch(function(ex) {
console.log('Exception on background container sync: ' + ex);
});
}, sync.options.interval);
}
});
}
return this.currentRun;
};
module.exports = ContainerSync;