@@ -12,18 +12,27 @@ const { program } = require("commander");
1212const pjson = require ( "../package.json" ) ;
1313
1414if ( require . main === module ) {
15- execute ( "cli" ) ;
15+ // local execution
16+ execute ( { Records : [ { body : null } ] } , { } ) ;
1617}
1718
18- async function execute ( logLevel = "on" ) {
19+ // eslint-disable-next-line no-unused-vars
20+ async function execute ( event , _context ) {
21+ let baseURlFromEvent ;
22+ const body = event . Records ?. [ 0 ] ?. body ;
23+ if ( body ) {
24+ baseURlFromEvent = JSON . parse ( body ) ?. baseUrl ;
25+ }
1926 const ignoreSSL = "false" ; // Fail any "https" tests with missing cert.
27+
2028 program . version ( pjson . version ) ;
2129 program
2230 . description ( "Start worker to execute RapidAPI tests and requests" )
2331 . requiredOption (
2432 "-u, --url <baseUrl>" ,
2533 "The base URL to fetch executions from (env variable: BASE_URL)" ,
26- process . env . BASE_URL || process . env . URL || "https://rapidapi.com/testing"
34+ // default workers get baseUrl from SQS event body, otherwise from config
35+ baseURlFromEvent || process . env . BASE_URL || "https://rapidapi.com/testing"
2736 )
2837 . requiredOption (
2938 "-s, --secret <secret>" ,
@@ -58,7 +67,7 @@ async function execute(logLevel = "on") {
5867 . option (
5968 "-l, --logging [on, off, cli]" ,
6069 "Logging level. 'cli' prints additional information at startup useful for debugging" ,
61- process . env . WORKER_LOGGING || logLevel
70+ process . env . WORKER_LOGGING || "off"
6271 )
6372 . option (
6473 "--ignore-ssl [true, false]" ,
@@ -97,39 +106,56 @@ async function execute(logLevel = "on") {
97106 const START_TIMESTAMP = Date . now ( ) ;
98107
99108 let cycle = 1 ;
100- // eslint-disable-next-line
101- if ( logging ) console . log ( `Staring cycle ${ cycle ++ } ` ) ;
102- try {
103- await executeOnce ( { ...global . settings } ) ;
104- } catch ( err ) {
105- consola . error ( err ) ;
106- }
107- if ( cmd . frequency ) {
109+ if ( ! cmd . frequency ) {
110+ if ( logging ) consola . log ( `Staring cycle ${ cycle } ` ) ;
111+ try {
112+ await executeOnce ( { ...global . settings } ) ;
113+ } catch ( err ) {
114+ consola . error ( err ) ;
115+ }
116+ } else {
117+ let unresolvedCycles = 0 ;
118+ let maxTimeReached = false ;
108119 const testLoop = new Promise ( ( resolve ) => {
109120 const interval = setInterval ( async function ( ) {
110121 if ( parseInt ( cmd . max ) ) {
111122 let currentTimestamp = Date . now ( ) ;
112123 if ( currentTimestamp > parseInt ( START_TIMESTAMP ) + parseInt ( cmd . max ) ) {
124+ if ( logging ) consola . log ( `Max polling time reached` ) ;
113125 clearInterval ( interval ) ;
114- resolve ( ) ;
126+ maxTimeReached = true ;
127+ if ( unresolvedCycles <= 0 ) {
128+ resolve ( ) ;
129+ }
130+ return ;
115131 }
116132 }
117133 if ( logging ) {
118134 // eslint-disable-next-line
119135 console . log ( `Staring cycle ${ cycle ++ } ` ) ;
120136 }
121137 try {
138+ unresolvedCycles += 1 ;
122139 await executeOnce ( { ...global . settings } ) ;
123140 } catch ( err ) {
124141 consola . error ( err ) ;
142+ } finally {
143+ unresolvedCycles -= 1 ;
144+ if ( maxTimeReached && unresolvedCycles <= 0 ) {
145+ resolve ( ) ;
146+ }
125147 }
126148 } , cmd . frequency ) ;
127149 } ) ;
128150 await testLoop ;
151+ if ( logging ) {
152+ consola . log ( `End execute()` ) ;
153+ }
129154 }
130155}
131156
132157async function executeOnce ( overwriteDetails = { } ) {
158+ // eslint-disable-next-line max-len
133159 return Promise . all ( [ fetchAndExecuteTests ( overwriteDetails ) , fetchAndExecuteRequests ( overwriteDetails ) ] ) . catch ( ( e ) => {
134160 if ( global . settings . logging ) {
135161 if ( e . response ) {
0 commit comments