This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.js
175 lines (160 loc) · 4.55 KB
/
export.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
var childProcess = require("child_process");
var async = require("async");
var chain = require("slide").chain;
var fs = require("fs");
var Readable = require("stream").Readable;
var uuid = require("node-uuid");
var exportsDebug = require("debug")("exports");
var exportDebug = require("debug")("export");
var inherits = require("util").inherits;
var EventEmitter = require("events").EventEmitter;
var Transform = require("stream").Transform;
var Tail = require("./tail");
var _ = require("lodash");
var __ = require("highland");
var JSONStream = require("JSONStream");
var JSONParse = require("./jsonparser");
/* Simple collection operations on MongoExport*/
inherits(MongoExports, Readable);
function MongoExports(mongoExports) {
Readable.call(this);
this.exports = mongoExports;
for(var i in this.exports) {
this.exports[i].on("close", this._closeListener.bind(this));
}
this.streams = __(this.exports.map(function(m) {
return m.stream;
})).merge();
}
MongoExports.prototype._closeListener = function(mongoExport) {
exportsDebug("MongoExport finished with code " + mongoExport.exitCode);
var mongoexport;
for(var i in this.exports) {
mongoexport = this.exports[i];
if(mongoexport.status !== "closed") {
return;
}
}
this.streams.end();
};
MongoExports.prototype.resume = function() {
//resume each export and merge
var exportStreams, combinedStreams;
exportStreams = this.exports.map(function(exports) {
return exports.resume();
});
return this;
};
MongoExports.prototype.pause = function() {
this.exports.forEach(function(mongoExport) {
mongoExport.pause();
});
};
MongoExports.create = function(configs, cb) {
async.map(configs, MongoExport.create.bind(MongoExport), function(err, mongoExportArr) {
if(err) {
return cb(err);
}
cb(null, new MongoExports(mongoExportArr));
});
};
inherits(MongoExport, EventEmitter);
function MongoExport(config) {
this.__config = config;
this.__id = uuid.v1();
this.status = "uninitialized";
}
/*
* takes {exportOptions: "", workingDirectory: ""}
*/
MongoExport.create = function(config, cb) {
var exportJob = new this(config);
exportJob.init(function(err) {
if(err) {
return cb(err);
}
cb(null, exportJob);
});
};
MongoExport.prototype.init = function(cb) {
var me = this;
this._createWorkingFile(function(err, workingFilePath) {
if(err) {
return cb(err);
}
me.__tail = Tail(workingFilePath);
// TODO: Allow client to define parser
me.__parser = JSONParse();//JSONStream.parse();
me.stream = me.__tail.stream.pipe(me.__parser);
me._spawnMongoExport(cb);
});
};
MongoExport.prototype._createWorkingFile = function(cb) {
var me = this;
this.workingFile = this.__config.workingDirectory + "/mongoexport-" + this.__id;
fs.open(this.workingFile, "w+", function(err, fd) {
if(err) {
return cb(err);
}
me.__fd = fd;
cb(null, me.workingFile);
});
};
/* Completes once the export has created the output file. The
process is then paused.
*/
MongoExport.prototype._spawnMongoExport = function(cb) {
var me = this;
var options = this.__config.exportOptions + " -o " + this.workingFile;
this.__spawn = childProcess.spawn("mongoexport", options.split(" "));
this.__spawn.on("close", function(exitCode) {
me.exitCode = exitCode;
me.__tail.waitForEof(function(err) {
if(err) {
me.emit("error", err);
}
me.status = "closed";
me.emit("close", me);
});
exportDebug("Job " + me.__id + " finished");
});
this._waitForOutputFileToExist(function(err) {
if(err) {
return cb(err);
}
me.pause();
cb();
});
};
MongoExport.prototype._waitForOutputFileToExist = function(maxTries, cb) {
exportDebug("Waiting for output file");
if(!cb) {
cb = maxTries;
maxTries = 5;
}
var fileDoesNotExist = new Error("Output file may not exist");
var me = this;
var tries = 0;
async.until(function() {
return !fileDoesNotExist;
}, function(cb) {
fs.stat(me.workingFile, function(err) {
if((err && !_.contains(err.message, "ENOENT")) || tries > maxTries) {
return cb(err);
}
fileDoesNotExist = err;
tries++;
cb();
});
}, cb);
};
MongoExport.prototype.pause = function() {
this.__spawn.kill("SIGSTOP");
this.status = "paused";
};
MongoExport.prototype.resume = function() {
this.__spawn.kill("SIGCONT");
this.status = "running";
};
exports.MongoExports = MongoExports;
exports.MongoExport = MongoExport;