feat: catch kill signals

This commit is contained in:
debugtalk
2022-11-29 23:34:56 +08:00
parent 11baeb415f
commit 99fab3ae59
3 changed files with 24 additions and 26 deletions
-1
View File
@@ -45,7 +45,6 @@ var rootCmd = &cobra.Command{
} }
wc.Start() wc.Start()
wc.DumpResult()
return nil return nil
}, },
} }
+24 -23
View File
@@ -6,9 +6,11 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"syscall"
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -87,7 +89,6 @@ type timeLog struct {
type WorldCupLive struct { type WorldCupLive struct {
driver *uixt.DriverExt driver *uixt.DriverExt
done chan bool
file *os.File file *os.File
resultDir string resultDir string
UUID string `json:"uuid"` UUID string `json:"uuid"`
@@ -224,32 +225,31 @@ func (wc *WorldCupLive) EnterLive(bundleID string) error {
} }
func (wc *WorldCupLive) Start() { func (wc *WorldCupLive) Start() {
wc.done = make(chan bool) c := make(chan os.Signal, 1)
go func() { signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
for { timer := time.NewTimer(time.Duration(wc.Duration) * time.Second)
select { for {
case <-wc.done: select {
return case <-timer.C:
default: wc.dumpResult()
utcTime := time.Now() return
if utcTime.Unix()%int64(wc.Interval) == 0 { case <-c:
wc.getCurrentLiveTime(utcTime) wc.dumpResult()
} else { return
time.Sleep(500 * time.Millisecond) default:
} utcTime := time.Now()
if utcTime.Unix()%int64(wc.Interval) == 0 {
wc.getCurrentLiveTime(utcTime)
} else {
time.Sleep(500 * time.Millisecond)
} }
} }
}() }
time.Sleep(time.Duration(wc.Duration) * time.Second)
wc.Stop()
} }
func (wc *WorldCupLive) Stop() { func (wc *WorldCupLive) dumpResult() error {
wc.EndTime = time.Now().Format("2006-01-02 15:04:05") wc.EndTime = time.Now().Format("2006-01-02 15:04:05")
wc.done <- true
}
func (wc *WorldCupLive) DumpResult() error {
// init json encoder // init json encoder
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer) encoder := json.NewEncoder(buffer)
@@ -263,11 +263,12 @@ func (wc *WorldCupLive) DumpResult() error {
return err return err
} }
filename := filepath.Join(wc.resultDir, "summary.json") path := filepath.Join(wc.resultDir, "summary.json")
err = os.WriteFile(filename, buffer.Bytes(), 0o755) err = os.WriteFile(path, buffer.Bytes(), 0o755)
if err != nil { if err != nil {
log.Error().Err(err).Msg("dump json path failed") log.Error().Err(err).Msg("dump json path failed")
return err return err
} }
log.Info().Str("path", path).Msg("dump summary success")
return nil return nil
} }
-2
View File
@@ -34,7 +34,6 @@ func TestMainIOS(t *testing.T) {
wc := NewWorldCupLive(device, "", bundleID, 30, 10) wc := NewWorldCupLive(device, "", bundleID, 30, 10)
wc.EnterLive(bundleID) wc.EnterLive(bundleID)
wc.Start() wc.Start()
wc.DumpResult()
} }
func TestMainAndroid(t *testing.T) { func TestMainAndroid(t *testing.T) {
@@ -43,5 +42,4 @@ func TestMainAndroid(t *testing.T) {
wc := NewWorldCupLive(device, "", bundleID, 30, 10) wc := NewWorldCupLive(device, "", bundleID, 30, 10)
wc.EnterLive(bundleID) wc.EnterLive(bundleID)
wc.Start() wc.Start()
wc.DumpResult()
} }