Refactor database pool use

This commit is contained in:
nymkappa
2022-04-12 15:15:57 +09:00
parent 0814816950
commit c18d525f4a
10 changed files with 164 additions and 358 deletions

View File

@@ -1,8 +1,8 @@
const https = require('https');
import poolsParser from "../api/pools-parser";
import config from "../config";
import { DB } from "../database";
import logger from "../logger";
import poolsParser from '../api/pools-parser';
import config from '../config';
import { DB } from '../database';
import logger from '../logger';
/**
* Maintain the most recent version of pools.json
@@ -64,15 +64,11 @@ class PoolsUpdater {
* Fetch our latest pools.json sha from the db
*/
private async updateDBSha(githubSha: string) {
let connection;
try {
connection = await DB.getConnection();
await connection.query('DELETE FROM state where name="pools_json_sha"');
await connection.query(`INSERT INTO state VALUES('pools_json_sha', NULL, '${githubSha}')`);
connection.release();
await DB.query('DELETE FROM state where name="pools_json_sha"');
await DB.query(`INSERT INTO state VALUES('pools_json_sha', NULL, '${githubSha}')`);
} catch (e) {
logger.err('Cannot save github pools.json sha into the db. Reason: ' + (e instanceof Error ? e.message : e));
connection.release();
return undefined;
}
}
@@ -81,15 +77,11 @@ class PoolsUpdater {
* Fetch our latest pools.json sha from the db
*/
private async getShaFromDb(): Promise<string | undefined> {
let connection;
try {
connection = await DB.getConnection();
const [rows] = await connection.query('SELECT string FROM state WHERE name="pools_json_sha"');
connection.release();
const [rows]: any[] = await DB.query('SELECT string FROM state WHERE name="pools_json_sha"');
return (rows.length > 0 ? rows[0].string : undefined);
} catch (e) {
logger.err('Cannot fetch pools.json sha from db. Reason: ' + (e instanceof Error ? e.message : e));
connection.release();
return undefined;
}
}
@@ -140,7 +132,7 @@ class PoolsUpdater {
request.on('error', (error) => {
logger.err('Github API query failed. Reason: ' + error);
reject(error);
})
});
});
}
}