Skip to content

Commit 4fb867f

Browse files
committed
coa-updater: Increase timeouts
1 parent 8f2aa4b commit 4fb867f

1 file changed

Lines changed: 62 additions & 24 deletions

File tree

apps/coa-updater/index.ts

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,39 @@ import { createPool } from "mariadb";
88
const dataPath = "/tmp/inducks",
99
isvPath = `${dataPath}/isv`;
1010

11+
/** Run CLI without a shell so MYSQL_ROOT_PASSWORD is not mangled ($, !, spaces, etc.). */
12+
async function runMariadbCheck(): Promise<number> {
13+
const host = process.env.MYSQL_HOST;
14+
const password = process.env.MYSQL_ROOT_PASSWORD;
15+
const database = process.env.MYSQL_DATABASE;
16+
if (!host || password === undefined || !database) {
17+
throw new Error(
18+
"MYSQL_HOST, MYSQL_ROOT_PASSWORD, and MYSQL_DATABASE are required for mariadb-check",
19+
);
20+
}
21+
const proc = Bun.spawn(
22+
[
23+
"mariadb-check",
24+
"-h",
25+
host,
26+
"-uroot",
27+
`-p${password}`,
28+
"-v",
29+
database,
30+
],
31+
{ stdout: "inherit", stderr: "inherit" },
32+
);
33+
return proc.exited;
34+
}
35+
1136
const poolParams = {
1237
host: process.env.MYSQL_HOST,
1338
port: parseInt(process.env.MYSQL_PORT || "3306"),
1439
user: "root",
1540
password: process.env.MYSQL_ROOT_PASSWORD,
41+
connectionLimit: 5,
1642
multipleStatements: true,
1743
permitLocalInfile: true,
18-
// Prevent "socket has unexpectedly been closed" during long LOAD DATA operations
19-
// (inducks_entry is large; default net_read_timeout/net_write_timeout is 60s)
2044
sessionVariables: {
2145
net_read_timeout: 1800, // 30 minutes
2246
net_write_timeout: 1800, // 30 minutes
@@ -149,61 +173,75 @@ set unique_checks = 1;
149173
set foreign_key_checks = 1;
150174
set sql_log_bin=1`;
151175

152-
const cleanSqlStatements = cleanSql.split(";");
153-
154-
const connection = await pool.getConnection();
155-
await connection.query(
156-
`DROP DATABASE IF EXISTS ${process.env.MYSQL_DATABASE_NEW};CREATE DATABASE ${process.env.MYSQL_DATABASE_NEW} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; set global net_buffer_length=1000000;
157-
set global max_allowed_packet=1000000000; `,
158-
);
176+
const cleanSqlStatements = cleanSql
177+
.split(";")
178+
.map((s) => s.trim())
179+
.filter((s) => s.length > 0);
180+
181+
const setupConnection = await pool.getConnection();
182+
for (const statement of [
183+
`DROP DATABASE IF EXISTS ${process.env.MYSQL_DATABASE_NEW}`,
184+
`CREATE DATABASE ${process.env.MYSQL_DATABASE_NEW} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`,
185+
"SET GLOBAL net_buffer_length=1000000",
186+
"SET GLOBAL max_allowed_packet=1000000000",
187+
]) {
188+
await setupConnection.query(statement);
189+
}
190+
await setupConnection.release();
159191

160192
const newDbPool = createPool({
161193
...poolParams,
162194
database: process.env.MYSQL_DATABASE_NEW,
163195
});
164196
const newDbConnection = await newDbPool.getConnection();
165-
// Explicitly set timeouts on this connection (sessionVariables may not apply reliably)
166-
await newDbConnection.query(
167-
"SET SESSION net_read_timeout = 1800; SET SESSION net_write_timeout = 1800; SET SESSION wait_timeout = 3600",
168-
);
197+
await newDbConnection.query("SET SESSION net_read_timeout = 1800");
198+
await newDbConnection.query("SET SESSION net_write_timeout = 1800");
199+
await newDbConnection.query("SET SESSION wait_timeout = 3600");
169200
for (const statement of cleanSqlStatements) {
170201
console.log(`Executing statement: ${statement}`);
171202
await newDbConnection.query(statement);
172203
console.log(" done.");
173204
}
174205

206+
console.log(
207+
`Listing tables in ${process.env.MYSQL_DATABASE_NEW} (information_schema)...`,
208+
);
175209
const tables = (
176210
await newDbConnection.query(
177211
`SELECT table_name FROM information_schema.tables WHERE table_schema = ? ORDER BY table_name`,
178212
[process.env.MYSQL_DATABASE_NEW],
179213
)
180214
).map((row: { table_name: string }) => row.table_name);
215+
console.log(`Found ${tables.length} tables to rename.`);
181216
await newDbConnection.release();
182217

183-
// Get a fresh connection BEFORE releasing the original - otherwise the pool may
184-
// return the same stale connection that was idle for 30+ min during the SQL phase
218+
console.log("Acquiring connection for rename phase...");
185219
const renameConnection = await pool.getConnection();
186-
await connection.release();
187-
await renameConnection.query(
188-
"SET SESSION net_read_timeout = 7200; SET SESSION net_write_timeout = 7200; SET SESSION wait_timeout = 28800",
189-
);
220+
await renameConnection.query("SET SESSION net_read_timeout = 7200");
221+
await renameConnection.query("SET SESSION net_write_timeout = 7200");
222+
await renameConnection.query("SET SESSION wait_timeout = 28800");
190223

191224
for (const table of tables) {
192225
console.log(`Renaming ${table}...`);
226+
await renameConnection.query("SET foreign_key_checks = 0");
193227
await renameConnection.query(
194-
`set foreign_key_checks = 0;
195-
drop table if exists ${process.env.MYSQL_DATABASE}.${table};
196-
rename table ${process.env.MYSQL_DATABASE_NEW}.${table} to ${process.env.MYSQL_DATABASE}.${table};
197-
set foreign_key_checks = 1;`,
228+
`DROP TABLE IF EXISTS ${process.env.MYSQL_DATABASE}.${table}`,
198229
);
230+
await renameConnection.query(
231+
`RENAME TABLE ${process.env.MYSQL_DATABASE_NEW}.${table} TO ${process.env.MYSQL_DATABASE}.${table}`,
232+
);
233+
await renameConnection.query("SET foreign_key_checks = 1");
199234
console.log(" done.");
200235
}
201236

202237
await renameConnection.query(`drop database ${process.env.MYSQL_DATABASE_NEW}`);
203238
await renameConnection.release();
204239

205240
console.log("mariadb-check...");
206-
await $`mariadb-check -h ${process.env.MYSQL_HOST} -uroot -p${process.env.MYSQL_ROOT_PASSWORD} -v ${process.env.MYSQL_DATABASE}`;
241+
const checkExit = await runMariadbCheck();
242+
if (checkExit !== 0) {
243+
throw new Error(`mariadb-check exited with code ${checkExit}`);
244+
}
207245
console.log(" done.");
208246
await pool.end();
209247
await newDbPool.end();

0 commit comments

Comments
 (0)