mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 12:31:13 -05:00
Implement backfill retry logic with CORS
Add retry mechanism to backfill-ride-data, backfill-company-data, and backfill-park-locations edge functions using withEdgeRetry, including isRetryableError and isDeadlockError. Apply CORS headers to ride-data and company-data responses, and ensure park-locations already contains CORS remains with retry wrapping. Update unauthorized and success responses to include CORS headers.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
import { withEdgeRetry, isRetryableError, isDeadlockError } from '../_shared/retryHelper.ts';
|
||||
import { corsHeadersWithMethods } from '../_shared/cors.ts';
|
||||
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
name: 'backfill-company-data',
|
||||
requireAuth: true,
|
||||
corsHeaders: corsHeadersWithMethods,
|
||||
},
|
||||
async (req, context, supabase) => {
|
||||
edgeLogger.info('Starting company data backfill', { requestId: context.requestId });
|
||||
@@ -23,20 +26,28 @@ export default createEdgeFunction(
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'Unauthorized: Superuser access required' }),
|
||||
{ status: 403, headers: { 'Content-Type': 'application/json' } }
|
||||
{ status: 403, headers: { ...corsHeadersWithMethods, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
|
||||
// Execute the backfill function
|
||||
const { data, error } = await supabase.rpc('backfill_company_data');
|
||||
|
||||
if (error) {
|
||||
edgeLogger.error('Error running company data backfill', {
|
||||
error,
|
||||
requestId: context.requestId
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
// Execute the backfill function with retry logic
|
||||
const { data, error } = await withEdgeRetry(
|
||||
async () => {
|
||||
const result = await supabase.rpc('backfill_company_data');
|
||||
if (result.error) throw result.error;
|
||||
return result;
|
||||
},
|
||||
{
|
||||
maxAttempts: 5,
|
||||
baseDelay: 2000,
|
||||
maxDelay: 30000,
|
||||
backoffMultiplier: 2,
|
||||
jitter: true,
|
||||
shouldRetry: (error) => isRetryableError(error) || isDeadlockError(error)
|
||||
},
|
||||
context.requestId,
|
||||
'backfill_company_data'
|
||||
);
|
||||
|
||||
edgeLogger.info('Company data backfill completed', {
|
||||
results: data,
|
||||
@@ -48,7 +59,7 @@ export default createEdgeFunction(
|
||||
success: true,
|
||||
...data,
|
||||
}),
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
{ headers: { ...corsHeadersWithMethods, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
import { corsHeadersWithMethods } from '../_shared/cors.ts';
|
||||
import { withEdgeRetry, isRetryableError, isDeadlockError } from '../_shared/retryHelper.ts';
|
||||
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
@@ -29,16 +30,24 @@ export default createEdgeFunction(
|
||||
);
|
||||
}
|
||||
|
||||
// Execute the backfill function
|
||||
const { data, error } = await supabase.rpc('backfill_park_locations');
|
||||
|
||||
if (error) {
|
||||
edgeLogger.error('Error running park location backfill', {
|
||||
error,
|
||||
requestId: context.requestId
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
// Execute the backfill function with retry logic
|
||||
const { data, error } = await withEdgeRetry(
|
||||
async () => {
|
||||
const result = await supabase.rpc('backfill_park_locations');
|
||||
if (result.error) throw result.error;
|
||||
return result;
|
||||
},
|
||||
{
|
||||
maxAttempts: 5,
|
||||
baseDelay: 2000,
|
||||
maxDelay: 30000,
|
||||
backoffMultiplier: 2,
|
||||
jitter: true,
|
||||
shouldRetry: (error) => isRetryableError(error) || isDeadlockError(error)
|
||||
},
|
||||
context.requestId,
|
||||
'backfill_park_locations'
|
||||
);
|
||||
|
||||
edgeLogger.info('Park location backfill completed', {
|
||||
results: data,
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
||||
import { edgeLogger } from '../_shared/logger.ts';
|
||||
import { withEdgeRetry, isRetryableError, isDeadlockError } from '../_shared/retryHelper.ts';
|
||||
import { corsHeadersWithMethods } from '../_shared/cors.ts';
|
||||
|
||||
export default createEdgeFunction(
|
||||
{
|
||||
name: 'backfill-ride-data',
|
||||
requireAuth: true,
|
||||
corsHeaders: corsHeadersWithMethods,
|
||||
},
|
||||
async (req, context, supabase) => {
|
||||
edgeLogger.info('Starting ride data backfill', { requestId: context.requestId });
|
||||
@@ -23,20 +26,28 @@ export default createEdgeFunction(
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'Unauthorized: Superuser access required' }),
|
||||
{ status: 403, headers: { 'Content-Type': 'application/json' } }
|
||||
{ status: 403, headers: { ...corsHeadersWithMethods, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
|
||||
// Execute the backfill function
|
||||
const { data, error } = await supabase.rpc('backfill_ride_data');
|
||||
|
||||
if (error) {
|
||||
edgeLogger.error('Error running ride data backfill', {
|
||||
error,
|
||||
requestId: context.requestId
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
// Execute the backfill function with retry logic
|
||||
const { data, error } = await withEdgeRetry(
|
||||
async () => {
|
||||
const result = await supabase.rpc('backfill_ride_data');
|
||||
if (result.error) throw result.error;
|
||||
return result;
|
||||
},
|
||||
{
|
||||
maxAttempts: 5,
|
||||
baseDelay: 2000,
|
||||
maxDelay: 30000,
|
||||
backoffMultiplier: 2,
|
||||
jitter: true,
|
||||
shouldRetry: (error) => isRetryableError(error) || isDeadlockError(error)
|
||||
},
|
||||
context.requestId,
|
||||
'backfill_ride_data'
|
||||
);
|
||||
|
||||
edgeLogger.info('Ride data backfill completed', {
|
||||
results: data,
|
||||
@@ -48,7 +59,7 @@ export default createEdgeFunction(
|
||||
success: true,
|
||||
...data,
|
||||
}),
|
||||
{ headers: { 'Content-Type': 'application/json' } }
|
||||
{ headers: { ...corsHeadersWithMethods, 'Content-Type': 'application/json' } }
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user