Skip to content

Commit

Permalink
server: lazy-load memcache module
Browse files Browse the repository at this point in the history
Don't require that the memcache module is installed if metrics_enabled=false.
  • Loading branch information
bnoordhuis committed Mar 28, 2012
1 parent 34d3c08 commit cfde236
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
7 changes: 3 additions & 4 deletions server/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function main(config) {
worlds = [],
lastTotalPlayers = 0,
checkPopulationInterval = setInterval(function() {
if(metrics.isReady) {
if(metrics && metrics.isReady) {
metrics.getTotalPlayers(function(totalPlayers) {
if(totalPlayers !== lastTotalPlayers) {
lastTotalPlayers = totalPlayers;
Expand Down Expand Up @@ -44,7 +44,7 @@ function main(config) {
}
};

if(config.metrics_enabled) {
if(metrics) {
metrics.getOpenWorldCount(function(open_world_count) {
// choose the least populated world among open worlds
world = _.min(_.first(worlds, open_world_count), function(w) { return w.playerCount; });
Expand Down Expand Up @@ -78,8 +78,7 @@ function main(config) {
var world = new WorldServer('world'+ (i+1), config.nb_players_per_world, server);
world.run(config.map_filepath);
worlds.push(world);

if(config.metrics_enabled) {
if(metrics) {
world.onPlayerAdded(onPopulationChange);
world.onPlayerRemoved(onPopulationChange);
}
Expand Down
7 changes: 3 additions & 4 deletions server/js/metrics.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

var cls = require("./lib/class"),
_ = require("underscore"),
memcache = require("memcache");
_ = require("underscore");

module.exports = Metrics = Class.extend({
init: function(config) {
var self = this;

this.config = config;
this.client = new memcache.Client(config.memcached_port, config.memcached_host),
this.client = new require("memcache").Client(config.memcached_port, config.memcached_host),
this.client.connect();
this.isReady = false;

Expand Down Expand Up @@ -73,4 +72,4 @@ module.exports = Metrics = Class.extend({
callback(result);
});
}
});
});

0 comments on commit cfde236

Please sign in to comment.