Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Feat: add notify handler for punch fails (aliyun fc) (#27)
Browse files Browse the repository at this point in the history
* feat: support task fail notify

* ci: add notify builder

* build: bump github.com/yin1999/healthreport to 1.3.4

* fix ci
  • Loading branch information
yin1999 authored Sep 25, 2022
1 parent 0990390 commit c4326da
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# binary file
tencent-serverless.zip
aliyun-serverless.zip
*-serverless.zip

# config file
/.vscode
4 changes: 2 additions & 2 deletions _script/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
}
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion aliyun.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build aliyun || !tencent
//go:build aliyun || !tencent || notify

package main

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
13 changes: 5 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
//go:build !notify

package main

import (
"context"
"encoding/json"
"errors"
"io"
"fmt"
"os"
"strings"
"time"

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 {
Expand Down Expand Up @@ -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])
}
Expand Down
72 changes: 72 additions & 0 deletions notify.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
9 changes: 9 additions & 0 deletions type.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c4326da

Please sign in to comment.