Skip to content

Commit acb142c

Browse files
committed
chore: merge main into release for new releases
2 parents 50dff74 + 82ccec8 commit acb142c

1 file changed

Lines changed: 63 additions & 36 deletions

File tree

apps/app/src/app/(app)/[orgId]/cloud-tests/actions/run-platform-scan.ts

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
'use server';
22

33
import { auth } from '@/utils/auth';
4+
import { runs, tasks } from '@trigger.dev/sdk';
45
import { revalidatePath } from 'next/cache';
56
import { headers } from 'next/headers';
67

8+
const MAX_POLL_ATTEMPTS = 150; // Max 5 minutes (150 * 2 seconds)
9+
const POLL_INTERVAL_MS = 2000;
10+
711
/**
812
* Run cloud security scan for a new platform connection.
9-
* This server action calls the API and properly revalidates the cache,
10-
* ensuring consistent behavior with the legacy runTests action.
13+
* Triggers a Trigger.dev background task and polls for completion,
14+
* avoiding ALB/gateway timeouts on long-running scans.
1115
*
1216
* @param connectionId - The IntegrationConnection ID (icn_...) to scan
1317
*/
@@ -32,48 +36,71 @@ export const runPlatformScan = async (connectionId: string) => {
3236
}
3337

3438
try {
35-
// Call the cloud security scan API
36-
const apiUrl = process.env.NEXT_PUBLIC_API_URL || process.env.API_URL;
37-
if (!apiUrl) {
38-
return {
39-
success: false,
40-
error: 'API URL not configured',
41-
};
42-
}
43-
44-
const response = await fetch(`${apiUrl}/v1/cloud-security/scan/${connectionId}`, {
45-
method: 'POST',
46-
headers: {
47-
'Content-Type': 'application/json',
48-
'x-organization-id': orgId,
49-
},
39+
// Trigger the scan as a background task (same pattern as legacy runTests)
40+
// This avoids ALB/gateway timeouts on long-running Azure/AWS scans
41+
const handle = await tasks.trigger('run-cloud-security-scan', {
42+
connectionId,
43+
organizationId: orgId,
44+
providerSlug: 'platform',
45+
connectionName: connectionId,
5046
});
5147

52-
if (!response.ok) {
53-
const errorData = await response.json().catch(() => ({}));
54-
return {
55-
success: false,
56-
error: errorData.message || `Scan failed with status ${response.status}`,
57-
};
58-
}
48+
// Poll for completion
49+
let attempts = 0;
50+
while (attempts < MAX_POLL_ATTEMPTS) {
51+
const run = await runs.retrieve(handle.id);
52+
53+
if (run.isCompleted) {
54+
// Revalidate cache
55+
const headersList = await headers();
56+
let path = headersList.get('x-pathname') || headersList.get('referer') || '';
57+
path = path.replace(/\/[a-z]{2}\//, '/');
58+
if (path) {
59+
revalidatePath(path);
60+
}
61+
revalidatePath(`/${orgId}/cloud-tests`);
5962

60-
const result = await response.json();
63+
if (run.isSuccess) {
64+
const output = run.output as {
65+
success?: boolean;
66+
error?: string;
67+
findingsCount?: number;
68+
provider?: string;
69+
scannedAt?: string;
70+
} | null;
6171

62-
// Revalidate the cloud-tests page to refresh data
63-
const headersList = await headers();
64-
let path = headersList.get('x-pathname') || headersList.get('referer') || '';
65-
path = path.replace(/\/[a-z]{2}\//, '/');
66-
if (path) {
67-
revalidatePath(path);
72+
if (output?.success === false) {
73+
return {
74+
success: false,
75+
error: output.error || 'Scan completed with errors',
76+
};
77+
}
78+
79+
return {
80+
success: true,
81+
findingsCount: output?.findingsCount,
82+
provider: output?.provider,
83+
scannedAt: output?.scannedAt,
84+
};
85+
}
86+
87+
return {
88+
success: false,
89+
error: 'Scan task failed or was canceled',
90+
};
91+
}
92+
93+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
94+
attempts++;
6895
}
69-
// Also revalidate the org's cloud-tests path specifically
96+
97+
// Polling timeout - the scan is still running in the background
98+
// Revalidate anyway so fresh data shows on next page load
7099
revalidatePath(`/${orgId}/cloud-tests`);
71100

72101
return {
73-
success: true,
74-
findingsCount: result.findingsCount,
75-
provider: result.provider,
76-
scannedAt: result.scannedAt,
102+
success: false,
103+
error: 'Scan is taking longer than expected. Results will appear when complete.',
77104
};
78105
} catch (error) {
79106
console.error('Error running platform scan:', error);

0 commit comments

Comments
 (0)