From 3f989d77cfddfcb03f7f643e53aaa3d75b8631d4 Mon Sep 17 00:00:00 2001 From: gsd Date: Wed, 24 Apr 2024 16:35:50 +0300 Subject: [PATCH] online updater --- .../java/app/controllers/other/PulseController.java | 11 ++++++++++- src/main/java/app/updates/OnlineUpdater.java | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/app/controllers/other/PulseController.java b/src/main/java/app/controllers/other/PulseController.java index 1f59f26..aec2197 100644 --- a/src/main/java/app/controllers/other/PulseController.java +++ b/src/main/java/app/controllers/other/PulseController.java @@ -1,6 +1,7 @@ package app.controllers.other; import app.services.db.DBService; +import app.updates.OnlineUpdater; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -13,14 +14,22 @@ import org.springframework.web.bind.annotation.RestController; public class PulseController { DBService dbService; + OnlineUpdater onlineUpdater; @Autowired - public PulseController(DBService dbService) { + public PulseController(DBService dbService, + OnlineUpdater onlineUpdater) { this.dbService = dbService; + this.onlineUpdater = onlineUpdater; } @GetMapping("/db") public ResponseEntity getDBPulse() { return new ResponseEntity(dbService.getDBServerTime(), HttpStatus.OK); } + + @GetMapping("/db/diff") + public ResponseEntity getDiff() { + return new ResponseEntity<>(onlineUpdater.getDifferentReplica(), HttpStatus.OK); + } } diff --git a/src/main/java/app/updates/OnlineUpdater.java b/src/main/java/app/updates/OnlineUpdater.java index ad1dfc5..7377a0e 100644 --- a/src/main/java/app/updates/OnlineUpdater.java +++ b/src/main/java/app/updates/OnlineUpdater.java @@ -20,6 +20,11 @@ public class OnlineUpdater extends BaseUpdater { @Autowired @Qualifier("jt_rw") private JdbcTemplate jdbcTemplate; + + @Autowired + @Qualifier("jt_ro") + private JdbcTemplate jdbcTemplate_ro; + private final static int sleep_time = 30 * 1000; @Autowired @@ -45,6 +50,12 @@ public class OnlineUpdater extends BaseUpdater { return true; } + public long getDifferentReplica() { + Long rw = jdbcTemplate.query("SELECT COUNT(*) FROM `servers_online`", (rs, n) -> rs.getLong(1)).get(0); + Long ro = jdbcTemplate.query("SELECT COUNT(*) FROM `servers_online`", (rs, n) -> rs.getLong(1)).get(0); + return Math.abs(rw - ro); + } + //CREATE TABLE `tf2.facti13`.`servers_online` (`id` INT NOT NULL AUTO_INCREMENT , `utime` INT NOT NULL , `player_count` INT NOT NULL , `max_players` INT NOT NULL , `server_id` VARCHAR(32) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB; public class ServerOnline { private int id;