diff --git a/.env b/.env index 6c40fb1..a50cac9 100644 --- a/.env +++ b/.env @@ -7,3 +7,5 @@ CEGO_TEST_DB_HOST=127.0.0.1 CEGO_TEST_DB_PORT=3306 CEGO_TEST_DB_USER=root CEGO_TEST_DB_DATABASE=testdb + +USER_HEADER=remote-user diff --git a/bun.lockb b/bun.lockb index 06c8142..ac544f5 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/pages/api/hello.ts b/src/pages/api/hello.ts deleted file mode 100644 index 96f00a0..0000000 --- a/src/pages/api/hello.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next' - -type Data = { - name: string -} - -export default function handler( - req: NextApiRequest, - res: NextApiResponse -) { - res.status(200).json({ name: 'John Doe' }) -} diff --git a/src/pages/api/kill.ts b/src/pages/api/kill.ts index 6c235a0..3abc550 100644 --- a/src/pages/api/kill.ts +++ b/src/pages/api/kill.ts @@ -1,5 +1,3 @@ -// Given the id in post body, kill the mysql process with that id - import { NextApiRequest, NextApiResponse } from 'next' import { getConfig } from '@/lib/config' import mysql from 'mysql2/promise' @@ -25,10 +23,18 @@ export default async function handler( } // Kill the mysql process with the id - // Use the id to kill the mysql process - // res.status(200).json({ name: 'John Doe' }); - const conn = await mysql.createConnection(dbConfig) - await conn.query(`KILL ?`, [id]) + let conn: mysql.Connection | null = null + try { + conn = await mysql.createConnection(dbConfig) + await conn.query(`KILL ?`, [id]) + if(process.env.USER_HEADER) { + const user = req.headers[process.env.USER_HEADER]; + console.log(`User ${user} killed process ${id} on instance ${instance}`) + } + } + finally { + conn?.end() + } res.status(200).json({ id }) } else { res.setHeader('Allow', ['POST']) diff --git a/src/pages/instance/[identifier]/index.tsx b/src/pages/instance/[identifier]/index.tsx index f091cb7..af5f2bf 100644 --- a/src/pages/instance/[identifier]/index.tsx +++ b/src/pages/instance/[identifier]/index.tsx @@ -115,18 +115,26 @@ export const getServerSideProps = (async (context) => { } } - // Fetch data from external API - const conn = await mysql.createConnection(instance) + let conn: mysql.Connection | null = null - const [processListResult] = await conn.query('SHOW PROCESSLIST;') - const processList: Process[] = processListResult as Process[] - const [innoDbStatusResult] = await conn.query( - 'SHOW ENGINE INNODB STATUS;' - ) + let processList: Process[] = [] + let innoDbStatusString = ''; + + try { + conn = await mysql.createConnection(instance) + const [processListResult] = await conn.query('SHOW PROCESSLIST;') + processList = processListResult as Process[] + const [innoDbStatusResult] = await conn.query( + 'SHOW ENGINE INNODB STATUS;' + ) + + const innoDbStatusString = innoDbStatusResult[0]['Status'] as string + } + finally { + conn?.end() + } - const innoDbStatusString = innoDbStatusResult[0]['Status'] as string - // Convert the status string - const innoDbStatus = parseInnoDbStatus(innoDbStatusString as string) + const innoDbStatus = parseInnoDbStatus(innoDbStatusString); const processListWithTransaction: ProcessWithTransaction[] = processList.map((process) => { @@ -139,10 +147,10 @@ export const getServerSideProps = (async (context) => { // Order by transaction.activeTime desc, then by process.Time desc processListWithTransaction.sort((a, b) => { - if (a.transaction && !b.transaction) { + if (a.transaction && !b.transaction && a.transaction.activeTime > 10) { return -1 } - if (!a.transaction && b.transaction) { + if (!a.transaction && b.transaction && b.transaction.activeTime > 10) { return 1 } if (a.transaction && b.transaction) {