@@ -36,7 +36,7 @@ const { deploy, assignIP } = __webpack_require__(909);
3636const core = __webpack_require__ ( 186 ) ;
3737const fetch = __webpack_require__ ( 805 ) ;
3838const isPortReachable = __webpack_require__ ( 157 ) ;
39- const periodicExecution = __webpack_require__ ( 660 ) ;
39+ const { periodicExecution, TimeoutError } = __webpack_require__ ( 660 ) ;
4040const process = __webpack_require__ ( 765 ) ;
4141
4242const config = __webpack_require__ ( 532 ) ;
@@ -88,15 +88,25 @@ async function deploy() {
8888 ipv4
8989 } ) ;
9090
91- const online = await periodicExecution ( fn , true , options . timeout ) ;
91+ let online ;
92+
93+ try {
94+ online = await periodicExecution ( fn , true , options . timeout ) ;
95+ } catch ( err ) {
96+ if ( err instanceof TimeoutError ) {
97+ online = false ;
98+ } else {
99+ throw err ;
100+ }
101+ }
92102
93103 if ( online ) {
94104 return res ;
95105 } else {
96106 core . setFailed (
97107 `Waited ${
98108 options . timeout
99- } ms for server to come online, but it never came online.`
109+ } ms for server to come online, but it never came online. Value: " ${ online } " `
100110 ) ;
101111 }
102112 } else {
@@ -178,33 +188,29 @@ function getAssignmentProgress(floatingIPId, actionId) {
178188}
179189
180190async function getFloatingIP ( id ) {
181- const URI = `${
182- config . API
183- } /floating_ips/${ id } `;
191+ const URI = `${ config . API } /floating_ips/${ id } ` ;
184192
185- try {
186- res = await fetch ( URI , {
187- method : "GET" ,
188- headers : {
189- "Content-Type" : "application/json" ,
190- Authorization : `Bearer ${ options . hcloudToken } ` ,
191- "User-Agent" : config . USER_AGENT
192- }
193- } ) ;
194- } catch ( err ) {
195- core . setFailed ( err . message ) ;
196- }
193+ try {
194+ res = await fetch ( URI , {
195+ method : "GET" ,
196+ headers : {
197+ "Content-Type" : "application/json" ,
198+ Authorization : `Bearer ${ options . hcloudToken } ` ,
199+ "User-Agent" : config . USER_AGENT
200+ }
201+ } ) ;
202+ } catch ( err ) {
203+ core . setFailed ( err . message ) ;
204+ }
197205
198206 if ( res . status === 200 ) {
199207 const body = await res . json ( ) ;
200208 return body . floating_ip . ip ;
201209 } else {
202- core . setFailed (
203- `When trying to get a floating ip, an error occurred ${
204- res . status
205- } `
206- ) ;
207- return ;
210+ core . setFailed (
211+ `When trying to get a floating ip, an error occurred ${ res . status } `
212+ ) ;
213+ return ;
208214 }
209215}
210216
@@ -251,22 +257,33 @@ async function assignIP() {
251257 if ( res . status === 201 ) {
252258 const body = await res . json ( ) ;
253259
254- const expectedStatus = "success"
255- const done = await periodicExecution (
256- getAssignmentProgress ( parsedIPId , body . action . id ) ,
257- expectedStatus ,
258- assignmentTimeout
259- ) ;
260+ const expectedStatus = "success" ;
261+ let _status ;
262+ try {
263+ _status = await periodicExecution (
264+ getAssignmentProgress ( parsedIPId , body . action . id ) ,
265+ expectedStatus ,
266+ assignmentTimeout
267+ ) ;
268+ } catch ( err ) {
269+ if ( err instanceof TimeoutError ) {
270+ _status = "timeout" ;
271+ } else {
272+ throw err ;
273+ }
274+ }
260275
261- if ( done ) {
276+ if ( _status === "success" ) {
262277 const floatingIP = await getFloatingIP ( parsedIPId ) ;
263278 core . exportVariable ( "SERVER_FLOATING_IPV4" , floatingIP ) ;
264279 core . info (
265280 `Floating IP with ID "${ parsedIPId } " was assigned to server with id: "${ SERVER_ID } "`
266281 ) ;
267282 return ;
268283 } else {
269- core . setFailed ( `Timed out while trying to set the server's floating IP.` ) ;
284+ core . setFailed (
285+ `An error happened while trying to get the IP's assignment progress. Status: "${ _status } "`
286+ ) ;
270287 return ;
271288 }
272289 } else {
@@ -2414,27 +2431,47 @@ let defaultOptions = {
24142431 interval : 1000
24152432} ;
24162433
2434+ class TimeoutError extends Error {
2435+ constructor ( ...params ) {
2436+ super ( ...params ) ;
2437+
2438+ if ( Error . captureStackTrace ) {
2439+ Error . captureStackTrace ( this , TimeoutError ) ;
2440+ }
2441+
2442+ this . name = "TimeoutError" ;
2443+ }
2444+ }
2445+
24172446async function periodicExecution ( fn , expected , timeout , options ) {
24182447 options = { ...defaultOptions , ...options } ;
24192448
2420- return new Promise ( async result => {
2449+ return new Promise ( async ( resolve , reject ) => {
24212450 const start = hrtime . bigint ( ) ;
24222451 let outcome ;
24232452 // NOTE: hrtime.bigint() is in nano seconds, which is 1e-6 apart from milli
24242453 // seconds
24252454 while ( Number ( hrtime . bigint ( ) - start ) / ( 1000 * 1000 ) < timeout ) {
24262455 outcome = await fn ( ) ;
24272456 if ( outcome === expected ) {
2428- break ;
2457+ return resolve ( outcome ) ;
24292458 }
24302459
24312460 await new Promise ( resolve => setTimeout ( resolve , options . interval ) ) ;
24322461 }
2433- return result ( outcome ) ;
2462+
2463+ return reject (
2464+ new TimeoutError (
2465+ `Execution didn't reach expected result. Outcome: "${ outcome } "`
2466+ )
2467+ ) ;
24342468 } ) ;
24352469}
24362470
2437- module . exports = periodicExecution ;
2471+ module . exports = {
2472+ periodicExecution,
2473+ TimeoutError
2474+ } ;
24382475
24392476
24402477/***/ } ) ,
0 commit comments