Skip to content

Commit 5f72f64

Browse files
Add function to kill long-running sleeping MySQL processes hourly (#295)
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent 440e37c commit 5f72f64

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/controller/GroundController.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,47 @@ const purgeIgnoredAddressesSubscriptions = () => {
133133
.catch((error) => console.log("error purging addresses subscriptions:", error));
134134
};
135135

136+
const killSleepingMySQLProcesses = () => {
137+
console.log("Checking for sleeping MySQL processes...");
138+
139+
// Query to find processes sleeping for more than 3600 seconds
140+
const query = `
141+
SELECT id, user, host, db, command, time, state, info
142+
FROM information_schema.processlist
143+
WHERE command = 'Sleep' AND time > 3600 AND id != CONNECTION_ID()
144+
`;
145+
146+
connection.query(query)
147+
.then((sleepingProcesses: any[]) => {
148+
if (sleepingProcesses.length > 0) {
149+
console.log(`Found ${sleepingProcesses.length} sleeping processes older than 1 hour`);
150+
151+
// Kill each sleeping process
152+
const killPromises = sleepingProcesses.map((process) => {
153+
console.log(`Killing process ID ${process.id} (user: ${process.user}, host: ${process.host}, sleeping for ${process.time}s)`);
154+
return connection.query(`KILL ${process.id}`)
155+
.then(() => console.log(`Successfully killed process ${process.id}`))
156+
.catch((error) => console.log(`Error killing process ${process.id}:`, error.message));
157+
});
158+
159+
return Promise.all(killPromises);
160+
} else {
161+
console.log("No sleeping processes found that are older than 1 hour");
162+
}
163+
})
164+
.catch((error) => {
165+
console.log("Error checking sleeping processes:", error.message);
166+
});
167+
};
168+
136169
dataSource.initialize().then((c) => {
137170
console.log("db connected");
138171
connection = c;
139172
purgeIgnoredAddressesSubscriptions();
140173
pushLogPurge();
141174
purgeOldTxidSubscriptions();
142175
setInterval(pushLogPurge, 3600 * 1000);
176+
setInterval(killSleepingMySQLProcesses, 3600 * 1000); // Run every hour
143177
});
144178

145179
export class GroundController {

0 commit comments

Comments
 (0)