mirror of
https://github.com/Wolvan/poll.horse.git
synced 2024-11-28 15:17:59 +01:00
Fix SQL connection on wakeup in heroku
When a heroku dyno goes into sleep mode it loses connection to the backend database. Once it is woken back up, no more reads or writes could be done anymore due to a dead connection. This change reinstates a new db connection when the connection is fatally terminated.
This commit is contained in:
parent
33c9ca168e
commit
1ad2c8c1a0
1 changed files with 11 additions and 2 deletions
|
@ -7,14 +7,23 @@ import { BackendPoll as Poll } from "./Poll";
|
||||||
|
|
||||||
export default class MySQLStorage extends Storage {
|
export default class MySQLStorage extends Storage {
|
||||||
#db: mysql.Connection;
|
#db: mysql.Connection;
|
||||||
|
#options: mysql.ConnectionOptions;
|
||||||
|
#createConnection(mysqlInstance?: mysql.Connection): void {
|
||||||
|
if (!mysqlInstance) this.#db = mysql.createConnection(this.#options);
|
||||||
|
this.#db.on("error", (err: mysql.QueryError) => {
|
||||||
|
if (err.fatal) this.#createConnection();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
constructor(options: mysql.ConnectionOptions) {
|
constructor(options: mysql.ConnectionOptions) {
|
||||||
super();
|
super();
|
||||||
|
this.#options = options;
|
||||||
console.debug("Initiating MySQLStorage.");
|
console.debug("Initiating MySQLStorage.");
|
||||||
this.#db = mysql.createConnection(options);
|
this.#db = mysql.createConnection(this.#options);
|
||||||
|
this.#createConnection(this.#db);
|
||||||
}
|
}
|
||||||
|
|
||||||
async init(): Promise<this> {
|
async init(): Promise<this> {
|
||||||
await this.#db.promise().connect();
|
|
||||||
await this.#db.promise().query(`
|
await this.#db.promise().query(`
|
||||||
CREATE TABLE IF NOT EXISTS polls (
|
CREATE TABLE IF NOT EXISTS polls (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
|
Loading…
Reference in a new issue