Skip to content

Commit 408609b

Browse files
committed
monitoring: 1sでタイムアウトするように
1 parent 04e9a66 commit 408609b

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

.yarn/install-state.gz

-5.8 KB
Binary file not shown.
+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
import { withTimeout } from "utils";
2+
3+
declare const global: Record<string, () => void>;
14
global.main = main;
25

36
function main() {
47
const url = "https://uplim-kun.onrender.com";
5-
UrlFetchApp.fetch(url).getContentText();
8+
try {
9+
withTimeout(
10+
new Promise((resolve) => {
11+
UrlFetchApp.fetch(url).getResponseCode();
12+
resolve("");
13+
}),
14+
1000,
15+
);
16+
} catch {
17+
// 何もしない
18+
}
619
}

packages/utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./postMessage";
2+
export * from "./withTimeout";

packages/utils/src/withTimeout.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* https://qiita.com/Yarnaguchi/items/515af4c4b068c4d1f25e
3+
* await にタイムアウトを設定する
4+
* timeoutを超えて await で待機すると UnhandledPromiseRejection 例外が throw されます。
5+
* @param {Promise} promise - 実行したい promise 関数
6+
* @param {number} timeout - タイムアウト(ms)
7+
* @returns Promise<T>
8+
*/
9+
export function withTimeout<T>(promise: Promise<T>, timeout: number) {
10+
const timeoutPromise: Promise<T> = new Promise((_, reject) =>
11+
setTimeout(() => reject(), timeout),
12+
);
13+
return Promise.race([promise, timeoutPromise]);
14+
}

0 commit comments

Comments
 (0)