@@ -6,22 +6,25 @@ const BATCH_SIZE = 1000
66async function getResourceId (
77 supabase : SupabaseClient ,
88 resourceType : string ,
9+ prerelease : boolean ,
910 cache : Map < string , string >
1011) : Promise < string > {
11- if ( cache . has ( resourceType ) ) {
12- return cache . get ( resourceType ) !
12+ const cacheKey = `${ resourceType } :${ prerelease } `
13+ if ( cache . has ( cacheKey ) ) {
14+ return cache . get ( cacheKey ) !
1315 }
1416
1517 const { data, error } = await supabase
1618 . from ( 'registry_resources' )
1719 . select ( 'id' )
1820 . eq ( 'type' , resourceType )
21+ . eq ( 'prerelease' , prerelease )
1922
2023 if ( error || ! data ?. [ 0 ] ?. id ) {
21- throw new Error ( `Resource type '${ resourceType } ' not found in registry_resources` )
24+ throw new Error ( `Resource type '${ resourceType } ' (prerelease= ${ prerelease } ) not found in registry_resources` )
2225 }
2326
24- cache . set ( resourceType , data [ 0 ] . id )
27+ cache . set ( cacheKey , data [ 0 ] . id )
2528 return data [ 0 ] . id
2629}
2730
@@ -30,14 +33,15 @@ async function processModule(
3033 resourceType : string ,
3134 parameterPath : string ,
3235 fetchFn : ( ) => Promise < string [ ] > ,
36+ prerelease : boolean ,
3337 resourceIdCache : Map < string , string >
3438) : Promise < void > {
3539 console . log ( `Processing ${ resourceType } ${ parameterPath } ...` )
3640
3741 const values = await fetchFn ( )
3842 console . log ( ` [${ resourceType } ${ parameterPath } ] Fetched ${ values . length } values` )
3943
40- const resourceId = await getResourceId ( supabase , resourceType , resourceIdCache )
44+ const resourceId = await getResourceId ( supabase , resourceType , prerelease , resourceIdCache )
4145
4246 await supabase
4347 . from ( 'resource_parameter_completions' )
@@ -66,31 +70,44 @@ async function processModule(
6670 console . log ( ` [${ resourceType } ${ parameterPath } ] Done: inserted ${ values . length } completions` )
6771}
6872
73+ async function runCompletions ( env : Env ) : Promise < void > {
74+ const supabase = createClient ( env . SUPABASE_URL , env . SUPABASE_SERVICE_ROLE_KEY )
75+ const prerelease = env . PRERELEASE === 'true'
76+ const resourceIdCache = new Map < string , string > ( )
77+
78+ const results = await Promise . allSettled (
79+ completionModules . map ( ( { resourceType, parameterPath, fetch } : CompletionModule ) =>
80+ processModule ( supabase , resourceType , parameterPath , fetch , prerelease , resourceIdCache )
81+ )
82+ )
83+
84+ for ( const result of results ) {
85+ if ( result . status === 'rejected' ) {
86+ console . error ( 'Completion module failed:' , result . reason )
87+ }
88+ }
89+
90+ console . log ( 'Successfully processed all resource completion tasks' )
91+ }
92+
6993export default {
70- async fetch ( req : Request ) {
94+ async fetch ( req : Request , env : Env , ctx : ExecutionContext ) {
7195 const url = new URL ( req . url )
96+
97+ if ( req . method === 'POST' && url . pathname === '/trigger' ) {
98+ if ( req . headers . get ( 'Authorization' ) !== env . TRIGGER_SECRET ) {
99+ return new Response ( 'Unauthorized' , { status : 401 } )
100+ }
101+ ctx . waitUntil ( runCompletions ( env ) )
102+ return new Response ( 'Triggered' , { status : 202 } )
103+ }
104+
72105 url . pathname = '/__scheduled'
73106 url . searchParams . append ( 'cron' , '* * * * *' )
74107 return new Response ( `To test the scheduled handler, ensure you have used the "--test-scheduled" then try running "curl ${ url . href } ".` )
75108 } ,
76109
77110 async scheduled ( _event : ScheduledEvent , env : Env , _ctx : ExecutionContext ) : Promise < void > {
78- console . log ( 'hihi' )
79- const supabase = createClient ( env . SUPABASE_URL , env . SUPABASE_SERVICE_ROLE_KEY )
80- const resourceIdCache = new Map < string , string > ( )
81-
82- const results = await Promise . allSettled (
83- completionModules . map ( ( { resourceType, parameterPath, fetch } : CompletionModule ) =>
84- processModule ( supabase , resourceType , parameterPath , fetch , resourceIdCache )
85- )
86- )
87-
88- for ( const result of results ) {
89- if ( result . status === 'rejected' ) {
90- console . error ( 'Completion module failed:' , result . reason )
91- }
92- }
93-
94- console . log ( 'Successfully processed all resource completion tasks' )
111+ await runCompletions ( env )
95112 } ,
96113} satisfies ExportedHandler < Env >
0 commit comments