22
22
23
23
import java .util .ArrayList ;
24
24
import java .util .List ;
25
- import java .util .Objects ;
26
25
import java .util .Optional ;
27
26
27
+ import static java .lang .String .format ;
28
28
import static java .util .Objects .requireNonNull ;
29
29
30
30
public class HaGatewayManager
@@ -83,24 +83,37 @@ public Optional<ProxyBackendConfiguration> getBackendByName(String name)
83
83
@ Override
84
84
public void deactivateBackend (String backendName )
85
85
{
86
- GatewayBackend model = dao .findFirstByName (backendName );
87
- if (model != null ) {
88
- Boolean prevStatus = model .active ();
89
- dao .deactivate (backendName );
90
- log .info ("Backend cluster %s has been deactivated (previous status: active=%s)." ,
91
- backendName , prevStatus );
92
- }
86
+ updateClusterActivationStatus (backendName , false , () -> dao .deactivate (backendName ));
93
87
}
94
88
95
89
@ Override
96
90
public void activateBackend (String backendName )
97
91
{
98
- GatewayBackend model = dao .findFirstByName (backendName );
99
- if (model != null ) {
100
- Boolean prevStatus = model .active ();
101
- dao .activate (backendName );
102
- log .info ("Backend cluster %s has been activated (previous status: active=%s)." ,
103
- backendName , prevStatus );
92
+ updateClusterActivationStatus (backendName , true , () -> dao .activate (backendName ));
93
+ }
94
+
95
+ private void updateClusterActivationStatus (String clusterName , boolean newStatus , Runnable action )
96
+ {
97
+ GatewayBackend model = dao .findFirstByName (clusterName );
98
+ if (model == null ) {
99
+ throw new IllegalStateException (format ("No cluster found with name: %s, could not (de)activate" , clusterName ));
100
+ }
101
+
102
+ boolean prevStatus = model .active ();
103
+ try {
104
+ action .run ();
105
+ logActivationStatusChange (clusterName , newStatus , prevStatus );
106
+ }
107
+ catch (Exception e ) {
108
+ throw e ;
109
+ }
110
+ }
111
+
112
+ private void logActivationStatusChange (String clusterName , boolean newStatus , boolean prevStatus )
113
+ {
114
+ if (prevStatus != newStatus ) {
115
+ log .info ("Backend cluster %s activation status set to active=%s (previous status: active=%s)." ,
116
+ clusterName , newStatus , prevStatus );
104
117
}
105
118
}
106
119
@@ -123,13 +136,8 @@ public ProxyBackendConfiguration updateBackend(ProxyBackendConfiguration backend
123
136
dao .create (backend .getName (), backend .getRoutingGroup (), backendProxyTo , backendExternalUrl , backend .isActive ());
124
137
}
125
138
else {
126
- Boolean prevStatus = model .active ();
127
139
dao .update (backend .getName (), backend .getRoutingGroup (), backendProxyTo , backendExternalUrl , backend .isActive ());
128
- Boolean currStatus = backend .isActive ();
129
- if (!Objects .equals (prevStatus , currStatus )) {
130
- log .info ("Backend cluster %s activation status changed to active=%s (previous status: active=%s)." ,
131
- backend .getName (), currStatus , prevStatus );
132
- }
140
+ logActivationStatusChange (backend .getName (), backend .isActive (), model .active ());
133
141
}
134
142
return backend ;
135
143
}
0 commit comments