@@ -25,9 +25,7 @@ async function invokeBatch(
2525 const invocationPromises = Array . from ( { length : count } , ( ) => {
2626 const command = new InvokeCommand ( {
2727 FunctionName : lambdaName ,
28- InvocationType : "RequestResponse" ,
29- LogType : LogType . Tail ,
30- Payload : JSON . stringify ( { action : "warmup" } ) ,
28+ Payload : JSON . stringify ( { action : "warmer" } ) ,
3129 } ) ;
3230 return lambdaClient . send ( command ) ;
3331 } ) ;
@@ -36,58 +34,66 @@ async function invokeBatch(
3634 const foundInstanceIds = new Set < string > ( ) ;
3735
3836 results . forEach ( ( result ) => {
39- if ( result . status === "fulfilled" ) {
37+ if ( result . status === "fulfilled" && result . value . Payload ) {
4038 try {
4139 const payloadString = textDecoder . decode ( result . value . Payload ) ;
42- const payload = JSON . parse ( payloadString ) ;
43- if ( payload . instanceId ) {
44- foundInstanceIds . add ( payload . instanceId ) ;
40+ const body = JSON . parse ( payloadString ) ;
41+ if ( body . instanceId ) {
42+ foundInstanceIds . add ( body . instanceId ) ;
4543 }
4644 } catch ( e ) {
47- // Suppress errors for failed payload parsing
45+ console . error ( "Error parsing payload from target function:" , e ) ;
4846 }
47+ } else if ( result . status === "rejected" ) {
48+ console . error ( "Invocation failed:" , result . reason . message ) ;
4949 }
5050 } ) ;
5151
5252 return foundInstanceIds ;
5353}
5454
5555export const handler = async ( event : { } ) => {
56- const { lambdaName, numInstancesStr } = {
56+ const { lambdaName, numInstancesStr, maxWavesStr } = {
5757 lambdaName : process . env . LAMBDA_NAME ,
5858 numInstancesStr : process . env . NUM_INSTANCES ,
59+ maxWavesStr : process . env . MAX_WAVES ,
5960 } ;
61+
6062 if ( ! lambdaName || ! numInstancesStr ) {
61- throw new Error ( "Parameters 'lambdaName ' and 'numInstances ' are required." ) ;
63+ throw new Error ( "Env vars 'LAMBDA_NAME ' and 'NUM_INSTANCES ' are required." ) ;
6264 }
65+
6366 const numInstances = parseInt ( numInstancesStr , 10 ) ;
67+ // Default to 2 waves if MAX_WAVES is not set
68+ const maxWaves = parseInt ( maxWavesStr || "2" , 10 ) ;
6469
6570 let totalInvocations = 0 ;
71+ let wavesCompleted = 0 ;
72+ const uniqueInstanceIds = new Set < string > ( ) ;
6673
67- const uniqueInstanceIds = await invokeBatch ( lambdaName , numInstances ) ;
68- totalInvocations += numInstances ;
69-
70- console . log (
71- `Wave 1 complete. Found ${ uniqueInstanceIds . size } of ${ numInstances } unique instances.` ,
72- ) ;
74+ for ( let i = 1 ; i <= maxWaves ; i ++ ) {
75+ wavesCompleted = i ;
7376
74- if ( uniqueInstanceIds . size < numInstances ) {
75- console . log (
76- `Target not met. Firing another full batch of ${ numInstances } invocations.` ,
77- ) ;
77+ // Calculate how many more instances are needed
78+ const neededCount = numInstances - uniqueInstanceIds . size ;
79+ if ( neededCount <= 0 ) {
80+ console . log ( "Target met. No more waves needed." ) ;
81+ break ;
82+ }
7883
79- const secondWaveIds = await invokeBatch ( lambdaName , numInstances ) ;
84+ console . log ( `--- Wave ${ i } of ${ maxWaves } ---` ) ;
85+ const newIds = await invokeBatch ( lambdaName , numInstances ) ;
8086 totalInvocations += numInstances ;
8187
82- secondWaveIds . forEach ( ( id ) => uniqueInstanceIds . add ( id ) ) ;
88+ newIds . forEach ( ( id ) => uniqueInstanceIds . add ( id ) ) ;
8389
8490 console . log (
85- `Wave 2 complete. Total unique instances is now ${ uniqueInstanceIds . size } .` ,
91+ `Wave ${ i } complete. Found ${ uniqueInstanceIds . size } of ${ numInstances } unique instances .` ,
8692 ) ;
8793 }
8894
8995 console . log (
90- `Warming complete. Found ${ uniqueInstanceIds . size } unique instances from ${ totalInvocations } total invocations.` ,
96+ `Warming complete. Found ${ uniqueInstanceIds . size } unique instances from ${ totalInvocations } total invocations over ${ wavesCompleted } waves .` ,
9197 ) ;
9298
9399 return {
@@ -96,13 +102,15 @@ export const handler = async (event: {}) => {
96102 targetInstances : numInstances ,
97103 warmedInstances : uniqueInstanceIds . size ,
98104 totalInvocations,
105+ wavesCompleted,
99106 instanceIds : [ ...uniqueInstanceIds ] ,
100107 } ) ,
101108 } ;
102109} ;
103110
104111if ( import . meta. url === `file://${ process . argv [ 1 ] } ` ) {
105112 process . env . LAMBDA_NAME = "infra-core-api-lambda" ;
106- process . env . NUM_INSTANCES = "2" ;
113+ process . env . NUM_INSTANCES = "3" ;
114+ process . env . MAX_WAVES = "3" ; // Configurable number of waves
107115 console . log ( await handler ( { } ) ) ;
108116}
0 commit comments