-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_locks.go
More file actions
79 lines (69 loc) · 1.83 KB
/
Copy pathruntime_locks.go
File metadata and controls
79 lines (69 loc) · 1.83 KB
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
package main
import "fmt"
func (s *Service) beginInstall(serverID int, reinstall bool) error {
if serverID <= 0 {
return fmt.Errorf("invalid server id")
}
label := "install"
if reinstall {
label = "reinstall"
}
if existing, loaded := s.installState.LoadOrStore(serverID, label); loaded {
existingLabel, _ := existing.(string)
if existingLabel == "" {
existingLabel = "install"
}
return fmt.Errorf("another %s is already running for this server", existingLabel)
}
s.powerStateMu.Lock()
defer s.powerStateMu.Unlock()
if action := s.powerState[serverID]; action != "" {
s.installState.Delete(serverID)
return fmt.Errorf("cannot execute server %s while another power action is running (%s)", label, action)
}
return nil
}
func (s *Service) finishInstall(serverID int) {
if serverID <= 0 {
return
}
s.installState.Delete(serverID)
}
func (s *Service) isInstalling(serverID int) bool {
if serverID <= 0 {
return false
}
_, ok := s.installState.Load(serverID)
return ok
}
func (s *Service) beginPowerAction(serverID int, action string) error {
if serverID <= 0 {
return fmt.Errorf("invalid server id")
}
if s.isInstalling(serverID) {
return fmt.Errorf("cannot execute power action while server install or reinstall is running")
}
s.powerStateMu.Lock()
defer s.powerStateMu.Unlock()
if existing := s.powerState[serverID]; existing != "" {
return fmt.Errorf("another power action is already running (%s)", existing)
}
s.powerState[serverID] = action
return nil
}
func (s *Service) finishPowerAction(serverID int) {
if serverID <= 0 {
return
}
s.powerStateMu.Lock()
delete(s.powerState, serverID)
s.powerStateMu.Unlock()
}
func (s *Service) isPowerActionRunning(serverID int) bool {
if serverID <= 0 {
return false
}
s.powerStateMu.Lock()
defer s.powerStateMu.Unlock()
return s.powerState[serverID] != ""
}