-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.js
More file actions
200 lines (169 loc) · 8.19 KB
/
Copy pathtest-api.js
File metadata and controls
200 lines (169 loc) · 8.19 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// queue-check.js - Detailed queue diagnostic
import dotenv from 'dotenv';
import { createClient } from '@supabase/supabase-js';
import Redis from 'ioredis';
// Load environment variables
dotenv.config({ path: '.env.local' });
dotenv.config();
const SUPABASE_URL = process.env.SUPABASE_URL || process.env.NEXT_PUBLIC_SUPABASE_URL;
const SUPABASE_SERVICE_KEY = process.env.SUPABASE_SERVICE_KEY ||
process.env.SUPABASE_SERVICE_ROLE_KEY;
const REDIS_URL = process.env.REDIS_URL;
const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_KEY);
const redis = new Redis(REDIS_URL);
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('🔍 VIDEO QUEUE DIAGNOSTIC');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
async function checkQueue() {
try {
// 1. Check queue length
console.log('📊 QUEUE STATUS');
console.log('─────────────────────────────────────────────────────');
const queueLength = await redis.llen('video_queue');
console.log(`Queue length: ${queueLength}`);
// 2. Get all items in queue
if (queueLength > 0) {
console.log('\n📋 ITEMS IN QUEUE:');
const items = await redis.lrange('video_queue', 0, -1);
items.forEach((item, index) => {
try {
const parsed = JSON.parse(item);
const age = parsed.timestamp ? Math.floor((Date.now() - parsed.timestamp) / 1000) : 'unknown';
console.log(`\n [${index}] ${parsed.questionTitle || 'Untitled'}`);
console.log(` Hash: ${parsed.questionHash?.substring(0, 12)}...`);
console.log(` Age: ${age}s`);
console.log(` Difficulty: ${parsed.difficulty}`);
} catch (e) {
console.log(` [${index}] Invalid JSON: ${item.substring(0, 50)}...`);
}
});
} else {
console.log(' ℹ️ Queue is empty');
}
// 3. Check database for pending videos
console.log('\n\n🗄️ DATABASE STATUS');
console.log('─────────────────────────────────────────────────────');
const { data: pending } = await supabase
.from('solutions')
.select('question_hash, question_title, video_status, created_at, updated_at')
.eq('video_status', 'pending')
.order('created_at', { ascending: false });
console.log(`Pending videos in DB: ${pending?.length || 0}`);
if (pending && pending.length > 0) {
console.log('\n Pending videos:');
pending.forEach((sol, i) => {
const age = Math.floor((Date.now() - new Date(sol.created_at).getTime()) / 1000);
console.log(` ${i + 1}. ${sol.question_title}`);
console.log(` Hash: ${sol.question_hash.substring(0, 12)}...`);
console.log(` Age: ${age}s`);
});
}
const { data: generating } = await supabase
.from('solutions')
.select('question_hash, question_title, video_status, updated_at')
.eq('video_status', 'generating');
console.log(`\nGenerating videos in DB: ${generating?.length || 0}`);
if (generating && generating.length > 0) {
console.log('\n Generating videos:');
generating.forEach((sol, i) => {
const age = Math.floor((Date.now() - new Date(sol.updated_at).getTime()) / 1000);
console.log(` ${i + 1}. ${sol.question_title}`);
console.log(` Hash: ${sol.question_hash.substring(0, 12)}...`);
console.log(` Age: ${age}s`);
});
}
const { data: completed } = await supabase
.from('solutions')
.select('question_hash, question_title, video_status, video_url')
.eq('video_status', 'completed');
console.log(`\nCompleted videos in DB: ${completed?.length || 0}`);
if (completed && completed.length > 0) {
console.log('\n Completed videos:');
completed.slice(0, 3).forEach((sol, i) => {
console.log(` ${i + 1}. ${sol.question_title}`);
console.log(` Has URL: ${!!sol.video_url}`);
console.log(` URL: ${sol.video_url?.substring(0, 50)}...`);
});
}
const { data: failed } = await supabase
.from('solutions')
.select('question_hash, question_title, video_status')
.eq('video_status', 'failed');
console.log(`\nFailed videos in DB: ${failed?.length || 0}`);
// 4. Test queue operations
console.log('\n\n🧪 QUEUE OPERATION TEST');
console.log('─────────────────────────────────────────────────────');
const testJob = {
questionHash: `diagnostic_test_${Date.now()}`,
questionTitle: 'Diagnostic Test',
timestamp: Date.now()
};
console.log('Adding test job to queue...');
await redis.lpush('video_queue', JSON.stringify(testJob));
// Wait a bit for serverless Redis
await new Promise(resolve => setTimeout(resolve, 200));
const lengthAfterPush = await redis.llen('video_queue');
console.log(`Queue length after push: ${lengthAfterPush}`);
const peeked = await redis.lindex('video_queue', 0);
if (peeked) {
const parsed = JSON.parse(peeked);
console.log(`First item in queue: ${parsed.questionTitle}`);
}
console.log('Removing test job...');
const popped = await redis.lpop('video_queue');
if (popped) {
const parsed = JSON.parse(popped);
console.log(`✅ Successfully popped: ${parsed.questionTitle}`);
} else {
console.log('❌ Failed to pop job (queue might be using RPUSH/LPOP pattern)');
// Try the other end
const rpoppedTest = await redis.rpop('video_queue');
if (rpoppedTest) {
console.log(' Found it on the right side (using RPOP)');
}
}
const finalLength = await redis.llen('video_queue');
console.log(`Final queue length: ${finalLength}`);
// 5. Check for orphaned jobs (in queue but not in DB)
console.log('\n\n🔍 ORPHAN CHECK');
console.log('─────────────────────────────────────────────────────');
if (queueLength > 0) {
const queueItems = await redis.lrange('video_queue', 0, -1);
const queueHashes = queueItems.map(item => {
try {
return JSON.parse(item).questionHash;
} catch {
return null;
}
}).filter(Boolean);
console.log(`Checking ${queueHashes.length} jobs in queue against DB...`);
for (const hash of queueHashes) {
const { data } = await supabase
.from('solutions')
.select('question_title, video_status')
.eq('question_hash', hash)
.single();
if (!data) {
console.log(` ⚠️ Orphan found: ${hash.substring(0, 12)}... (not in DB)`);
} else if (data.video_status === 'completed') {
console.log(` ⚠️ Stale job: ${data.question_title} (already completed)`);
} else if (data.video_status === 'failed') {
console.log(` ⚠️ Stale job: ${data.question_title} (marked as failed)`);
} else {
console.log(` ✅ Valid: ${data.question_title} (status: ${data.video_status})`);
}
}
} else {
console.log('Queue is empty - no orphan check needed');
}
console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('✅ DIAGNOSTIC COMPLETE');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
} catch (error) {
console.error('❌ Error during diagnostic:', error);
} finally {
await redis.quit();
process.exit(0);
}
}
checkQueue();