Skip to content

Commit 06e10aa

Browse files
author
Boris
committed
Возвращаем значение waitgreen в true через 3 часа
1 parent 8682079 commit 06e10aa

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

modules/wg/wg.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package wg
22

33
import (
44
"encoding/json"
5-
65
"log"
76
"net/http"
8-
7+
"time"
98
"waitgreen/modules/config"
109

1110
"github.com/uzhinskiy/lib.go/helpers"
@@ -20,12 +19,16 @@ type apiRequest struct {
2019
}
2120

2221
var wgEnabled bool
22+
var wgSetTime = time.Now()
23+
24+
var resetTime = 3 * time.Hour
2325

2426
func Run(cnf config.Config) {
2527
wg := WaitGreen{}
2628
wg.conf = cnf
2729

2830
wgEnabled = cnf.App.DefaultWG
31+
go cleanup()
2932

3033
http.HandleFunc("/", wg.ApiHandler)
3134
http.ListenAndServe(cnf.App.Bind+":"+cnf.App.Port, nil)
@@ -63,6 +66,7 @@ func (wg *WaitGreen) ApiHandler(w http.ResponseWriter, r *http.Request) {
6366
}
6467

6568
wgEnabled = request.WaitGreen
69+
wgSetTime = time.Now()
6670

6771
resp := map[string]interface{}{
6872
"status": http.StatusOK,
@@ -95,3 +99,18 @@ func httpJsonError(w http.ResponseWriter, errorText string, errorCode int) {
9599
j, _ := json.Marshal(resp)
96100
w.Write(j)
97101
}
102+
103+
func cleanup() {
104+
for {
105+
now := time.Now()
106+
if !wgEnabled {
107+
if diff := now.Sub(wgSetTime); diff > cutoff {
108+
log.Printf("Set wgEnabled to true at %s", diff)
109+
wgEnabled = true
110+
wgSetTime = time.Now()
111+
}
112+
}
113+
// do some job
114+
time.Sleep(30 * time.Second)
115+
}
116+
}

0 commit comments

Comments
 (0)