4 changed files with 141 additions and 4 deletions
@ -0,0 +1,53 @@ |
|||
package app.services; |
|||
|
|||
import app.services.db.DBService; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.time.Instant; |
|||
import java.util.HashMap; |
|||
|
|||
@Service |
|||
public class ExternalServices { |
|||
private HashMap<String, Long> services; |
|||
private DBService dbService; |
|||
private long last_db_update = 0L; |
|||
private final long refresh = 60L; |
|||
|
|||
@Autowired |
|||
ExternalServices(DBService dbService) { |
|||
this.dbService = dbService; |
|||
this.services = new HashMap<>(); |
|||
} |
|||
|
|||
public HashMap<String, Boolean> getServices() { |
|||
update(); |
|||
HashMap<String, Boolean> s = new HashMap<>(); |
|||
services.forEach((key, utime) -> { |
|||
if (Instant.now().getEpochSecond() - utime < refresh) s.put(key, true); |
|||
else s.put(key, false); |
|||
}); |
|||
return s; |
|||
} |
|||
|
|||
public void update() { |
|||
if (Instant.now().getEpochSecond() - last_db_update < refresh / 3) |
|||
return; |
|||
|
|||
try { |
|||
services.put("db_main", dbService.getMainServerTime()); |
|||
} catch (Exception e) { |
|||
services.put("db_main", 0L); |
|||
} |
|||
|
|||
try { |
|||
services.put("db_replica", dbService.getReplicaServerTime()); |
|||
} catch (Exception e) { |
|||
services.put("db_replica", 0L); |
|||
} |
|||
} |
|||
|
|||
public void update(String name) { |
|||
services.put(name, Instant.now().getEpochSecond()); |
|||
} |
|||
} |
@ -0,0 +1,67 @@ |
|||
package app.websocket.handlers; |
|||
|
|||
import app.annotations.enums.AuthMethod; |
|||
import app.annotations.interfaces.CheckWebAccess; |
|||
import app.services.ExternalServices; |
|||
import app.websocket.BaseWebsocketHandler; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.socket.CloseStatus; |
|||
import org.springframework.web.socket.TextMessage; |
|||
import org.springframework.web.socket.WebSocketSession; |
|||
import org.springframework.web.socket.handler.TextWebSocketHandler; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
@Component |
|||
public class ExternalServicesHandler extends TextWebSocketHandler implements BaseWebsocketHandler { |
|||
|
|||
private final Logger logger = LoggerFactory.getLogger(getClass()); |
|||
private ObjectMapper objectMapper = new ObjectMapper(); |
|||
|
|||
private ExternalServices externalServices; |
|||
|
|||
@Autowired |
|||
ExternalServicesHandler(ExternalServices externalServices) { |
|||
this.externalServices = externalServices; |
|||
} |
|||
|
|||
@Override |
|||
public String getPath() { |
|||
return "services"; |
|||
} |
|||
|
|||
@Override |
|||
@CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) |
|||
public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
|||
logger.info("Session {} open", session.getId()); |
|||
super.afterConnectionEstablished(session); |
|||
if (session.isOpen()) { |
|||
logger.info("Session {} open", session.getId()); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
@CheckWebAccess(auth_method = AuthMethod.SECRET_KEY) |
|||
public void handleTextMessage(WebSocketSession session, TextMessage message) { |
|||
if (!session.isOpen()) return; |
|||
HashMap<String, Object> payload; |
|||
try { |
|||
payload = objectMapper.readValue(message.getPayload(), HashMap.class); |
|||
} catch (Exception e) { |
|||
logger.error("Cannot explode payload: {}", e.getMessage()); |
|||
return; |
|||
} |
|||
String service_name = (String) payload.get("name"); |
|||
externalServices.update(service_name); |
|||
} |
|||
|
|||
@Override |
|||
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { |
|||
logger.info("Session {} close", session.getId()); |
|||
super.afterConnectionClosed(session, status); |
|||
} |
|||
} |
Loading…
Reference in new issue