Refactored Autoload
Autoload now uses kernel events to watch files on disk. This greatly increases performance and reliability. Autoload will also watch user.json files for changes and reload the users data.
This commit is contained in:
parent
0ba67481fb
commit
8a61e4e969
@ -13,7 +13,7 @@ function ClientManager() {
|
|||||||
if(!/^win/.test(process.platform)) {
|
if(!/^win/.test(process.platform)) {
|
||||||
process.on('SIGHUP', function() {
|
process.on('SIGHUP', function() {
|
||||||
console.log("Received 'SIGHUP'. Reloading Users.");
|
console.log("Received 'SIGHUP'. Reloading Users.");
|
||||||
self.loadUsers();
|
self.reloadUsers();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,9 +55,35 @@ ClientManager.prototype.loadUser = function(name) {
|
|||||||
name,
|
name,
|
||||||
json
|
json
|
||||||
));
|
));
|
||||||
console.log(
|
console.log("User '%s' loaded.", name);
|
||||||
"User '" + name + "' loaded."
|
}
|
||||||
);
|
};
|
||||||
|
|
||||||
|
ClientManager.prototype.reloadUsers = function() {
|
||||||
|
var users = this.getUsers();
|
||||||
|
for (var i in users) {
|
||||||
|
this.reloadUser(users[i]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ClientManager.prototype.reloadUser = function(name) {
|
||||||
|
var client = this.findClient(name);
|
||||||
|
if (client) {
|
||||||
|
try {
|
||||||
|
var json = fs.readFileSync(
|
||||||
|
Helper.HOME + "/users/" + name + "/user.json",
|
||||||
|
"utf-8"
|
||||||
|
);
|
||||||
|
json = JSON.parse(json);
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!json) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
client.config = json;
|
||||||
|
console.log("User '%s' reloaded.", name);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -117,34 +143,66 @@ ClientManager.prototype.removeUser = function(name) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ClientManager.prototype.watchUser = function(name) {
|
||||||
|
var self = this;
|
||||||
|
var client = this.findClient(name);
|
||||||
|
if(!client || client.watcher) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var path = Helper.HOME + "/users/" + client.name + "/user.json";
|
||||||
|
var lastReload = Date.now();
|
||||||
|
client.watcher = fs.watch(path, {persistent: false}, function(event, filename) {
|
||||||
|
switch (event) {
|
||||||
|
case "change":
|
||||||
|
// user.json modified
|
||||||
|
if(Date.now() - lastReload > 50) {
|
||||||
|
self.reloadUser(client.name);
|
||||||
|
lastReload = Date.now();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
ClientManager.prototype.autoload = function(sockets) {
|
ClientManager.prototype.autoload = function(sockets) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var loaded = ["erming"];
|
|
||||||
setInterval(function() {
|
|
||||||
var loaded = _.pluck(
|
|
||||||
self.clients,
|
|
||||||
"name"
|
|
||||||
);
|
|
||||||
|
|
||||||
var added = _.difference(self.getUsers(), loaded);
|
// Listen to new users being added/removed
|
||||||
_.each(added, function(name) {
|
fs.watch(Helper.HOME + "/users/", { persistent: false }, function(event, filename) {
|
||||||
self.loadUser(name);
|
switch (event) {
|
||||||
});
|
case "rename":
|
||||||
|
if(filename === null) {
|
||||||
var removed = _.difference(loaded, self.getUsers());
|
// User removed.
|
||||||
_.each(removed, function(name) {
|
var removed = _(self.clients)
|
||||||
var client = _.find(
|
.pluck('name')
|
||||||
self.clients, {
|
.difference(self.getUsers())
|
||||||
name: name
|
.value();
|
||||||
}
|
_.each(removed, function(name) {
|
||||||
);
|
var client = self.findClient(name);
|
||||||
if (client) {
|
if (client) {
|
||||||
client.quit();
|
client.quit();
|
||||||
self.clients = _.without(self.clients, client);
|
if(client.watcher) {
|
||||||
console.log(
|
client.watcher.close();
|
||||||
"User '" + name + "' disconnected."
|
}
|
||||||
);
|
self.clients = _.without(self.clients, client);
|
||||||
|
console.log("User '%s' disconnected.", name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// User created.
|
||||||
|
self.loadUser(filename);
|
||||||
|
self.watchUser(filename);
|
||||||
}
|
}
|
||||||
});
|
break;
|
||||||
}, 1000);
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Listen to user modification
|
||||||
|
_.each(this.clients, function(client) {
|
||||||
|
self.watchUser(client.name);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user