11package main
22
33import (
4+ "bytes"
45 "encoding/json"
56 "fmt"
7+ "net/http"
68 "os"
9+ "sync"
10+ "time"
711
812 "github.com/sirupsen/logrus"
913)
@@ -22,6 +26,9 @@ type config struct {
2226
2327 // DeviceIPv4 网卡ipv4地址
2428 DeviceIPv4 string
29+
30+ mu sync.Mutex
31+ isTaskRunning bool
2532}
2633
2734var configuration = config {}
@@ -47,8 +54,95 @@ func (c *config) init() {
4754 logrus .Fatalf ("%s: this device has no ipv4 address." , c .Device )
4855 }
4956
57+ // 启动后台去执行一些自动更新的动作
58+ go func () {
59+ logrus .Info ("configuration background started." )
60+ for {
61+ c .setIsTaskRunning ()
62+ c .reportDepolyed ()
63+ time .Sleep (5 * time .Second )
64+ }
65+ }()
66+
5067 logrus .WithField (
5168 "configuration" ,
5269 fmt .Sprintf ("%+v" , configuration ),
5370 ).Debug ("configuration initialized." )
5471}
72+
73+ func (c * config ) getIsTaskRunning () bool {
74+ c .mu .Lock ()
75+ defer c .mu .Unlock ()
76+ return c .isTaskRunning
77+ }
78+
79+ func (c * config ) setIsTaskRunning () {
80+ var err error
81+ var apiResp struct {
82+ Err string `json:"err"`
83+ IsRunning bool `json:"is_running"`
84+ }
85+
86+ logrus .Infof ("[setIsTaskRunning] updating TaskID=%d status..." , c .Taskid )
87+ // call bubblereplay
88+ api := fmt .Sprintf ("http://%s%s/%d" , c .ReplaySvrAddr , ApiTaskStatus , c .Taskid )
89+ resp , err := http .Get (api )
90+ if err != nil {
91+ logrus .Errorf ("[setIsTaskRunning] call api failed, %s" , err )
92+ return
93+ }
94+
95+ err = json .NewDecoder (resp .Body ).Decode (& apiResp )
96+ if err != nil {
97+ logrus .Errorf ("[setIsTaskRunning] decode json response failed, %s" , err )
98+ return
99+ }
100+ resp .Body .Close ()
101+
102+ if apiResp .Err != "" {
103+ logrus .Errorf ("[setIsTaskRunning] response return error, %s" , err )
104+ return
105+ }
106+
107+ // update state
108+ c .mu .Lock ()
109+ defer c .mu .Unlock ()
110+ c .isTaskRunning = apiResp .IsRunning
111+ }
112+
113+ func (c * config ) reportDepolyed () {
114+ var err error
115+ var apiBody = struct {
116+ TaskID int64 `json:"task_id"`
117+ // Addr 基准服务地址
118+ Addr string `json:"addr"`
119+ }{
120+ TaskID : c .Taskid ,
121+ Addr : c .DeviceIPv4 ,
122+ }
123+ var apiResp struct {
124+ Err string `json:"err"`
125+ }
126+
127+ logrus .Infof ("[reportDepolyed] reporting TaskID=%d has been deployed the bubblecopy..." , c .Taskid )
128+ // call bubblereplay
129+ api := fmt .Sprintf ("http://%s%s" , c .ReplaySvrAddr , ApiSetDeployed )
130+ data , err := json .Marshal (apiBody )
131+ resp , err := http .Post (api , "application/json" , bytes .NewReader (data ))
132+ if err != nil {
133+ logrus .Errorf ("[reportDepolyed] call api failed, %s" , err )
134+ return
135+ }
136+
137+ err = json .NewDecoder (resp .Body ).Decode (& apiResp )
138+ if err != nil {
139+ logrus .Errorf ("[reportDepolyed] decode json response failed, %s" , err )
140+ return
141+ }
142+ resp .Body .Close ()
143+
144+ if apiResp .Err != "" {
145+ logrus .Errorf ("[reportDepolyed] response return error, %s" , err )
146+ return
147+ }
148+ }
0 commit comments