From c4326da2402348471ede52aae26242e76ab0ce80 Mon Sep 17 00:00:00 2001 From: A1lo Date: Sun, 25 Sep 2022 12:16:21 +0800 Subject: [PATCH] Feat: add notify handler for punch fails (aliyun fc) (#27) * feat: support task fail notify * ci: add notify builder * build: bump github.com/yin1999/healthreport to 1.3.4 * fix ci --- .github/workflows/Build.yml | 1 + .github/workflows/Release.yml | 2 +- .gitignore | 3 +- _script/build.go | 4 +- aliyun.go | 2 +- go.mod | 2 +- go.sum | 4 +- main.go | 13 +++---- notify.go | 72 +++++++++++++++++++++++++++++++++++ type.go | 9 +++++ 10 files changed, 95 insertions(+), 17 deletions(-) create mode 100644 notify.go create mode 100644 type.go diff --git a/.github/workflows/Build.yml b/.github/workflows/Build.yml index a971051..ea03b84 100644 --- a/.github/workflows/Build.yml +++ b/.github/workflows/Build.yml @@ -37,6 +37,7 @@ jobs: # spread the work across 2 processes build1=$! go build -tags aliyun + go build -tags notify build2=$! go build -tags tencent diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 956240f..2c839e4 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -29,7 +29,7 @@ jobs: run: | # spread the work across 2 processes build1=$! - go run _script/build.go aliyun + go run _script/build.go aliyun notify build2=$! go run _script/build.go tencent diff --git a/.gitignore b/.gitignore index 5699e2b..a655ac3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ # binary file -tencent-serverless.zip -aliyun-serverless.zip +*-serverless.zip # config file /.vscode diff --git a/_script/build.go b/_script/build.go index fc3279e..2554d04 100644 --- a/_script/build.go +++ b/_script/build.go @@ -11,7 +11,7 @@ import ( func main() { var targets []string if len(os.Args) == 1 { - targets = []string{"aliyun", "tencent"} + targets = []string{"aliyun", "tencent", "notify"} } else { targets = os.Args[1:] } @@ -20,7 +20,7 @@ func main() { for _, target := range targets { var out string switch target { - case "aliyun": + case "aliyun", "notify": out = "main" case "tencent": out = "bootstrap" diff --git a/aliyun.go b/aliyun.go index 6474054..b8ead8f 100644 --- a/aliyun.go +++ b/aliyun.go @@ -1,4 +1,4 @@ -//go:build aliyun || !tencent +//go:build aliyun || !tencent || notify package main diff --git a/go.mod b/go.mod index 10ab8cb..d412b53 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,6 @@ module main go 1.17 -require github.com/yin1999/healthreport v1.3.3 +require github.com/yin1999/healthreport v1.3.4 require github.com/google/go-querystring v1.1.0 // indirect diff --git a/go.sum b/go.sum index 6fb84a0..e850069 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,6 @@ github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/yin1999/healthreport v1.3.3 h1:gwIdsVOZhHUYnbZos1gCKMOVdsP0n/HrMaIvUF9R4hg= -github.com/yin1999/healthreport v1.3.3/go.mod h1:QBX/b/LmKlkJoaNiYNdczSedBK+6tmtHmczTmMwUu2Y= +github.com/yin1999/healthreport v1.3.4 h1:Zdpq9rHYgDkDtIpXeSFTgSKiTeal2fWgc3ZXBupAn4s= +github.com/yin1999/healthreport v1.3.4/go.mod h1:QBX/b/LmKlkJoaNiYNdczSedBK+6tmtHmczTmMwUu2Y= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 0ed13b2..ccc1e5e 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ +//go:build !notify + package main import ( "context" "encoding/json" "errors" - "io" + "fmt" "os" "strings" "time" @@ -12,12 +14,6 @@ import ( client "github.com/yin1999/healthreport/httpclient" ) -type handler interface { - Next() (body io.ReadCloser, reqID string, err error) - ReportError(msg string, id string) - ReportSuccess(id string) -} - func main() { h, err := regist() if err != nil { @@ -64,7 +60,8 @@ func punch(payload string) error { } err := client.Punch(context.Background(), &client.Account{Username: account[0], Password: account[1]}, 30*time.Second) if err != nil { - Error.Log("account: %s punch failed, err: %s\n", account[0], err.Error()) + err = fmt.Errorf("account: %s punch failed, err: %s\n", account[0], err.Error()) + Error.Log(err.Error()) } else { Info.Log("account: %s punch success\n", account[0]) } diff --git a/notify.go b/notify.go new file mode 100644 index 0000000..a773efa --- /dev/null +++ b/notify.go @@ -0,0 +1,72 @@ +//go:build notify + +package main + +import ( + "encoding/json" + "fmt" + "os" + "strconv" + "strings" + "time" + + "github.com/yin1999/healthreport/utils/email" +) + +var emailCfg *email.Config + +func init() { + port, err := strconv.Atoi(os.Getenv("PORT")) + if err != nil { + panic(err) + } + emailCfg = &email.Config{ + To: strings.Split(os.Getenv("TO"), ","), + SMTP: email.SmtpConfig{ + Host: os.Getenv("HOST"), + Port: port, + TLS: os.Getenv("TLS") == "true", + Username: os.Getenv("USERNAME"), + Password: os.Getenv("PASSWORD"), + }, + } +} + +func main() { + h, err := regist() + if err != nil { + os.Exit(1) + } + startServe(h) +} + +type message struct { + ResponsePayload string +} + +func startServe(handler handler) { + for { + body, id, err := handler.Next() + if err != nil { + Error.Log("get trigger payload failed, err: %s\n", err.Error()) + continue + } + t := &message{} + err = json.NewDecoder(body).Decode(t) + body.Close() // close body + if err != nil { + msg := "parse request body failed, err: " + err.Error() + Error.Log(msg + "\n") + handler.ReportError(msg, id) + continue + } + err = emailCfg.Send("打卡状态推送", fmt.Sprintf("打卡状态推送-%s", + time.Now().Format("2006-01-02")), + fmt.Sprintf("打卡失败: %s ", t.ResponsePayload)) + if err != nil { + handler.ReportError(err.Error(), id) + } else { + handler.ReportSuccess(id) + } + } +} diff --git a/type.go b/type.go new file mode 100644 index 0000000..ceae8d1 --- /dev/null +++ b/type.go @@ -0,0 +1,9 @@ +package main + +import "io" + +type handler interface { + Next() (body io.ReadCloser, reqID string, err error) + ReportError(msg string, id string) + ReportSuccess(id string) +}