Files
thrilltrack-explorer/supabase/functions/scheduled-maintenance/index.ts
gpt-engineer-app[bot] d903e96e13 Implement pipeline monitoring alerts
Approve and implement the Supabase migration for the pipeline monitoring alert system. This includes expanding alert types, adding new monitoring functions, and updating existing ones with escalating thresholds.
2025-11-07 05:05:32 +00:00

74 lines
2.2 KiB
TypeScript

import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
import { edgeLogger } from '../_shared/logger.ts';
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
};
serve(async (req: Request) => {
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
const requestId = crypto.randomUUID();
try {
edgeLogger.info('Starting scheduled maintenance', { requestId });
const supabase = createClient(
Deno.env.get('SUPABASE_URL')!,
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
);
// Run system maintenance (orphaned image cleanup)
const { data: maintenanceData, error: maintenanceError } = await supabase.rpc('run_system_maintenance');
if (maintenanceError) {
edgeLogger.error('Maintenance failed', { requestId, error: maintenanceError.message });
} else {
edgeLogger.info('Maintenance completed', { requestId, result: maintenanceData });
}
// Run pipeline monitoring checks
const { data: monitoringData, error: monitoringError } = await supabase.rpc('run_pipeline_monitoring');
if (monitoringError) {
edgeLogger.error('Pipeline monitoring failed', { requestId, error: monitoringError.message });
} else {
edgeLogger.info('Pipeline monitoring completed', { requestId, result: monitoringData });
}
return new Response(
JSON.stringify({
success: true,
maintenance: maintenanceData,
monitoring: monitoringData,
requestId
}),
{
status: 200,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
}
);
} catch (error) {
edgeLogger.error('Maintenance exception', {
requestId,
error: error instanceof Error ? error.message : String(error)
});
return new Response(
JSON.stringify({
success: false,
error: 'Internal server error',
requestId
}),
{
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
}
);
}
});