This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
226 lines (195 loc) · 5.25 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// MIT License
//
// Copyright (c) 2020 Lack
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package update
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"sync"
"time"
log "github.com/lack-io/vine/lib/logger"
"github.com/lack-io/vine/lib/runtime"
)
var (
// DefaultTick defines how often we poll for updates
DefaultTick = 1 * time.Minute
// DefaultURL defines url to poll for updates
DefaultURL = "https://go.vine.mu/update"
)
// Build is service build
type Build struct {
// Commit is git commit sha
Commit string `json:"commit,omitempty"`
// Image is Docker build timestamp
Image string `json:"image,omitempty"`
// Release is vine release tag
Release string `json:"release,omitempty"`
}
// notifier is http notifier
type notifier struct {
sync.RWMutex
// url to poll for updates
url string
// poll time to check for updates
tick time.Duration
// version is current version
version time.Time
// events is notifications channel
events chan runtime.Event
// indicates if we're running
running bool
// used to stop the runtime
closed chan bool
}
// NewScheduler returns new runtime notifier
func NewScheduler(updateURL, buildDate string) runtime.Scheduler {
// convert the build date to a time.Time value
timestamp, err := strconv.ParseInt(buildDate, 10, 64)
if err != nil {
timestamp = time.Now().Unix()
}
// the current version
version := time.Unix(timestamp, 0)
// return a new notifier
return newScheduler(updateURL, DefaultTick, version)
}
// NewHTTP creates HTTP poller and returns it
func newScheduler(url string, tick time.Duration, version time.Time) *notifier {
return ¬ifier{
url: url,
tick: tick,
version: version,
closed: make(chan bool),
}
}
// Poll polls for updates and returns results
func (h *notifier) poll() (*Build, error) {
// this should not return error, but lets make sure.
url, err := url.Parse(h.url)
if err != nil {
return nil, err
}
rsp, err := http.Get(url.String())
if err != nil {
log.Debugf("Scheduler error polling updates: %v", err)
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode != 200 {
log.Debugf("Scheduler error unexpected http response: %v", rsp.StatusCode)
return nil, err
}
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
log.Debugf("Scheduler error reading http response: %v", err)
return nil, err
}
// encoding format is assumed to be json
var build *Build
if err := json.Unmarshal(b, &build); err != nil {
log.Debugf("Scheduler error unmarshalling response: %v", err)
return nil, err
}
return build, nil
}
// run runs the notifier
func (h *notifier) run() {
t := time.NewTicker(h.tick)
defer t.Stop()
for {
select {
case <-h.closed:
return
case <-t.C:
log.Debugf("Scheduler polling for new update: %s", h.url)
resp, err := h.poll()
if err != nil {
log.Debugf("Scheduler error polling for updates: %v", err)
continue
}
if resp == nil {
log.Debug("Poll response is empty")
continue
}
// parse returned response to timestamp
buildTime, err := time.Parse(time.RFC3339, resp.Image)
if err != nil {
log.Debugf("Scheduler error parsing build time: %v", err)
continue
}
// if the latest build is newer than the current emit Update event
if !buildTime.After(h.version) {
continue
}
// fire the event
h.events <- runtime.Event{
// new update
Type: runtime.Update,
// timestamp of the update
Timestamp: buildTime,
}
// set the build time
h.version = buildTime
}
}
}
// Notify polls for new build and returns a channel to consume the events
func (h *notifier) Notify() (<-chan runtime.Event, error) {
h.Lock()
defer h.Unlock()
// already running
if h.running {
return h.events, nil
}
// set running
h.running = true
h.closed = make(chan bool)
h.events = make(chan runtime.Event)
// runt the notifier
go h.run()
return h.events, nil
}
// Close stops the notifier
func (h *notifier) Close() error {
h.Lock()
defer h.Unlock()
if !h.running {
return nil
}
select {
case <-h.closed:
return nil
default:
close(h.closed)
// stop the event stream
close(h.events)
// set not running
h.running = false
}
return nil
}
// String implements tringer interface
func (h *notifier) String() string {
return "default"
}