Skip to content

Commit

Permalink
Log user if user header is set and fix for indefinite connections
Browse files Browse the repository at this point in the history
  • Loading branch information
LauJosefsen committed Apr 11, 2024
1 parent ead3bf2 commit b77af30
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified bun.lockb
Binary file not shown.
13 changes: 0 additions & 13 deletions src/pages/api/hello.ts

This file was deleted.

18 changes: 12 additions & 6 deletions src/pages/api/kill.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'])
Expand Down
32 changes: 20 additions & 12 deletions src/pages/instance/[identifier]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<RowDataPacket[]>(
'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<RowDataPacket[]>(
'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) => {
Expand All @@ -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) {
Expand Down

0 comments on commit b77af30

Please sign in to comment.