Skip to content

Commit

Permalink
better start order
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Tahler committed Jan 7, 2013
1 parent 5f6aa8d commit 2cd2998
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
36 changes: 26 additions & 10 deletions actionHero.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,34 @@ actionHero.prototype.start = function(params, next){
starters.push(i);
}
}
starters.forEach(function(starter){
self.api[starter]._start(self.api, function(){
self.api.log(" > start: " + starter, 'grey');
});
});

var started = 0;
var successMessage = "*** Server Started @ " + self.api.utils.sqlDateTime() + " ***";
self.api.bootTime = new Date().getTime();
self.api.log("server ID: " + self.api.id);
self.api.log(successMessage, ["green", "bold"]);
if(next !== null){
next(null, self.api);
if(starters.length == 0){
self.api.bootTime = new Date().getTime();
self.api.log("server ID: " + self.api.id);
self.api.log(successMessage, ["green", "bold"]);
if(next !== null){
next(null, self.api);
}
}else{
starters.forEach(function(starter){
started++;
self.api[starter]._start(self.api, function(){
process.nextTick(function(){
self.api.log(" > start: " + starter, 'grey');
started--;
if(started == 0){
self.api.bootTime = new Date().getTime();
self.api.log("server ID: " + self.api.id);
self.api.log(successMessage, ["green", "bold"]);
if(next !== null){
next(null, self.api);
}
}
});
});
});
}
};

Expand Down
2 changes: 1 addition & 1 deletion helpers/_specHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var specHelper = {}
var showLogs = false;
var showLogs = true;
specHelper.fs = require('fs');
specHelper.net = require('net');
specHelper.should = require('should');
Expand Down
2 changes: 1 addition & 1 deletion initializers/initRedis.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ var initPingAndCheck = function(api, next){
}

// start timers
api.redis._start = function(){
api.redis._start = function(api, next){
api.redis.ping(api, function(){
api.redis.checkForDroppedPeers(api, function(){
next();
Expand Down
70 changes: 65 additions & 5 deletions test/core_tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,62 @@ describe('Core: Tasks', function(){
var should = require("should");

before(function(done){
specHelper.prepare(0, function(api){
rawAPI = api;
apiObj = specHelper.cleanAPIObject(api);
done();
})
specHelper.stopServer(0, function(api){
specHelper.prepare(0, function(api){
rawAPI = api;
apiObj = specHelper.cleanAPIObject(api);

rawAPI.tasks.taskProcessors.forEach(function(taskProcessor){
taskProcessor.stop();
});

rawAPI.tasks.tasks['regular_any'] = {
name: 'regular_all',
description: 'task: ' + this.name,
scope: 'any',
frequency: 0,
run: function(api, params, next){
api.fs.writeFileSync(params.word);
next();
}
}

rawAPI.tasks.tasks['regular_all'] = {
name: 'regular_all',
description: 'task: ' + this.name,
scope: 'all',
frequency: 0,
run: function(api, params, next){
api.fs.writeFileSync(params.word);
next();
}
}

rawAPI.tasks.tasks['periodic_any'] = {
name: 'regular_all',
description: 'task: ' + this.name,
scope: 'any',
frequency: 1000,
run: function(api, params, next){
api.fs.writeFileSync(params.word);
next();
}
}

rawAPI.tasks.tasks['periodic_all'] = {
name: 'regular_all',
description: 'task: ' + this.name,
scope: 'all',
frequency: 1000,
run: function(api, params, next){
api.fs.writeFileSync(params.word);
next();
}
}

done();
});
});
});

after(function(done){
Expand All @@ -18,6 +69,15 @@ describe('Core: Tasks', function(){
})
});

it('setup worked', function(done){
rawAPI.utils.hashLength(rawAPI.tasks.tasks).should.equal(4 + 1);
rawAPI.tasks.taskProcessors.length.should.equal(1);
rawAPI.tasks.taskProcessors.forEach(function(taskProcessor){
taskProcessor.running.should.equal(false);
});
done();
});

it('all perioduc tasks should be enqueued when the server starts', function(done){
done();
});
Expand Down

0 comments on commit 2cd2998

Please sign in to comment.