EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
cleanup_old_agents.js
Go to the documentation of this file.
1#!/usr/bin/env node
2// Script to clean up old/test agents from the database
3const pool = require('../services/db');
4
5/**
6 *
7 */
8async function cleanupOldAgents() {
9 console.log('[Cleanup] Starting agent cleanup...');
10
11 try {
12 // Find agents older than 30 days with no recent activity
13 const oldAgentsQuery = `
14 SELECT agent_id, agent_uuid, hostname, last_seen, created_at
15 FROM agents
16 WHERE last_seen < NOW() - INTERVAL '30 days'
17 OR (last_seen IS NULL AND created_at < NOW() - INTERVAL '7 days')
18 ORDER BY last_seen DESC NULLS LAST
19 `;
20
21 const result = await pool.query(oldAgentsQuery);
22 console.log(`[Cleanup] Found ${result.rows.length} old agents`);
23
24 if (result.rows.length === 0) {
25 console.log('[Cleanup] No old agents to clean up');
26 return;
27 }
28
29 // Display agents
30 console.log('\nOld agents:');
31 result.rows.forEach((agent, idx) => {
32 console.log(`${idx + 1}. ${agent.hostname} (${agent.agent_uuid})`);
33 console.log(` Last seen: ${agent.last_seen || 'Never'}`);
34 console.log(` Created: ${agent.created_at}`);
35 });
36
37 // Confirm deletion
38 const readline = require('readline').createInterface({
39 input: process.stdin,
40 output: process.stdout
41 });
42
43 readline.question('\nDelete these agents? (yes/no): ', async (answer) => {
44 if (answer.toLowerCase() === 'yes' || answer.toLowerCase() === 'y') {
45 // Delete agents
46 for (const agent of result.rows) {
47 try {
48 await pool.query('DELETE FROM agents WHERE agent_id = $1', [agent.agent_id]);
49 console.log(`✓ Deleted ${agent.hostname} (${agent.agent_uuid})`);
50 } catch (err) {
51 console.error(`✗ Failed to delete ${agent.hostname}:`, err.message);
52 }
53 }
54 console.log('\n[Cleanup] Agent cleanup complete');
55 } else {
56 console.log('[Cleanup] Cancelled');
57 }
58
59 readline.close();
60 process.exit(0);
61 });
62
63 } catch (err) {
64 console.error('[Cleanup] Error:', err);
65 process.exit(1);
66 }
67}
68
69cleanupOldAgents();