Skip to content

Commit

Permalink
🎨 🐎 improve ghost run memory usage by migrating during ghost start
Browse files Browse the repository at this point in the history
no issue
- move knex-migrator step to `ghost start`, making `ghost run` much more
performant
  • Loading branch information
acburdine committed Mar 1, 2017
1 parent a1d25e8 commit e2d2b5b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
19 changes: 1 addition & 18 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const path = require('path');
const KnexMigrator = require('knex-migrator');

const Config = require('../utils/config');
const Instance = require('../utils/instance');
Expand All @@ -11,27 +10,11 @@ let instance;
module.exports.execute = function execute() {
checkValidInstall('run');

process.env.NODE_ENV = this.environment;
process.env.paths__contentPath = path.join(process.cwd(), 'content');

let knexMigrator = new KnexMigrator({
knexMigratorFilePath: path.join(process.cwd(), 'current')
});

this.service.setConfig(Config.load(this.environment));

return knexMigrator.isDatabaseOK().catch((error) => {
if (error.code === 'DB_NOT_INITIALISED' ||
error.code === 'MIGRATION_TABLE_IS_MISSING') {
return knexMigrator.init();
} else if (error.code === 'DB_NEEDS_MIGRATION') {
return knexMigrator.migrate();
}
}).then(() => {
instance = new Instance(this.ui, this.service.process);
}).catch((error) => {
this.service.process.error(error.message);
});
instance = new Instance(this.ui, this.service.process);
};

module.exports.exit = function exit() {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports.execute = function execute(options) {
// In the case that the user runs `ghost setup --local`, we want to make
// sure we're set up in development mode
this.development = true;
this.environment = 'development';
process.env.NODE_ENV = this.environment = 'development';
}

return configCommand.execute.call(this, null, null, options).then((config) => {
Expand Down
36 changes: 26 additions & 10 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const fs = require('fs');
const path = require('path');
const KnexMigrator = require('knex-migrator');

const Config = require('../utils/config');
const checkValidInstall = require('../utils/check-valid-install');
Expand Down Expand Up @@ -30,7 +31,7 @@ module.exports.execute = function execute(options) {
!fs.existsSync(path.join(process.cwd(), 'config.production.json'))) {
this.ui.log('Found a development config but not a production config, starting in development mode instead.', 'yellow');
this.development = false;
process.NODE_ENV = this.environment = 'development';
process.env.NODE_ENV = this.environment = 'development';
}

let config = Config.load(this.environment);
Expand All @@ -42,17 +43,32 @@ module.exports.execute = function execute(options) {

this.service.setConfig(config);

let start = () => Promise.resolve(this.service.process.start(process.cwd(), this.environment)).then(() => {
register(config, this.environment);
cliConfig.set('running', this.environment).save();
return Promise.resolve();
process.env.paths__contentPath = path.join(process.cwd(), 'content');

let knexMigrator = new KnexMigrator({
knexMigratorFilePath: path.join(process.cwd(), 'current')
});

if (options.quiet) {
return start();
}
return knexMigrator.isDatabaseOK().catch((error) => {
if (error.code === 'DB_NOT_INITIALISED' ||
error.code === 'MIGRATION_TABLE_IS_MISSING') {
return knexMigrator.init();
} else if (error.code === 'DB_NEEDS_MIGRATION') {
return knexMigrator.migrate();
}
}).then(() => {
let start = () => Promise.resolve(this.service.process.start(process.cwd(), this.environment)).then(() => {
register(config, this.environment);
cliConfig.set('running', this.environment).save();
return Promise.resolve();
});

if (options.quiet) {
return start();
}

return this.ui.run(start, 'Starting Ghost').then(() => {
this.ui.log(`You can access your blog at ${config.get('url')}`, 'cyan');
return this.ui.run(start, 'Starting Ghost').then(() => {
this.ui.log(`You can access your blog at ${config.get('url')}`, 'cyan');
});
});
};

0 comments on commit e2d2b5b

Please sign in to comment.