@kamil.michalak/laravel-echo-server
Version:
Laravel Echo Node JS Server for Socket.io
94 lines (93 loc) • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisDatabase = void 0;
var log_1 = require("./../log");
var os_1 = require("os");
var _ = require("lodash");
var Redis = require('ioredis');
var RedisDatabase = (function () {
function RedisDatabase(options) {
var _this = this;
this.options = options;
this._serverHostName = os_1.hostname();
this._serverName = (options.databaseConfig.instancePrefix || '') + this._serverHostName + ':' + Date.now();
this._checkInterval = options.databaseConfig.checkInterval || 60;
this._checkGuard = options.databaseConfig.checkGuard || 20;
this._redis = new Redis(options.databaseConfig.redis);
if (this.options.devMode) {
log_1.Log.info({ "Redis check interval:": this._checkInterval, "Redis check guard:": this._checkGuard });
}
this._serverList = [this._serverName];
this.pingAlive();
setInterval(function () {
_this.pingAlive();
}, this._checkInterval * 1000);
process.on("exit", function () {
_this._redis.hdel("list:server_list", _this._serverName);
});
}
RedisDatabase.prototype.pingAlive = function () {
var _this = this;
var time = Date.now();
this._redis.hset("list:server_list", this._serverName, JSON.stringify({ time: time, name: this._serverName, host: this._serverHostName }));
this._redis.hvals("list:server_list")
.catch(function (e) { log_1.Log.error(e); })
.then(function (value) {
value = _.flatten((value || []).map(function (i) { return JSON.parse(i); }));
if (_this.options.devMode) {
log_1.Log.info({ "Ping check server:": value });
}
_this._serverList = value.filter(function (i) {
if (i.host === _this._serverHostName && i.name !== _this._serverName) {
log_1.Log.info({ "Remove old instance from list:": i });
_this._redis.hdel("list:server_list", i.name);
return false;
}
if (i.time < (time - (_this._checkInterval + _this._checkGuard) * 1000)) {
log_1.Log.info({ "Remove server from list:": i });
_this._redis.hdel("list:server_list", i.name);
return false;
}
return true;
}).map(function (i) { return i.name; });
});
};
RedisDatabase.prototype.get = function (key) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._redis.hget("list:" + key, _this._serverName)
.catch(function (e) { log_1.Log.error(e); })
.then(function (value) {
resolve(JSON.parse(value));
});
});
};
RedisDatabase.prototype.getAll = function (key) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._redis.hgetall("list:" + key)
.catch(function (e) { log_1.Log.error(e); })
.then(function (values) {
Object.keys(values)
.filter(function (i) {
return _this._serverList.indexOf(i) < 0;
})
.forEach(function (i) {
delete values[i];
_this._redis.hdel("list:" + key, i);
});
resolve(_.flatten(Object.values(values).map(function (i) { return JSON.parse(i); })));
});
});
};
RedisDatabase.prototype.set = function (key, value) {
this._redis.hset("list:" + key, this._serverName, JSON.stringify(value));
};
RedisDatabase.prototype.publish = function (channel, value) {
if (this.options.databaseConfig.publishPresence === true) {
this._redis.publish((this.options.databaseConfig.redis.keyPrefix || '') + channel, value);
}
};
return RedisDatabase;
}());
exports.RedisDatabase = RedisDatabase;