Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed Nov 22, 2016
1 parent cb20b8b commit 065daf6
Show file tree
Hide file tree
Showing 24 changed files with 324 additions and 322 deletions.
8 changes: 6 additions & 2 deletions actions/cacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ exports.cacheTest = {
required: true,
formatter: function (s) { return String(s) },
validator: function (s) {
if (s.length < 3) { return '`value` should be at least 3 letters long' }
else { return true }
if (s.length < 3) {
return '`value` should be at least 3 letters long'
} else { return true }
}
}
},
Expand All @@ -41,10 +42,13 @@ exports.cacheTest = {
data.response.cacheTestResults = {}

api.cache.save(key, value, 5000, function (error, resp) {
if (error) { return next(error) }
data.response.cacheTestResults.saveResp = resp
api.cache.size(function (error, numberOfCacheObjects) {
if (error) { return next(error) }
data.response.cacheTestResults.sizeResp = numberOfCacheObjects
api.cache.load(key, function (error, resp, expireTimestamp, createdAt, readAt) {
if (error) { return next(error) }
data.response.cacheTestResults.loadResp = {
key: key,
value: resp,
Expand Down
1 change: 1 addition & 0 deletions actions/createChatRoom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'

exports.createChatRoom = {
name: 'createChatRoom',
Expand Down
3 changes: 2 additions & 1 deletion actions/status.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const path = require('path')
const packageJSON = require(path.normalize(__dirname + path.sep + '..' + path.sep + 'package.json'))
const packageJSON = require(path.normalize(path.join(__dirname, '..', 'package.json')))

// These values are probably good starting points, but you should expect to tweak them for your application
const maxEventLoopDelay = process.env.eventLoopDelay || 10
Expand Down Expand Up @@ -34,6 +34,7 @@ exports.status = {

const checkEventLoop = function (callback) {
api.utils.eventLoopDelay(10000, function (error, eventLoopDelay) {
if (error) { return callback(error) }
data.response.eventLoopDelay = eventLoopDelay
if (eventLoopDelay > maxEventLoopDelay) {
data.response.nodeStatus = data.connection.localize('Node Unhealthy')
Expand Down
126 changes: 60 additions & 66 deletions bin/actionhero
Original file line number Diff line number Diff line change
@@ -1,82 +1,76 @@
#!/usr/bin/env node
'use strict';
'use strict'

const cluster = require('cluster');
const fs = require('fs');
const winston = require('winston');
const path = require('path');
const optimist = require('optimist');
const spawn = require('child_process').spawn;
const path = require('path')
const optimist = require('optimist')
const spawn = require('child_process').spawn

const actionheroRoot = path.normalize(__dirname + '/..');
let projectRoot;
const actionheroRoot = path.normalize(path.join(__dirname, '..'))
let projectRoot

if(process.env.projectRoot){
projectRoot = process.env.projectRoot;
}else if(process.env.project_root){
projectRoot = process.env.project_root;
}else if(process.env.PROJECT_ROOT){
projectRoot = process.env.PROJECT_ROOT;
}else{
projectRoot = path.normalize(process.cwd());
if (process.env.projectRoot) {
projectRoot = process.env.projectRoot
} else if (process.env.project_root) {
projectRoot = process.env.project_root
} else if (process.env.PROJECT_ROOT) {
projectRoot = process.env.PROJECT_ROOT
} else {
projectRoot = path.normalize(process.cwd())
}

const ActionheroPrototype = require(actionheroRoot + '/actionhero.js');
const actionhero = new ActionheroPrototype();
const configChanges = {};

let commands = [];
if(!optimist.argv._ || optimist.argv._.length === 0){ commands.push('start'); }
optimist.argv._.forEach(function(arg){ commands.push(arg); });
if(commands.length === 1 && ['generate', 'help', 'version'].indexOf(commands[0]) >= 0){
const ActionheroPrototype = require(actionheroRoot + '/actionhero.js')
const actionhero = new ActionheroPrototype()
const configChanges = {}

let commands = []
if (!optimist.argv._ || optimist.argv._.length === 0) { commands.push('start') }
optimist.argv._.forEach(function (arg) { commands.push(arg) })
if (commands.length === 1 && ['generate', 'help', 'version'].indexOf(commands[0]) >= 0) {
// when generating the project from scratch, we cannot rely on the normal initilizers
const runner = require(__dirname + path.sep + 'methods' + path.sep + commands.join(path.sep) + '.js');
runner({projectRoot: projectRoot}, function(error){
if(error){ throw error; }
setTimeout(process.exit, 500, 0);
});

}else{

actionhero.initialize({configChanges: configChanges}, function(error, api){
if(error){ throw error; }
api._context = actionhero;
const runner = require(path.join(__dirname, 'methods', commands.join(path.sep) + '.js'))
runner({projectRoot: projectRoot}, function (error) {
if (error) { throw error }
setTimeout(process.exit, 500, 0)
})
} else {
actionhero.initialize({configChanges: configChanges}, function (error, api) {
if (error) { throw error }
api._context = actionhero

try{
const runner = require(__dirname + path.sep + 'methods' + path.sep + commands.join(path.sep) + '.js');
try {
const runner = require(path.join(__dirname, 'methods', commands.join(path.sep) + '.js'))

api.log('--------------------------------------');
api.log(['ACTIONHERO COMMAND >> %s', commands.join(' ')]);
api.log(['projectRoot: %s', path.normalize(projectRoot)], 'debug');
api.log(['actionheroRoot: %s', path.normalize(actionheroRoot)], 'debug');
api.log('--------------------------------------');
api.log('--------------------------------------')
api.log(['ACTIONHERO COMMAND >> %s', commands.join(' ')])
api.log(['projectRoot: %s', path.normalize(projectRoot)], 'debug')
api.log(['actionheroRoot: %s', path.normalize(actionheroRoot)], 'debug')
api.log('--------------------------------------')

if(optimist.argv.daemon){
let newArgs = process.argv.splice(2);
for(let i in newArgs){
if(newArgs[i].indexOf('--daemon') >= 0){ newArgs.splice(i, 1); }
if (optimist.argv.daemon) {
let newArgs = process.argv.splice(2)
for (let i in newArgs) {
if (newArgs[i].indexOf('--daemon') >= 0) { newArgs.splice(i, 1) }
}
newArgs.push('--isDaemon=true');
const command = path.normalize(actionheroRoot + '/bin/actionhero');
const child = spawn(command, newArgs, {detached: true, cwd: process.cwd(), env: process.env, stdio: 'ignore'});
api.log(['%s %s', command, newArgs.join(' ')], 'debug');
api.log(['spawned child process with pid %s', child.pid], 'notice');
process.nextTick(process.exit);
}else{
runner(api, function(error, toStop){
if(error){ throw error; }
if(toStop){ setTimeout(process.exit, 500, 0); }
});
newArgs.push('--isDaemon=true')
const command = path.normalize(actionheroRoot + '/bin/actionhero')
const child = spawn(command, newArgs, {detached: true, cwd: process.cwd(), env: process.env, stdio: 'ignore'})
api.log(['%s %s', command, newArgs.join(' ')], 'debug')
api.log(['spawned child process with pid %s', child.pid], 'notice')
process.nextTick(process.exit)
} else {
runner(api, function (error, toStop) {
if (error) { throw error }
if (toStop) { setTimeout(process.exit, 500, 0) }
})
}
}catch(e){
if(!e.message.match(/Cannot find module/)){
throw(e);
}else{
api.log(['Error: `%s` is not a method I can perform', commands.join(' ')], 'error');
api.log('run `actionhero help` to learn more', 'error');
setTimeout(process.exit, 500, 1);
} catch (e) {
if (!e.message.match(/Cannot find module/)) {
throw (e)
} else {
api.log(['Error: `%s` is not a method I can perform', commands.join(' ')], 'error')
api.log('run `actionhero help` to learn more', 'error')
setTimeout(process.exit, 500, 1)
}
}
});
})
}
10 changes: 5 additions & 5 deletions bin/methods/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ module.exports = function (api, next) {
}

// reload utils, as they won't have been loaded yet
api.utils = require(path.normalize(__dirname + '/../../initializers/utils.js')).initialize(api, function (error) {
api.utils = require(path.normalize(path.join(__dirname, '/../../initializers/utils.js'))).initialize(api, function (error) {
if (error) { return next(error) }

// ////// DOCUMENTS ////////

let documents = {}

documents.projectMap = fs.readFileSync(__dirname + '/../templates/projectMap.txt')
documents.projectMap = fs.readFileSync(path.join(__dirname, '/../templates/projectMap.txt'))

const oldFileMap = {
configApiJs: '/config/api.js',
Expand All @@ -42,14 +42,14 @@ module.exports = function (api, next) {
}

for (let name in oldFileMap) {
documents[name] = fs.readFileSync(__dirname + '/../../' + oldFileMap[name])
documents[name] = fs.readFileSync(path.join(__dirname, '/../../', oldFileMap[name]))
}

const AHversionNumber = JSON.parse(documents.packageJson).version

documents.packageJson = String(fs.readFileSync(__dirname + '/../templates/package.json'))
documents.packageJson = String(fs.readFileSync(path.join(__dirname, '/../templates/package.json')))
documents.packageJson = documents.packageJson.replace('%%versionNumber%%', AHversionNumber)
documents.readmeMd = String(fs.readFileSync(__dirname + '/../templates/README.md'))
documents.readmeMd = String(fs.readFileSync(path.join(__dirname, '/../templates/README.md')))

// ////// LOGIC ////////

Expand Down
3 changes: 2 additions & 1 deletion bin/methods/generate/action.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 optimist = require('optimist')
const argv = optimist
.demand('name')
Expand All @@ -10,7 +11,7 @@ const argv = optimist
.argv

module.exports = function (api, next) {
let data = fs.readFileSync(__dirname + '/../../templates/action.js')
let data = fs.readFileSync(path.join(__dirname, '/../../templates/action.js'))
data = String(data);

[
Expand Down
3 changes: 2 additions & 1 deletion bin/methods/generate/initializer.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 optimist = require('optimist')
const argv = optimist
.demand('name')
Expand All @@ -14,7 +15,7 @@ const argv = optimist
.argv

module.exports = function (api, next) {
let data = fs.readFileSync(__dirname + '/../../templates/initializer.js')
let data = fs.readFileSync(path.join(__dirname, '/../../templates/initializer.js'))
data = String(data);

[
Expand Down
3 changes: 2 additions & 1 deletion bin/methods/generate/server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'

const fs = require('fs')
const path = require('path')
const optimist = require('optimist')
const argv = optimist
.demand('name')
.describe('name', 'The name of the initializer')
.argv

module.exports = function (api, next) {
let data = fs.readFileSync(__dirname + '/../../templates/server.js')
let data = fs.readFileSync(path.join(__dirname, '/../../templates/server.js'))
data = String(data);

[
Expand Down
3 changes: 2 additions & 1 deletion bin/methods/generate/task.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 optimist = require('optimist')
const argv = optimist
.demand('name')
Expand All @@ -14,7 +15,7 @@ const argv = optimist
.argv

module.exports = function (api, next) {
let data = fs.readFileSync(__dirname + '/../../templates/task.js')
let data = fs.readFileSync(path.join(__dirname, '/../../templates/task.js'))
data = String(data);

[
Expand Down
3 changes: 2 additions & 1 deletion bin/methods/help.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const fs = require('fs')
const path = require('path')

module.exports = function (api, next) {
const help = fs.readFileSync(__dirname + '/../templates/help.txt').toString()
const help = fs.readFileSync(path.join(__dirname, '/../templates/help.txt')).toString()
help.split('\n').forEach(function (line) { console.log(line) })
next(null, true)
}
2 changes: 1 addition & 1 deletion bin/methods/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = function (api, next) {
} else {
const content = fs.readFileSync(file)
const fileParts = pluginConfigFile.split(path.sep)
const localConfigFile = linkRelativeBase + 'config' + path.sep + prepend + fileParts[(fileParts.length - 1)]
let localConfigFile = linkRelativeBase + 'config' + path.sep + prepend + fileParts[(fileParts.length - 1)]
if (process.env.ACTIONHERO_CONFIG) {
localConfigFile = process.env.ACTIONHERO_CONFIG + path.sep + prepend + fileParts[(fileParts.length - 1)]
}
Expand Down
11 changes: 7 additions & 4 deletions bin/methods/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ module.exports = function (api, next) {

if (cluster.isWorker) {
process.on('message', function (msg) {
if (msg === 'start') { startServer() }
else if (msg === 'stop') { stopServer() }
else if (msg === 'stopProcess') { stopProcess() }
if (msg === 'start') {
startServer()
} else if (msg === 'stop') {
stopServer()
} else if (msg === 'stopProcess') {
stopProcess()
// in cluster, we cannot re-bind the port
// so kill this worker, and then let the cluster start a new worker
else if (msg === 'restart') { stopProcess() }
} else if (msg === 'restart') { stopProcess() }
})

process.on('uncaughtException', function (error) {
Expand Down
21 changes: 7 additions & 14 deletions bin/methods/start/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const winston = require('winston')
const isrunning = require('is-running')
const optimist = require('optimist')

const argv = optimist
optimist
.describe('workers', 'How many worker node processes')
.default('workers', os.cpus().length)
.describe('workerTitlePrefix', 'Set worker title prefix')
Expand Down Expand Up @@ -288,8 +288,7 @@ ActionHeroCluster.prototype.start = function (callback) {
if (error) {
this.log(error, 'error')
process.exit(1)
}
else {
} else {
this.work()
if (typeof callback === 'function') { callback() }
}
Expand Down Expand Up @@ -335,9 +334,7 @@ ActionHeroCluster.prototype.work = function () {
worker = this.workers[(this.workers.length - 1)]
this.log('signaling worker #' + worker.id + ' to stop', 'info')
worker.stop()
}

else if (
} else if (
(this.options.expectedWorkers > this.workers.length) &&
!stateCounts.starting &&
!stateCounts.restarting
Expand All @@ -352,9 +349,7 @@ ActionHeroCluster.prototype.work = function () {
worker = new Worker(this, workerId, env)
worker.start()
this.workers.push(worker)
}

else if (
} else if (
this.workersToRestart.length > 0 &&
!stateCounts.starting &&
!stateCounts.stopping &&
Expand All @@ -365,9 +360,7 @@ ActionHeroCluster.prototype.work = function () {
this.workers.forEach((w) => {
if (w.id === workerId) { w.stop() }
})
}

else {
} else {
if (stateCounts.started === this.workers.length) {
this.log('cluster equilibrium state reached with ' + this.workers.length + ' workers', 'notice')
}
Expand All @@ -378,9 +371,9 @@ ActionHeroCluster.prototype.work = function () {

module.exports = function (api) {
let options = {
execPath: path.normalize(__dirname + '/../../actionhero'),
execPath: path.normalize(path.join(__dirname, '/../../actionhero')),
args: 'start',
silent: (optimist.argv.silent === 'true' || optimist.argv.silent === true) ? true : false,
silent: (optimist.argv.silent === 'true' || optimist.argv.silent === true),
expectedWorkers: optimist.argv.workers,
id: api.id,
buildEnv: (workerId) => {
Expand Down
Loading

0 comments on commit 065daf6

Please sign in to comment.