Skip to content

Commit 2f64944

Browse files
waleedlatif1claude
andcommitted
refactor(polling): consolidate polling services into provider handler pattern
Eliminate self-POST anti-pattern and extract shared boilerplate from 4 polling services into a clean handler registry mirroring lib/webhooks/providers/. - Add processPolledWebhookEvent() to processor.ts for direct in-process webhook execution, removing HTTP round-trips that caused Lambda 403/timeout errors - Extract shared utilities (markWebhookFailed/Success, fetchActiveWebhooks, runWithConcurrency, resolveOAuthCredential, updateWebhookProviderConfig) - Create PollingProviderHandler interface with per-provider implementations - Consolidate 4 identical route files into single dynamic [provider] route - Standardize concurrency to 10 across all providers - No infra changes needed — Helm cron paths resolve via dynamic route Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 712e58a commit 2f64944

File tree

16 files changed

+1697
-2098
lines changed

16 files changed

+1697
-2098
lines changed

apps/sim/app/api/webhooks/poll/rss/route.ts renamed to apps/sim/app/api/webhooks/poll/[provider]/route.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,33 @@ import { type NextRequest, NextResponse } from 'next/server'
33
import { verifyCronAuth } from '@/lib/auth/internal'
44
import { acquireLock, releaseLock } from '@/lib/core/config/redis'
55
import { generateShortId } from '@/lib/core/utils/uuid'
6-
import { pollRssWebhooks } from '@/lib/webhooks/rss-polling-service'
6+
import { pollProvider, VALID_POLLING_PROVIDERS } from '@/lib/webhooks/polling'
77

8-
const logger = createLogger('RssPollingAPI')
8+
const logger = createLogger('PollingAPI')
99

1010
export const dynamic = 'force-dynamic'
11-
export const maxDuration = 180 // Allow up to 3 minutes for polling to complete
11+
export const maxDuration = 180
1212

13-
const LOCK_KEY = 'rss-polling-lock'
14-
const LOCK_TTL_SECONDS = 180 // Same as maxDuration (3 min)
15-
16-
export async function GET(request: NextRequest) {
13+
export async function GET(
14+
request: NextRequest,
15+
{ params }: { params: Promise<{ provider: string }> }
16+
) {
17+
const { provider } = await params
1718
const requestId = generateShortId()
18-
logger.info(`RSS webhook polling triggered (${requestId})`)
1919

20+
if (!VALID_POLLING_PROVIDERS.has(provider)) {
21+
return NextResponse.json({ error: `Unknown polling provider: ${provider}` }, { status: 404 })
22+
}
23+
24+
const LOCK_KEY = `${provider}-polling-lock`
2025
let lockValue: string | undefined
2126

2227
try {
23-
const authError = verifyCronAuth(request, 'RSS webhook polling')
24-
if (authError) {
25-
return authError
26-
}
28+
const authError = verifyCronAuth(request, `${provider} webhook polling`)
29+
if (authError) return authError
2730

2831
lockValue = requestId
29-
const locked = await acquireLock(LOCK_KEY, lockValue, LOCK_TTL_SECONDS)
30-
32+
const locked = await acquireLock(LOCK_KEY, lockValue, 180)
3133
if (!locked) {
3234
return NextResponse.json(
3335
{
@@ -40,21 +42,21 @@ export async function GET(request: NextRequest) {
4042
)
4143
}
4244

43-
const results = await pollRssWebhooks()
45+
const results = await pollProvider(provider)
4446

4547
return NextResponse.json({
4648
success: true,
47-
message: 'RSS polling completed',
49+
message: `${provider} polling completed`,
4850
requestId,
4951
status: 'completed',
5052
...results,
5153
})
5254
} catch (error) {
53-
logger.error(`Error during RSS polling (${requestId}):`, error)
55+
logger.error(`Error during ${provider} polling (${requestId}):`, error)
5456
return NextResponse.json(
5557
{
5658
success: false,
57-
message: 'RSS polling failed',
59+
message: `${provider} polling failed`,
5860
error: error instanceof Error ? error.message : 'Unknown error',
5961
requestId,
6062
},

apps/sim/app/api/webhooks/poll/gmail/route.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

apps/sim/app/api/webhooks/poll/imap/route.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

apps/sim/app/api/webhooks/poll/outlook/route.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)