Continue Phase 2 Batch 2 and Batch 3

Migrate 6 background jobs to use wrapEdgeFunction: cleanup-old-versions, process-scheduled-deletions, data-retention-cleanup, run-cleanup-jobs, scheduled-maintenance, process-expired-bans. Replace old server routines with edgeFunction wrapper, add centralized logging, tracing, and standardized error handling, and adjust for batch-wise deployment.
This commit is contained in:
gpt-engineer-app[bot]
2025-11-11 03:36:40 +00:00
parent 12d2518eb9
commit 16a1fa756d
6 changed files with 210 additions and 308 deletions

View File

@@ -11,7 +11,7 @@
*/
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
import { corsHeaders } from '../_shared/cors.ts';
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
import { edgeLogger } from '../_shared/logger.ts';
interface CleanupResult {
@@ -50,28 +50,16 @@ interface CleanupResult {
};
}
Deno.serve(async (req) => {
// Handle CORS preflight
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
const startTime = Date.now();
try {
export default createEdgeFunction(
{
name: 'run-cleanup-jobs',
requireAuth: false,
},
async (req, context, supabase) => {
const startTime = Date.now();
edgeLogger.info('Starting automated cleanup jobs', {
timestamp: new Date().toISOString(),
});
// Create Supabase client with service role
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
const supabase = createClient(supabaseUrl, supabaseServiceKey, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
requestId: context.requestId,
});
// Execute the master cleanup function
@@ -82,19 +70,9 @@ Deno.serve(async (req) => {
error: error.message,
code: error.code,
duration_ms: Date.now() - startTime,
requestId: context.requestId,
});
return new Response(
JSON.stringify({
success: false,
error: error.message,
duration_ms: Date.now() - startTime,
}),
{
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
}
);
throw error;
}
const result = data as CleanupResult;
@@ -106,6 +84,7 @@ Deno.serve(async (req) => {
locks_released: result.locks?.released || 0,
submissions_deleted: result.old_submissions?.deleted || 0,
duration_ms: result.execution.duration_ms,
requestId: context.requestId,
});
// Log any individual task failures
@@ -136,27 +115,7 @@ Deno.serve(async (req) => {
results: result,
total_duration_ms: Date.now() - startTime,
}),
{
status: 200,
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
}
);
} catch (error) {
edgeLogger.error('Unexpected error in cleanup jobs', {
error: error instanceof Error ? error.message : 'Unknown error',
duration_ms: Date.now() - startTime,
});
return new Response(
JSON.stringify({
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
duration_ms: Date.now() - startTime,
}),
{
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
}
{ headers: { 'Content-Type': 'application/json' } }
);
}
});
);