Comparing version 1.2.1 to 1.2.2
@@ -59,2 +59,3 @@ "use strict"; | ||
var notificationLogger = debug_1.default("pg-listen:notification"); | ||
var paranoidLogger = debug_1.default("pg-listen:paranoid"); | ||
var subscriptionLogger = debug_1.default("pg-listen:subscription"); | ||
@@ -70,3 +71,3 @@ var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; | ||
var reconnect = function (onAttempt) { return __awaiter(_this, void 0, void 0, function () { | ||
var startTime, attempt, newClient, error_1; | ||
var startTime, _loop_1, attempt, state_1; | ||
return __generator(this, function (_a) { | ||
@@ -77,30 +78,54 @@ switch (_a.label) { | ||
startTime = Date.now(); | ||
_loop_1 = function (attempt) { | ||
var newClient_1, connecting, error_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
connectionLogger("PostgreSQL reconnection attempt #" + attempt + "..."); | ||
onAttempt(attempt); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 4, , 6]); | ||
newClient_1 = new Client(effectiveConnectionConfig); | ||
connecting = new Promise(function (resolve, reject) { | ||
newClient_1.once("connect", resolve); | ||
newClient_1.once("end", function () { return reject(Error("Connection ended.")); }); | ||
newClient_1.once("error", reject); | ||
}); | ||
return [4 /*yield*/, newClient_1.connect()]; | ||
case 2: | ||
_a.sent(); | ||
return [4 /*yield*/, connecting]; | ||
case 3: | ||
_a.sent(); | ||
connectionLogger("PostgreSQL reconnection succeeded"); | ||
return [2 /*return*/, { value: newClient_1 }]; | ||
case 4: | ||
error_1 = _a.sent(); | ||
connectionLogger("PostgreSQL reconnection attempt failed:", error_1); | ||
return [4 /*yield*/, delay(retryInterval)]; | ||
case 5: | ||
_a.sent(); | ||
if (retryTimeout && (Date.now() - startTime) > retryTimeout) { | ||
throw new Error("Stopping PostgreSQL reconnection attempts after " + retryTimeout + "ms timeout has been reached."); | ||
} | ||
return [3 /*break*/, 6]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
}); | ||
}; | ||
attempt = 1; | ||
_a.label = 1; | ||
case 1: | ||
if (!(attempt < retryLimit || !retryLimit)) return [3 /*break*/, 7]; | ||
connectionLogger("PostgreSQL reconnection attempt #" + attempt + "..."); | ||
onAttempt(attempt); | ||
_a.label = 2; | ||
if (!(attempt < retryLimit || !retryLimit)) return [3 /*break*/, 4]; | ||
return [5 /*yield**/, _loop_1(attempt)]; | ||
case 2: | ||
_a.trys.push([2, 4, , 6]); | ||
newClient = new Client(effectiveConnectionConfig); | ||
return [4 /*yield*/, newClient.connect()]; | ||
state_1 = _a.sent(); | ||
if (typeof state_1 === "object") | ||
return [2 /*return*/, state_1.value]; | ||
_a.label = 3; | ||
case 3: | ||
_a.sent(); | ||
return [2 /*return*/, newClient]; | ||
case 4: | ||
error_1 = _a.sent(); | ||
connectionLogger("PostgreSQL reconnection attempt failed:", error_1); | ||
return [4 /*yield*/, delay(retryInterval)]; | ||
case 5: | ||
_a.sent(); | ||
if (retryTimeout && (Date.now() - startTime) > retryTimeout) { | ||
throw new Error("Stopping PostgreSQL reconnection attempts after " + retryTimeout + "ms timeout has been reached."); | ||
} | ||
return [3 /*break*/, 6]; | ||
case 6: | ||
attempt++; | ||
return [3 /*break*/, 1]; | ||
case 7: throw new Error("Reconnecting notification client to PostgreSQL database failed."); | ||
case 4: throw new Error("Reconnecting notification client to PostgreSQL database failed."); | ||
} | ||
@@ -144,8 +169,10 @@ }); | ||
_a.trys.push([0, 2, , 4]); | ||
return [4 /*yield*/, dbClient.query("SELECT 1")]; | ||
return [4 /*yield*/, dbClient.query("SELECT pg_backend_pid()")]; | ||
case 1: | ||
_a.sent(); | ||
paranoidLogger("Paranoid connection check ok"); | ||
return [3 /*break*/, 4]; | ||
case 2: | ||
error_2 = _a.sent(); | ||
paranoidLogger("Paranoid connection check failed"); | ||
connectionLogger("Paranoid connection check failed:", error_2); | ||
@@ -179,25 +206,25 @@ return [4 /*yield*/, reconnect()]; | ||
var dbClient = initialDBClient; | ||
var reinitializingRightNow = false; | ||
var subscribedChannels = []; | ||
var cancelEventForwarding = function () { return undefined; }; | ||
var cancelParanoidChecking = function () { return undefined; }; | ||
var initialize = function (client) { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
// Wire the DB client events to our exposed emitter's events | ||
cancelEventForwarding = forwardDBNotificationEvents(client, emitter); | ||
dbClient.on("error", function (error) { | ||
var initialize = function (client) { | ||
// Wire the DB client events to our exposed emitter's events | ||
cancelEventForwarding = forwardDBNotificationEvents(client, emitter); | ||
dbClient.on("error", function (error) { | ||
if (!reinitializingRightNow) { | ||
connectionLogger("DB Client error:", error); | ||
reinitialize(); | ||
}); | ||
dbClient.on("end", function () { | ||
} | ||
}); | ||
dbClient.on("end", function () { | ||
if (!reinitializingRightNow) { | ||
connectionLogger("DB Client connection ended"); | ||
if (!closing) { | ||
reinitialize(); | ||
} | ||
}); | ||
if (paranoidChecking) { | ||
cancelParanoidChecking = scheduleParanoidChecking(client, paranoidChecking, reinitialize); | ||
reinitialize(); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); }; | ||
if (paranoidChecking) { | ||
cancelParanoidChecking = scheduleParanoidChecking(client, paranoidChecking, reinitialize); | ||
} | ||
}; | ||
// No need to handle errors when calling `reinitialize()`, it handles its errors itself | ||
@@ -209,15 +236,23 @@ var reinitialize = function () { return __awaiter(_this, void 0, void 0, function () { | ||
case 0: | ||
_a.trys.push([0, 4, , 5]); | ||
if (reinitializingRightNow || closing) { | ||
return [2 /*return*/]; | ||
} | ||
reinitializingRightNow = true; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 4, 5, 6]); | ||
cancelParanoidChecking(); | ||
cancelEventForwarding(); | ||
dbClient.removeAllListeners(); | ||
dbClient.once("error", function (error) { return connectionLogger("Previous DB client errored after reconnecting already:", error); }); | ||
dbClient.end(); | ||
return [4 /*yield*/, reconnect(function (attempt) { return emitter.emit("reconnect", attempt); })]; | ||
case 1: | ||
case 2: | ||
dbClient = _a.sent(); | ||
return [4 /*yield*/, initialize(dbClient)]; | ||
case 2: | ||
_a.sent(); | ||
return [4 /*yield*/, Promise.all(subscribedChannels.map(function (channelName) { return "LISTEN " + pg_format_1.default.ident(channelName); }))]; | ||
initialize(dbClient); | ||
subscriptionLogger("Re-subscribing to channels: " + subscribedChannels.join(", ")); | ||
return [4 /*yield*/, Promise.all(subscribedChannels.map(function (channelName) { return dbClient.query("LISTEN " + pg_format_1.default.ident(channelName)); }))]; | ||
case 3: | ||
_a.sent(); | ||
return [3 /*break*/, 5]; | ||
return [3 /*break*/, 6]; | ||
case 4: | ||
@@ -228,4 +263,7 @@ error_3 = _a.sent(); | ||
emitter.emit("error", error_3); | ||
return [3 /*break*/, 5]; | ||
case 5: return [2 /*return*/]; | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
reinitializingRightNow = false; | ||
return [7 /*endfinally*/]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
@@ -232,0 +270,0 @@ }); |
{ | ||
"name": "pg-listen", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"license": "MIT", | ||
@@ -37,4 +37,4 @@ "description": "PostgreSQL LISTEN & NOTIFY that finally works.", | ||
"@types/pg-format": "^1.0.0", | ||
"ava": "^1.3.1", | ||
"lint-staged": "^8.1.5", | ||
"ava": "^1.4.1", | ||
"lint-staged": "^8.1.7", | ||
"pg": "^7.9.0", | ||
@@ -41,0 +41,0 @@ "prettier": "^1.16.4", |
26459
413