-
Notifications
You must be signed in to change notification settings - Fork 19
/
probe_state.go
165 lines (128 loc) · 4.05 KB
/
probe_state.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
/*
* UpdateHub
* Copyright (C) 2017
* O.S. Systems Sofware LTDA: contato@ossystems.com.br
*
* SPDX-License-Identifier: Apache-2.0
*/
package updatehub
import (
"net"
"time"
"github.com/OSSystems/pkg/log"
"github.com/UpdateHub/updatehub/client"
"github.com/UpdateHub/updatehub/metadata"
"github.com/pkg/errors"
)
// ProbeState is the State interface implementation for the UpdateHubStateProbe
type ProbeState struct {
BaseState
CancellableState
ProbeResponseReady chan bool
probeUpdateMetadata *metadata.UpdateMetadata
probeExtraPoll time.Duration
}
func (state *ProbeState) ProbeResponse() (*metadata.UpdateMetadata, time.Duration) {
return state.probeUpdateMetadata, state.probeExtraPoll
}
// ID returns the state id
func (state *ProbeState) ID() UpdateHubState {
return state.id
}
// Cancel cancels a state if it is cancellable
func (state *ProbeState) Cancel(ok bool, nextState State) bool {
state.CancellableState.Cancel(ok, nextState)
return ok
}
// Handle for ProbeState executes a ProbeUpdate procedure and
// proceed to download the update if there is one. It goes back to the
// polling state otherwise.
func (state *ProbeState) Handle(uh *UpdateHub) (State, bool) {
var signature []byte
var err error
var nextState State
nextState = state
pollingRetries := uh.Settings.PollingRetries
go func() {
loop:
for {
select {
case <-time.After(time.Second):
state.probeUpdateMetadata, signature, state.probeExtraPoll, err = uh.Controller.ProbeUpdate(state.apiClient, pollingRetries)
if neterr, ok := errors.Cause(err).(net.Error); ok {
if neterr.Timeout() {
log.Warn("timeout during download update")
}
nextState = NewProbeState(uh.DefaultApiClient)
}
break loop
case <-state.cancel:
break loop
}
}
state.Cancel(true, nextState)
}()
state.Wait()
// "non-blocking" write to channel
select {
case state.ProbeResponseReady <- true:
default:
}
// Increment the number of polling retries
uh.Settings.PollingRetries++
defer uh.Settings.Save(uh.Store)
// state cancelled
if state.NextState() != state {
return state.NextState(), true
}
// Reset polling retries and disable ASAP mode in case of extra polling
if state.probeExtraPoll > 0 {
uh.Settings.PollingRetries = 0
uh.Settings.ProbeASAP = false
}
uh.Settings.LastPoll = time.Now()
uh.Settings.ExtraPollingInterval = 0
if state.probeUpdateMetadata != nil {
// Reset polling retries and disable ASAP mode in case of ProbeUpdate success
uh.Settings.PollingRetries = 0
uh.Settings.ProbeASAP = false
packageUID := state.probeUpdateMetadata.PackageUID()
if packageUID == uh.lastInstalledPackageUID {
return NewIdleState(), false
}
if !state.probeUpdateMetadata.VerifySignature(uh.PublicKey, signature) {
err := errors.New("invalid signature")
return NewErrorState(state.apiClient, state.probeUpdateMetadata, NewTransientError(err)), false
}
pendingDownload, err := uh.hasPendingDownload(state.probeUpdateMetadata)
if !pendingDownload && err == nil {
return NewInstallingState(state.apiClient, state.probeUpdateMetadata, &ProgressTrackerImpl{}, uh.Store), false
}
return NewDownloadingState(state.apiClient, state.probeUpdateMetadata, &ProgressTrackerImpl{}), false
}
if state.probeExtraPoll > 0 {
now := time.Now()
nextPoll := time.Unix(uh.Settings.FirstPoll.Unix(), 0)
probeExtraPollTime := now.Add(state.probeExtraPoll)
for nextPoll.Before(now) {
nextPoll = nextPoll.Add(uh.Settings.PollingInterval)
}
if probeExtraPollTime.Before(nextPoll) {
uh.Settings.ExtraPollingInterval = state.probeExtraPoll
poll := NewPollState(uh.Settings.PollingInterval)
poll.interval = state.probeExtraPoll
return poll, false
}
}
return NewIdleState(), false
}
// NewProbeState creates a new ProbeState
func NewProbeState(apiClient *client.ApiClient) *ProbeState {
state := &ProbeState{
BaseState: BaseState{id: UpdateHubStateProbe},
CancellableState: CancellableState{cancel: make(chan bool, 1)},
}
state.apiClient = apiClient
state.ProbeResponseReady = make(chan bool, 1)
return state
}