This is an automated email from the ASF dual-hosted git repository.

wangzx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/echarts-bot.git


The following commit(s) were added to refs/heads/master by this push:
     new 5656660  chore: send notification when the failure last for one hour 
and more
5656660 is described below

commit 5656660391a5a8b03c8927449583a2a3134970a3
Author: plainheart <[email protected]>
AuthorDate: Mon Mar 6 05:14:00 2023 +0800

    chore: send notification when the failure last for one hour and more
---
 src/glitch/wakeup.js | 47 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 10 deletions(-)

diff --git a/src/glitch/wakeup.js b/src/glitch/wakeup.js
index aca6343..5974804 100644
--- a/src/glitch/wakeup.js
+++ b/src/glitch/wakeup.js
@@ -3,15 +3,42 @@ const fetch = require('node-fetch');
 const WAKEUP_URL = 'https://apache-incubator-echarts-bot-1.glitch.me/probot';
 
 (async function() {
-  const body = await fetch(WAKEUP_URL, {
-    headers: {
-      'Accept': 'text/html',
-      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
+    let isWorking = true;
+    let err;
+    try {
+        const body = await fetch(WAKEUP_URL, {
+            headers: {
+                'Accept': 'text/html',
+                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
+            }
+        }).then(res => res.text());
+        if (body.indexOf('Welcome to') === -1) {
+            isWorking = false;
+            console.log('Response body:\n', body);
+        }
+    } catch (e) {
+        err = e;
+        isWorking = false;
+    } finally {
+        if (isWorking) {
+            return console.log('✨ bot is now working~');
+        }
+        const errLog = '💤 bot may not be working...';
+        console.error(errLog);
+        if (await isNotificationNeeded()) {
+            throw err || new Error(errLog);
+        }
     }
-  }).then(res => res.text());
-  if (body.indexOf('Welcome to') === -1) {
-    console.log('Response body:\n', body);
-    throw new Error('bot may not be working.');
-  }
-  console.log('・゚✧ bot is now working.');
 })();
+
+async function isNotificationNeeded() {
+    const failedActionRuns = await 
fetch('https://api.github.com/repos/apache/echarts-bot/actions/workflows/8490751/runs?status=failure&per_page=1')
+        .then(res => res.json());
+    const latestFailedRun = failedActionRuns.workflow_runs[0];
+    if (!latestFailedRun) {
+        return true
+    }
+    const lastFailedTs = Date.parse(latestFailedRun.run_started_at);
+    // send notification when the failure last for one hour and more
+    return Date.now() - lastFailedTs >= 60 * 60 * 1e3;
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to