mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 11:11:12 -05:00
feat: Implement retry logic and tracking
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
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, startRequest, endRequest } from '../_shared/logger.ts';
|
||||
import { withEdgeRetry } from '../_shared/retryHelper.ts';
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
@@ -186,15 +187,30 @@ serve(async (req) => {
|
||||
isEscalated: is_escalated,
|
||||
};
|
||||
|
||||
// Send ONE notification to the moderation-submissions topic
|
||||
// Send ONE notification to the moderation-submissions topic with retry
|
||||
// All subscribers (moderators) will receive it automatically
|
||||
const { data, error } = await supabase.functions.invoke('trigger-notification', {
|
||||
body: {
|
||||
workflowId: workflow.workflow_id,
|
||||
topicKey: 'moderation-submissions',
|
||||
payload: notificationPayload,
|
||||
const data = await withEdgeRetry(
|
||||
async () => {
|
||||
const { data: result, error } = await supabase.functions.invoke('trigger-notification', {
|
||||
body: {
|
||||
workflowId: workflow.workflow_id,
|
||||
topicKey: 'moderation-submissions',
|
||||
payload: notificationPayload,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
const enhancedError = new Error(error.message || 'Notification trigger failed');
|
||||
(enhancedError as any).status = error.status;
|
||||
throw enhancedError;
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
});
|
||||
{ maxAttempts: 3, baseDelay: 1000 },
|
||||
tracking.requestId,
|
||||
'trigger-submission-notification'
|
||||
);
|
||||
|
||||
// Log notification in notification_logs with idempotency key
|
||||
await supabase.from('notification_logs').insert({
|
||||
@@ -209,32 +225,6 @@ serve(async (req) => {
|
||||
}
|
||||
});
|
||||
|
||||
if (error) {
|
||||
const duration = endRequest(tracking);
|
||||
edgeLogger.error('Failed to notify moderators via topic', {
|
||||
action: 'notify_moderators',
|
||||
requestId: tracking.requestId,
|
||||
duration,
|
||||
error: error.message
|
||||
});
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
success: false,
|
||||
error: 'Failed to send notification to topic',
|
||||
details: error.message,
|
||||
requestId: tracking.requestId
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
...corsHeaders,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Request-ID': tracking.requestId
|
||||
},
|
||||
status: 500,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const duration = endRequest(tracking);
|
||||
edgeLogger.info('Successfully notified all moderators via topic', {
|
||||
action: 'notify_moderators',
|
||||
|
||||
Reference in New Issue
Block a user