File size: 3,745 Bytes
c7fbc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

import { createClient } from '@supabase/supabase-js';
import { Redis } from '@upstash/redis';
import amqplib from 'amqplib';
import dotenv from 'dotenv';
import path from 'path';

// Load environment variables from .env.local
dotenv.config({ path: path.resolve(process.cwd(), '.env.local') });

async function checkSupabase() {
    console.log('--- Checking Supabase ---');
    if (!process.env.NEXT_PUBLIC_SUPABASE_URL || !process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY) {
        console.error('❌ Missing Supabase credentials');
        return;
    }

    try {
        const supabase = createClient(
            process.env.NEXT_PUBLIC_SUPABASE_URL,
            process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
        );
        const { data, error } = await supabase.from('User').select('count', { count: 'exact', head: true });

        if (error) {
            console.error('❌ Supabase Connection Failed:', error.message);
        } else {
            console.log('βœ… Supabase Connected');
        }
    } catch (e: any) {
        console.error('❌ Supabase Connection Error:', e.message);
    }
}

async function checkUpstash() {
    console.log('\n--- Checking QStash ---');
    if (process.env.QSTASH_TOKEN) {
        console.log('βœ… QStash Token found');
        // Verify token validity by listing messages or similar if possible, 
        // but for now existence is a strong indicator.
        // We can try to instantiate the client.
        try {
            // Basic import check was done at top.
            // Real check:
            const { Client } = await import("@upstash/qstash");
            const client = new Client({ token: process.env.QSTASH_TOKEN });
            // There isn't a simple "ping" in QStash client, but client creation is a start.
            console.log('βœ… QStash Client initialized');
        } catch (e: any) {
            console.error('❌ QStash Client Init Failed:', e.message);
        }

    } else {
        console.warn('⚠️ QStash Token (QSTASH_TOKEN) not found');
    }

    console.log('\n--- Checking Upstash Redis (Optional) ---');
    if (!process.env.UPSTASH_REDIS_REST_URL || !process.env.UPSTASH_REDIS_REST_TOKEN) {
        console.log('ℹ️ Upstash Redis credentials missing (Skipping)');
        return;
    }

    try {
        const redis = new Redis({
            url: process.env.UPSTASH_REDIS_REST_URL,
            token: process.env.UPSTASH_REDIS_REST_TOKEN,
        });

        await redis.set('test_connection', 'ok');
        const val = await redis.get('test_connection');

        if (val === 'ok') {
            console.log('βœ… Upstash Redis Connected');
            await redis.del('test_connection');
        } else {
            console.error('❌ Upstash Redis Failed: Unexpected value');
        }
    } catch (e: any) {
        console.error('❌ Upstash Redis Connection Error:', e.message);
    }
}

async function checkLavinMQ() {
    console.log('\n--- Checking LavinMQ ---');
    const url = process.env.LAVINMQ_URL;
    if (!url) {
        console.warn('⚠️ LAVINMQ_URL not found in environment. Checking default localhost...');
    }

    const connectionUrl = url || 'amqp://guest:guest@localhost:5672';

    try {
        const conn = await amqplib.connect(connectionUrl);
        const channel = await conn.createChannel();
        console.log(`βœ… LavinMQ Connected to ${connectionUrl}`);
        await channel.close();
        await conn.close();
    } catch (e: any) {
        console.error('❌ LavinMQ Connection Failed:', e.message);
    }
}

async function main() {
    console.log('Starting Connection Checks...\n');
    await checkSupabase();
    await checkUpstash();
    await checkLavinMQ();
    console.log('\nDone.');
}

main();