-
Notifications
You must be signed in to change notification settings - Fork 19
/
idle_state.go
65 lines (50 loc) · 1.35 KB
/
idle_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
/*
* UpdateHub
* Copyright (C) 2017
* O.S. Systems Sofware LTDA: contato@ossystems.com.br
*
* SPDX-License-Identifier: Apache-2.0
*/
package updatehub
import (
"time"
"github.com/OSSystems/pkg/log"
)
// IdleState is the State interface implementation for the UpdateHubStateIdle
type IdleState struct {
BaseState
CancellableState
}
// ID returns the state id
func (state *IdleState) ID() UpdateHubState {
return state.id
}
// Cancel cancels a state if it is cancellable
func (state *IdleState) Cancel(ok bool, nextState State) bool {
return state.CancellableState.Cancel(ok, nextState)
}
// Handle for IdleState
func (state *IdleState) Handle(uh *UpdateHub) (State, bool) {
if !uh.Settings.PollingEnabled {
state.Wait()
return state.NextState(), false
}
uh.IgnoreProbeASAP = false
now := time.Now()
if uh.Settings.ExtraPollingInterval > 0 {
extraPollTime := uh.Settings.LastPoll.Add(uh.Settings.ExtraPollingInterval)
log.Info("ExtraPoll received: ", extraPollTime)
if extraPollTime.Before(now) {
return NewProbeState(uh.DefaultApiClient), false
}
}
return NewPollState(uh.Settings.PollingInterval), false
}
// NewIdleState creates a new IdleState
func NewIdleState() *IdleState {
state := &IdleState{
BaseState: BaseState{id: UpdateHubStateIdle},
CancellableState: CancellableState{cancel: make(chan bool)},
}
return state
}