|
23 | 23 |
|
24 | 24 | @Service |
25 | 25 | public class SystemMonitorService implements MonitorService { |
26 | | - @Autowired |
27 | | - ServiceRepo serviceRepo; |
28 | | - |
29 | | - @Autowired |
30 | | - HttpUtility httpUtility; |
31 | | - |
32 | | - @Autowired |
33 | | - ObjectMapper objectMapper; |
34 | | - |
35 | | - private static final String SUCCESS_MESSAGE = "All services are working."; |
36 | | - private static final String ERROR_MESSAGE = "Some services are experiencing problems."; |
37 | | - |
38 | | - private Logger logger = LoggerFactory.getLogger(this.getClass()); |
39 | | - |
40 | | - @Override |
41 | | - public void updateAll() { |
42 | | - logger.debug("Monitor Service is checking the status of the managed services"); |
43 | | - //get all monitor-able services from ServiceRepo |
44 | | - serviceRepo.findByIsAuto(true).forEach(service -> { |
45 | | - try { |
46 | | - Status serviceStatus = getServiceStatus(service.getServiceUrl()); |
47 | | - //update the service status if it's changed |
48 | | - logger.debug("The status reported by ["+service.getServiceUrl()+"] of ["+service.getName()+"] is: "+serviceStatus); |
49 | | - if (serviceStatus != service.getStatus()) { |
50 | | - logger.debug("Updating the status of ["+service.getName()+"] to: "+serviceStatus); |
51 | | - service.setStatus(serviceStatus); |
52 | | - serviceRepo.save(service); |
53 | | - } |
54 | | - } catch(MalformedURLException e) { |
55 | | - logger.error("Did not check the status of ["+service.getName()+"] due to a malformed URL: "+service.getServiceUrl()); |
56 | | - } catch(IOException e) { |
57 | | - logger.error("Attempt to check the status of ["+service.getName()+"] failed due to an IOException"); |
58 | | - } |
59 | | - }); |
60 | | - } |
61 | | - |
62 | | - public OverallStatus getOverallStatus() { |
63 | | - Long downCount = serviceRepo.countByStatus(Status.DOWN); |
64 | | - if (downCount == 0) { |
65 | | - return new OverallStatus(SUCCESS, SUCCESS_MESSAGE); |
66 | | - } |
67 | | - return new OverallStatus(ERROR, ERROR_MESSAGE); |
68 | | - } |
69 | | - |
70 | | - public OverallStatus getOverallStatusPublic() { |
71 | | - Long downCount = serviceRepo.countByStatusAndIsPublic(Status.DOWN,true); |
72 | | - if (downCount == 0) { |
73 | | - return new OverallStatus(SUCCESS, SUCCESS_MESSAGE); |
74 | | - } |
75 | | - return new OverallStatus(ERROR, ERROR_MESSAGE); |
76 | | - } |
77 | | - protected Status getServiceStatus(String serviceUrl) throws MalformedURLException,IOException { |
78 | | - String rawStatusResponse = httpUtility.makeHttpRequest(serviceUrl, "GET"); |
79 | | - List<Map<String,String>> mappedStatusResponse = objectMapper.readValue(rawStatusResponse, new TypeReference<List<Map<String, String>>>(){}); |
80 | | - return Status.valueOf(mappedStatusResponse.get(0).get("service").toUpperCase()); |
81 | | - } |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private ServiceRepo serviceRepo; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private HttpUtility httpUtility; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private ObjectMapper objectMapper; |
| 35 | + |
| 36 | + private static final String SUCCESS_MESSAGE = "All services are working."; |
| 37 | + |
| 38 | + private static final String ERROR_MESSAGE = "Some services are experiencing problems."; |
| 39 | + |
| 40 | + private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 41 | + |
| 42 | + @Override |
| 43 | + public void updateAll() { |
| 44 | + logger.debug("Monitor Service is checking the status of the managed services"); |
| 45 | + // get all monitor-able services from ServiceRepo |
| 46 | + serviceRepo.findByIsAuto(true).forEach(service -> { |
| 47 | + try { |
| 48 | + Status serviceStatus = getServiceStatus(service.getServiceUrl()); |
| 49 | + // update the service status if it's changed |
| 50 | + logger.debug("The status reported by [" + service.getServiceUrl() + "] of [" + service.getName() + "] is: " + serviceStatus); |
| 51 | + if (serviceStatus != service.getStatus()) { |
| 52 | + logger.debug("Updating the status of [" + service.getName() + "] to: " + serviceStatus); |
| 53 | + service.setStatus(serviceStatus); |
| 54 | + serviceRepo.save(service); |
| 55 | + } |
| 56 | + } catch (MalformedURLException e) { |
| 57 | + logger.error("Did not check the status of [" + service.getName() + "] due to a malformed URL: " + service.getServiceUrl()); |
| 58 | + } catch (IOException e) { |
| 59 | + logger.error("Attempt to check the status of [" + service.getName() + "] failed due to an IOException"); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + public OverallStatus getOverallStatus() { |
| 65 | + Long downCount = serviceRepo.countByStatus(Status.DOWN); |
| 66 | + if (downCount == 0) { |
| 67 | + return new OverallStatus(SUCCESS, SUCCESS_MESSAGE); |
| 68 | + } |
| 69 | + return new OverallStatus(ERROR, ERROR_MESSAGE); |
| 70 | + } |
| 71 | + |
| 72 | + public OverallStatus getOverallStatusPublic() { |
| 73 | + Long downCount = serviceRepo.countByStatusAndIsPublic(Status.DOWN, true); |
| 74 | + if (downCount == 0) { |
| 75 | + return new OverallStatus(SUCCESS, SUCCESS_MESSAGE); |
| 76 | + } |
| 77 | + return new OverallStatus(ERROR, ERROR_MESSAGE); |
| 78 | + } |
| 79 | + |
| 80 | + protected Status getServiceStatus(String serviceUrl) throws MalformedURLException, IOException { |
| 81 | + String rawStatusResponse = httpUtility.makeHttpRequest(serviceUrl, "GET"); |
| 82 | + List<Map<String, String>> mappedStatusResponse = objectMapper.readValue(rawStatusResponse, new TypeReference<List<Map<String, String>>>() {}); |
| 83 | + return Status.valueOf(mappedStatusResponse.get(0).get("service").toUpperCase()); |
| 84 | + } |
82 | 85 | } |
0 commit comments