-
Notifications
You must be signed in to change notification settings - Fork 13
/
simple_state_waiter.go
103 lines (86 loc) · 3.08 KB
/
simple_state_waiter.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
// Copyright 2016-2022 The Libsacloud Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wait
import (
"context"
"time"
"github.com/sacloud/libsacloud/v2/sacloud"
"github.com/sacloud/libsacloud/v2/sacloud/types"
)
// ByFunc デフォルトのパラメータでSimpleStateWaiterを作成して返す
func ByFunc(readStateFunc ReadStateFunc) sacloud.StateWaiter {
return &SimpleStateWaiter{ReadStateFunc: readStateFunc}
}
type ReadStateFunc func() (bool, error)
// SimpleStateWaiter シンプルな待ち処理のためのsacloud.StateWaiterの実装
//
// sacloud.StatePollingWaiterをラップし、シンプルなfuncのみで待つべきかを判定する
type SimpleStateWaiter struct {
// ReadStateFunc 待つべきかの判定func
// trueかつerrorが空の場合は待ち処理を完了させる
ReadStateFunc ReadStateFunc
// Timeout タイムアウト
Timeout time.Duration
// PollingInterval ポーリング間隔
PollingInterval time.Duration
}
func (s *SimpleStateWaiter) waiter() sacloud.StateWaiter {
return &sacloud.StatePollingWaiter{
ReadFunc: func() (interface{}, error) {
result, err := s.ReadStateFunc()
if err != nil {
return false, err
}
return &fakeState{available: result}, nil
},
TargetAvailability: []types.EAvailability{
types.Availabilities.Available,
},
PendingAvailability: []types.EAvailability{
types.Availabilities.Unknown,
},
PollingInterval: s.PollingInterval,
Timeout: s.Timeout,
}
}
// WaitForState sacloud.StateWaiterの実装
func (s *SimpleStateWaiter) WaitForState(ctx context.Context) (interface{}, error) {
return s.waiter().WaitForState(ctx)
}
// AsyncWaitForState sacloud.StateWaiterの実装
func (s *SimpleStateWaiter) AsyncWaitForState(ctx context.Context) (compCh <-chan interface{}, progressCh <-chan interface{}, errorCh <-chan error) {
return s.waiter().AsyncWaitForState(ctx)
}
// SetPollingTimeout sacloud.StateWaiterの実装
func (s *SimpleStateWaiter) SetPollingTimeout(d time.Duration) {
s.waiter().SetPollingTimeout(d)
}
// SetPollingInterval sacloud.StateWaiterの実装
func (s *SimpleStateWaiter) SetPollingInterval(d time.Duration) {
s.waiter().SetPollingInterval(d)
}
type fakeState struct {
available bool
}
// GetAvailability accessor.Availabilityの実装
func (f *fakeState) GetAvailability() types.EAvailability {
if f.available {
return types.Availabilities.Available
}
return types.Availabilities.Unknown
}
// SetAvailability accessor.Availabilityの実装
func (f *fakeState) SetAvailability(v types.EAvailability) {
f.available = v == types.Availabilities.Available
}