Permalink
Browse files

[fix] Move sync files to post-startup, keep only db on pre

  • Loading branch information...
1 parent 582b064 commit 1d5d6fc795b4cb23db117a5c008b522bf8f8e82a @julianduque julianduque committed Mar 4, 2014
Showing with 110 additions and 10 deletions.
  1. +15 −9 index.js
  2. +94 −0 lib/post-startup.js
  3. +1 −1 lib/pre-startup.js
View
@@ -27,17 +27,23 @@ function Ghost() {
//
ghost.blog = require('ghost')();
+
//
- // Wait two second, to prevent premature triggering, before listening
- // for file changes in eiter sqlite or content, ignore journaling.
+ // Post Startup
//
- setTimeout(function defer() {
- ghost.watcher = notify.watch(content, { ignored: /README\.md|\/themes\/|\.git|\.db-journal$/ })
- .on('change', ghost.change)
- .on('add', ghost.change)
- .on('unlink', ghost.unlink)
- .on('err', console.error);
- }, 2000);
+ require('./lib/post-startup').setup(function done(err) {
+ //
+ // Wait two second, to prevent premature triggering, before listening
+ // for file changes in eiter sqlite or content, ignore journaling.
+ //
+ setTimeout(function defer() {
+ ghost.watcher = notify.watch(content, { ignored: /README\.md|\/themes\/|\.git|\.db-journal$/ })
+ .on('change', ghost.change)
+ .on('add', ghost.change)
+ .on('unlink', ghost.unlink)
+ .on('err', console.error);
+ }, 2000);
+ });
});
}
View
@@ -0,0 +1,94 @@
+'use strict';
+
+//
+// Required modules.
+//
+var fs = require('fs')
+ , spawn = require('child_process').spawn
+ , mkdirp = require('mkdirp')
+ , async = require('async')
+ , path = require('path')
+ , colors = require('colors')
+ , local = require('../config')
+ , pkg = require('../package')
+ , Mongo = require('./mongo')
+ , mongo = new Mongo;
+
+//
+// Path to config, get content and production config.
+//
+var part = path.join(__dirname, '..', 'node_modules', 'ghost')
+ , content = path.join(part, 'content')
+ , config = require(path.join(part, 'config.example.js'))
+ , recover = process.env.RECOVER;
+
+//
+// Create the proper configuration.
+//
+exports.setup = function setup(cb) {
+ console.log('Running post-startup sync\n'.blue);
+
+ fetch();
+
+ //
+ // Get data from MongoDB.
+ //
+ function fetch() {
+ mongo.get(function open(db) {
+ db.collection('fs.files')
+ .find()
+ .sort({ filename: 1, uploadDate: recover ? 1 : -1 })
+ .toArray(process);
+ });
+ }
+
+ //
+ // Process each file in the results.
+ //
+ function process(err, results, prev) {
+ if (err) return cb(err);
+
+ results = results.filter(function newest(file) {
+ if (prev === file.filename) return false;
+ if (file.filename.match(/ghost.*db/)) return false; // Ignore database
+
+ prev = file.filename;
+ return true;
+ });
+
+ async.forEach(results, synchronise, function done() {
+ console.log('\nPost-startup sync completed\n'.blue);
+ cb.apply(cb, arguments);
+ });
+ }
+
+ //
+ // Get file content and save to filesystem.
+ //
+ function synchronise(file, fn) {
+ var full = path.join(content + file.filename);
+
+ // Get file content
+ mongo.fetch(file, function save(err, data) {
+ if (err) return fn(err);
+
+ // Check if we need to do mkdir -p
+ mkdirp(path.dirname(full), function(err, created) {
+ if (err) return fn(err);
+ if (created) console.log([
+ ' +++'.magenta,
+ created.replace(content, ''),
+ 'directory created'
+ ].join(' '));
+
+ // Write the content to disk.
+ fs.writeFile(full, data, function saved(err) {
+ if (err) return fn(err);
+
+ console.log(' +++ '.green + file.filename + ' synced to drone');
+ fn();
+ });
+ });
+ });
+ }
+};
View
@@ -96,7 +96,7 @@ exports.setup = function setup(cb) {
mongo.get(function open(db) {
db.collection('fs.files')
- .find()
+ .find({ filename: /ghost.*db/ }) // Fetch only the database
.sort({ filename: 1, uploadDate: recover ? 1 : -1 })
.toArray(process);
});

0 comments on commit 1d5d6fc

Please sign in to comment.