mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-12-26 14:09:45 -08:00
disable pool for sqlite, re-use a connection to improve the performance.
This commit is contained in:
parent
86826fb826
commit
61e758d872
|
@ -3,6 +3,7 @@ const { sleep } = require("../src/util");
|
||||||
const { R } = require("redbean-node");
|
const { R } = require("redbean-node");
|
||||||
const { setSetting, setting } = require("./util-server");
|
const { setSetting, setting } = require("./util-server");
|
||||||
const knex = require("knex");
|
const knex = require("knex");
|
||||||
|
const sqlite3 = require("@louislam/sqlite3");
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
|
|
||||||
|
@ -10,24 +11,32 @@ class Database {
|
||||||
static path = "./data/kuma.db";
|
static path = "./data/kuma.db";
|
||||||
static latestVersion = 6;
|
static latestVersion = 6;
|
||||||
static noReject = true;
|
static noReject = true;
|
||||||
|
static sqliteInstance = null;
|
||||||
|
|
||||||
static async connect() {
|
static async connect() {
|
||||||
const Dialect = require("knex/lib/dialects/sqlite3/index.js");
|
|
||||||
Dialect.prototype._driver = () => require("@louislam/sqlite3");
|
|
||||||
|
|
||||||
R.setup(knex({
|
if (! this.sqliteInstance) {
|
||||||
|
this.sqliteInstance = new sqlite3.Database(Database.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Dialect = require("knex/lib/dialects/sqlite3/index.js");
|
||||||
|
Dialect.prototype._driver = () => sqlite3;
|
||||||
|
|
||||||
|
// Disable Pool by overriding acquireConnection()
|
||||||
|
Dialect.prototype.acquireConnection = async () => {
|
||||||
|
return this.sqliteInstance;
|
||||||
|
}
|
||||||
|
Dialect.prototype.releaseConnection = async () => { }
|
||||||
|
|
||||||
|
const knexInstance = knex({
|
||||||
client: Dialect,
|
client: Dialect,
|
||||||
connection: {
|
connection: {
|
||||||
filename: Database.path,
|
filename: Database.path,
|
||||||
},
|
},
|
||||||
useNullAsDefault: true,
|
useNullAsDefault: true,
|
||||||
pool: {
|
});
|
||||||
min: 1,
|
|
||||||
max: 1,
|
R.setup(knexInstance);
|
||||||
acquireTimeoutMillis: 3600 * 1000,
|
|
||||||
idleTimeoutMillis: 3600 * 1000
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (process.env.SQL_LOG === "1") {
|
if (process.env.SQL_LOG === "1") {
|
||||||
R.debug(true);
|
R.debug(true);
|
||||||
|
@ -121,27 +130,10 @@ class Database {
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
static async close() {
|
static async close() {
|
||||||
const listener = (reason, p) => {
|
if (this.sqliteInstance) {
|
||||||
Database.noReject = false;
|
this.sqliteInstance.close();
|
||||||
};
|
|
||||||
process.addListener("unhandledRejection", listener);
|
|
||||||
|
|
||||||
console.log("Closing DB")
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
Database.noReject = true;
|
|
||||||
await R.close()
|
|
||||||
await sleep(2000)
|
|
||||||
|
|
||||||
if (Database.noReject) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
console.log("Waiting to close the db")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
console.log("SQLite closed")
|
console.log("Stopped database");
|
||||||
|
|
||||||
process.removeListener("unhandledRejection", listener);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -811,11 +811,10 @@ async function shutdownFunction(signal) {
|
||||||
}
|
}
|
||||||
await sleep(2000);
|
await sleep(2000);
|
||||||
await Database.close();
|
await Database.close();
|
||||||
console.log("Stopped DB")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function finalFunction() {
|
function finalFunction() {
|
||||||
console.log("Graceful Shutdown Done")
|
console.log("Graceful shutdown successfully!");
|
||||||
}
|
}
|
||||||
|
|
||||||
gracefulShutdown(server, {
|
gracefulShutdown(server, {
|
||||||
|
|
Loading…
Reference in a new issue