Skip to content

Commit 04c5245

Browse files
committed
Upgrade periodic-execution to 0.1.0
1 parent bf35dcb commit 04c5245

6 files changed

Lines changed: 271 additions & 108 deletions

File tree

dist/clean/index.js

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
const core = __webpack_require__(186);
3232
const fetch = __webpack_require__(805);
3333
const isPortReachable = __webpack_require__(157);
34-
const periodicExecution = __webpack_require__(660);
34+
const { periodicExecution, TimeoutError } = __webpack_require__(660);
3535
const process = __webpack_require__(765);
3636

3737
const config = __webpack_require__(532);
@@ -83,15 +83,25 @@ async function deploy() {
8383
ipv4
8484
});
8585

86-
const online = await periodicExecution(fn, true, options.timeout);
86+
let online;
87+
88+
try {
89+
online = await periodicExecution(fn, true, options.timeout);
90+
} catch(err) {
91+
if (err instanceof TimeoutError) {
92+
online = false;
93+
} else {
94+
throw err;
95+
}
96+
}
8797

8898
if (online) {
8999
return res;
90100
} else {
91101
core.setFailed(
92102
`Waited ${
93103
options.timeout
94-
}ms for server to come online, but it never came online.`
104+
}ms for server to come online, but it never came online. Value: "${online}"`
95105
);
96106
}
97107
} else {
@@ -173,33 +183,29 @@ function getAssignmentProgress(floatingIPId, actionId) {
173183
}
174184

175185
async function getFloatingIP(id) {
176-
const URI = `${
177-
config.API
178-
}/floating_ips/${id}`;
186+
const URI = `${config.API}/floating_ips/${id}`;
179187

180-
try {
181-
res = await fetch(URI, {
182-
method: "GET",
183-
headers: {
184-
"Content-Type": "application/json",
185-
Authorization: `Bearer ${options.hcloudToken}`,
186-
"User-Agent": config.USER_AGENT
187-
}
188-
});
189-
} catch (err) {
190-
core.setFailed(err.message);
191-
}
188+
try {
189+
res = await fetch(URI, {
190+
method: "GET",
191+
headers: {
192+
"Content-Type": "application/json",
193+
Authorization: `Bearer ${options.hcloudToken}`,
194+
"User-Agent": config.USER_AGENT
195+
}
196+
});
197+
} catch (err) {
198+
core.setFailed(err.message);
199+
}
192200

193201
if (res.status === 200) {
194202
const body = await res.json();
195203
return body.floating_ip.ip;
196204
} else {
197-
core.setFailed(
198-
`When trying to get a floating ip, an error occurred ${
199-
res.status
200-
}`
201-
);
202-
return;
205+
core.setFailed(
206+
`When trying to get a floating ip, an error occurred ${res.status}`
207+
);
208+
return;
203209
}
204210
}
205211

@@ -246,22 +252,33 @@ async function assignIP() {
246252
if (res.status === 201) {
247253
const body = await res.json();
248254

249-
const expectedStatus = "success"
250-
const done = await periodicExecution(
251-
getAssignmentProgress(parsedIPId, body.action.id),
252-
expectedStatus,
253-
assignmentTimeout
254-
);
255+
const expectedStatus = "success";
256+
let _status;
257+
try {
258+
_status = await periodicExecution(
259+
getAssignmentProgress(parsedIPId, body.action.id),
260+
expectedStatus,
261+
assignmentTimeout
262+
);
263+
} catch (err) {
264+
if (err instanceof TimeoutError) {
265+
_status = "timeout";
266+
} else {
267+
throw err;
268+
}
269+
}
255270

256-
if (done) {
271+
if (_status === "success") {
257272
const floatingIP = await getFloatingIP(parsedIPId);
258273
core.exportVariable("SERVER_FLOATING_IPV4", floatingIP);
259274
core.info(
260275
`Floating IP with ID "${parsedIPId}" was assigned to server with id: "${SERVER_ID}"`
261276
);
262277
return;
263278
} else {
264-
core.setFailed(`Timed out while trying to set the server's floating IP.`);
279+
core.setFailed(
280+
`An error happened while trying to get the IP's assignment progress. Status: "${_status}"`
281+
);
265282
return;
266283
}
267284
} else {
@@ -2409,27 +2426,47 @@ let defaultOptions = {
24092426
interval: 1000
24102427
};
24112428

2429+
class TimeoutError extends Error {
2430+
constructor(...params) {
2431+
super(...params);
2432+
2433+
if (Error.captureStackTrace) {
2434+
Error.captureStackTrace(this, TimeoutError);
2435+
}
2436+
2437+
this.name = "TimeoutError";
2438+
}
2439+
}
2440+
24122441
async function periodicExecution(fn, expected, timeout, options) {
24132442
options = { ...defaultOptions, ...options };
24142443

2415-
return new Promise(async result => {
2444+
return new Promise(async (resolve, reject) => {
24162445
const start = hrtime.bigint();
24172446
let outcome;
24182447
// NOTE: hrtime.bigint() is in nano seconds, which is 1e-6 apart from milli
24192448
// seconds
24202449
while (Number(hrtime.bigint() - start) / (1000 * 1000) < timeout) {
24212450
outcome = await fn();
24222451
if (outcome === expected) {
2423-
break;
2452+
return resolve(outcome);
24242453
}
24252454

24262455
await new Promise(resolve => setTimeout(resolve, options.interval));
24272456
}
2428-
return result(outcome);
2457+
2458+
return reject(
2459+
new TimeoutError(
2460+
`Execution didn't reach expected result. Outcome: "${outcome}"`
2461+
)
2462+
);
24292463
});
24302464
}
24312465

2432-
module.exports = periodicExecution;
2466+
module.exports = {
2467+
periodicExecution,
2468+
TimeoutError
2469+
};
24332470

24342471

24352472
/***/ }),

dist/deploy/index.js

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const { deploy, assignIP } = __webpack_require__(909);
3636
const core = __webpack_require__(186);
3737
const fetch = __webpack_require__(805);
3838
const isPortReachable = __webpack_require__(157);
39-
const periodicExecution = __webpack_require__(660);
39+
const { periodicExecution, TimeoutError } = __webpack_require__(660);
4040
const process = __webpack_require__(765);
4141

4242
const 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

180190
async 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+
24172446
async 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

Comments
 (0)