|
1 | | -import type { Handler, HandlerEvent, HandlerContext } from '@netlify/functions' |
| 1 | +import type { Config } from "@netlify/functions"; |
2 | 2 | import { |
3 | 3 | refreshNpmOrgStats, |
4 | 4 | fetchGitHubOwnerStats, |
5 | 5 | fetchGitHubRepoStats, |
6 | | -} from '~/utils/stats.functions' |
7 | | -import { setCachedGitHubStats } from '~/utils/stats-db.server' |
| 6 | +} from "~/utils/stats.functions"; |
| 7 | +import { setCachedGitHubStats } from "~/utils/stats-db.server"; |
8 | 8 |
|
9 | 9 | /** |
10 | | - * Netlify Background + Scheduled Function - Refresh org-level stats cache |
11 | | - * |
12 | | - * This function combines both background and scheduled execution: |
13 | | - * - Can be invoked via HTTP POST (returns 202 immediately, runs in background) |
14 | | - * - Can be triggered on a schedule (runs automatically via cron) |
15 | | - * |
16 | | - * Background functions have a 15-minute timeout (vs 30 seconds for regular functions) |
17 | | - * and return a 202 response immediately while processing continues in the background. |
| 10 | + * Netlify Scheduled Function - Refresh org-level stats cache |
18 | 11 | * |
19 | 12 | * This function refreshes pre-aggregated stats for the TanStack organization: |
20 | 13 | * - GitHub: stars, contributors, dependents |
21 | 14 | * - NPM: total downloads across all packages (including legacy packages) |
22 | 15 | * |
23 | | - * Scheduled: Runs automatically every 6 hours (configured via export config) |
24 | | - * Only called by Netlify's scheduler - not publicly accessible |
25 | | - * Timeout: 15 minutes |
| 16 | + * Scheduled: Runs automatically every 6 hours |
26 | 17 | * Concurrency: 8 packages at a time, 500ms delay between packages |
27 | 18 | * |
28 | 19 | * Performance Impact: |
29 | 20 | * - Refresh time: ~10-20 minutes for 203 packages (199 scoped + 4 legacy) |
30 | 21 | * - After refresh: ~100-200ms per request (served from cache) |
31 | 22 | */ |
32 | | -export const handler: Handler = async ( |
33 | | - event: HandlerEvent, |
34 | | - context: HandlerContext, |
35 | | -) => { |
36 | | - console.log('[refresh-stats-cache-background] Starting stats refresh...') |
| 23 | +const handler = async (req: Request) => { |
| 24 | + const { next_run } = await req.json(); |
| 25 | + |
| 26 | + console.log("[refresh-stats-cache-background] Starting stats refresh..."); |
37 | 27 |
|
38 | | - const startTime = Date.now() |
| 28 | + const startTime = Date.now(); |
39 | 29 |
|
40 | 30 | try { |
41 | | - const org = 'tanstack' |
| 31 | + const org = "tanstack"; |
42 | 32 |
|
43 | 33 | // Refresh NPM org stats (fetches all packages, aggregates, and caches) |
44 | | - console.log('[refresh-stats-cache-background] Refreshing NPM org stats...') |
45 | | - const npmStats = await refreshNpmOrgStats(org) |
| 34 | + console.log("[refresh-stats-cache-background] Refreshing NPM org stats..."); |
| 35 | + const npmStats = await refreshNpmOrgStats(org); |
46 | 36 |
|
47 | 37 | // Refresh GitHub org stats |
48 | 38 | console.log( |
49 | | - '[refresh-stats-cache-background] Refreshing GitHub org stats...', |
50 | | - ) |
51 | | - const githubCacheKey = `org:${org}` |
52 | | - const githubStats = await fetchGitHubOwnerStats(org) |
53 | | - await setCachedGitHubStats(githubCacheKey, githubStats, 1) |
| 39 | + "[refresh-stats-cache-background] Refreshing GitHub org stats..." |
| 40 | + ); |
| 41 | + const githubCacheKey = `org:${org}`; |
| 42 | + const githubStats = await fetchGitHubOwnerStats(org); |
| 43 | + await setCachedGitHubStats(githubCacheKey, githubStats, 1); |
54 | 44 |
|
55 | 45 | // Refresh GitHub stats for each library repo |
56 | 46 | console.log( |
57 | | - '[refresh-stats-cache-background] Refreshing GitHub stats for individual libraries...', |
58 | | - ) |
59 | | - const { libraries } = await import('~/libraries') |
| 47 | + "[refresh-stats-cache-background] Refreshing GitHub stats for individual libraries..." |
| 48 | + ); |
| 49 | + const { libraries } = await import("~/libraries"); |
60 | 50 | console.log( |
61 | 51 | `[refresh-stats-cache-background] Found ${libraries.length} libraries to process:`, |
62 | | - libraries.map((lib) => ({ id: lib.id, repo: lib.repo })), |
63 | | - ) |
64 | | - const libraryResults = [] |
65 | | - const libraryErrors = [] |
| 52 | + libraries.map((lib) => ({ id: lib.id, repo: lib.repo })) |
| 53 | + ); |
| 54 | + const libraryResults = []; |
| 55 | + const libraryErrors = []; |
66 | 56 |
|
67 | 57 | for (let i = 0; i < libraries.length; i++) { |
68 | | - const library = libraries[i] |
| 58 | + const library = libraries[i]; |
69 | 59 | if (!library.repo) { |
70 | 60 | console.log( |
71 | | - `[refresh-stats-cache-background] Skipping library ${library.id} - no repo`, |
72 | | - ) |
73 | | - continue |
| 61 | + `[refresh-stats-cache-background] Skipping library ${library.id} - no repo` |
| 62 | + ); |
| 63 | + continue; |
74 | 64 | } |
75 | 65 |
|
76 | 66 | console.log( |
77 | | - `[refresh-stats-cache-background] Processing library ${library.id} (${library.repo})...`, |
78 | | - ) |
| 67 | + `[refresh-stats-cache-background] Processing library ${library.id} (${library.repo})...` |
| 68 | + ); |
79 | 69 | try { |
80 | | - const repoStats = await fetchGitHubRepoStats(library.repo) |
| 70 | + const repoStats = await fetchGitHubRepoStats(library.repo); |
81 | 71 | console.log( |
82 | 72 | `[refresh-stats-cache-background] Fetched stats for ${ |
83 | 73 | library.repo |
84 | 74 | }: ${repoStats.starCount} stars, ${ |
85 | 75 | repoStats.contributorCount |
86 | | - } contributors, ${repoStats.dependentCount ?? 'N/A'} dependents`, |
87 | | - ) |
88 | | - await setCachedGitHubStats(library.repo, repoStats, 1) |
| 76 | + } contributors, ${repoStats.dependentCount ?? "N/A"} dependents` |
| 77 | + ); |
| 78 | + await setCachedGitHubStats(library.repo, repoStats, 1); |
89 | 79 | console.log( |
90 | | - `[refresh-stats-cache-background] ✓ Successfully cached stats for ${library.repo}`, |
91 | | - ) |
| 80 | + `[refresh-stats-cache-background] ✓ Successfully cached stats for ${library.repo}` |
| 81 | + ); |
92 | 82 | libraryResults.push({ |
93 | 83 | repo: library.repo, |
94 | 84 | stars: repoStats.starCount, |
95 | | - }) |
| 85 | + }); |
96 | 86 |
|
97 | 87 | // Add delay between requests to avoid rate limiting (except for last item) |
98 | 88 | if (i < libraries.length - 1) { |
99 | | - await new Promise((resolve) => setTimeout(resolve, 500)) |
| 89 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
100 | 90 | } |
101 | 91 | } catch (error) { |
102 | 92 | const errorMessage = |
103 | | - error instanceof Error ? error.message : String(error) |
| 93 | + error instanceof Error ? error.message : String(error); |
104 | 94 | console.error( |
105 | 95 | `[refresh-stats-cache-background] Failed to refresh ${library.repo}:`, |
106 | | - errorMessage, |
107 | | - ) |
| 96 | + errorMessage |
| 97 | + ); |
108 | 98 | libraryErrors.push({ |
109 | 99 | repo: library.repo, |
110 | 100 | error: errorMessage, |
111 | | - }) |
| 101 | + }); |
112 | 102 | } |
113 | 103 | } |
114 | 104 |
|
115 | | - const duration = Date.now() - startTime |
| 105 | + const duration = Date.now() - startTime; |
116 | 106 | console.log( |
117 | 107 | `[refresh-stats-cache-background] ✓ Completed in ${duration}ms - NPM: ${npmStats.totalDownloads.toLocaleString()} downloads (${ |
118 | 108 | Object.keys(npmStats.packageStats || {}).length |
119 | 109 | } packages), GitHub Org: ${githubStats.starCount.toLocaleString()} stars, Libraries: ${ |
120 | 110 | libraryResults.length |
121 | | - } refreshed, ${libraryErrors.length} failed`, |
122 | | - ) |
123 | | - |
124 | | - return { |
125 | | - statusCode: 200, |
126 | | - body: JSON.stringify({ |
127 | | - success: true, |
128 | | - duration, |
129 | | - stats: { |
130 | | - npm: { |
131 | | - totalDownloads: npmStats.totalDownloads, |
132 | | - packageCount: Object.keys(npmStats.packageStats || {}).length, |
133 | | - }, |
134 | | - github: { |
135 | | - org: { |
136 | | - starCount: githubStats.starCount, |
137 | | - contributorCount: githubStats.contributorCount, |
138 | | - dependentCount: githubStats.dependentCount, |
139 | | - }, |
140 | | - libraries: { |
141 | | - refreshed: libraryResults.length, |
142 | | - failed: libraryErrors.length, |
143 | | - results: libraryResults, |
144 | | - errors: libraryErrors, |
145 | | - }, |
146 | | - }, |
147 | | - }, |
148 | | - }), |
149 | | - } |
| 111 | + } refreshed, ${libraryErrors.length} failed` |
| 112 | + ); |
| 113 | + console.log( |
| 114 | + "[refresh-stats-cache-background] Next invocation at:", |
| 115 | + next_run |
| 116 | + ); |
150 | 117 | } catch (error) { |
151 | | - const duration = Date.now() - startTime |
152 | | - const errorMessage = error instanceof Error ? error.message : String(error) |
153 | | - const errorStack = error instanceof Error ? error.stack : undefined |
| 118 | + const duration = Date.now() - startTime; |
| 119 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 120 | + const errorStack = error instanceof Error ? error.stack : undefined; |
154 | 121 |
|
155 | 122 | console.error( |
156 | 123 | `[refresh-stats-cache-background] ✗ Failed after ${duration}ms:`, |
157 | | - errorMessage, |
158 | | - ) |
| 124 | + errorMessage |
| 125 | + ); |
159 | 126 | if (errorStack) { |
160 | | - console.error('[refresh-stats-cache-background] Stack:', errorStack) |
161 | | - } |
162 | | - |
163 | | - return { |
164 | | - statusCode: 500, |
165 | | - body: JSON.stringify({ |
166 | | - success: false, |
167 | | - duration, |
168 | | - error: errorMessage, |
169 | | - stack: errorStack, |
170 | | - }), |
| 127 | + console.error("[refresh-stats-cache-background] Stack:", errorStack); |
171 | 128 | } |
172 | 129 | } |
173 | | -} |
| 130 | +}; |
174 | 131 |
|
175 | | -/** |
176 | | - * Netlify function configuration |
177 | | - * - type: 'experimental-background' enables background execution (15 min timeout, returns 202 immediately) |
178 | | - * - schedule: Cron expression for scheduled execution (runs every 6 hours) |
179 | | - */ |
180 | | -export const config = { |
181 | | - type: 'experimental-background' as const, |
182 | | - schedule: '0 */6 * * *', // Every 6 hours |
183 | | -} |
| 132 | +export default handler; |
| 133 | + |
| 134 | +export const config: Config = { |
| 135 | + schedule: "0 */6 * * *", // Every 6 hours |
| 136 | +}; |
0 commit comments