RedisMonitorJob implements Job { @Override public void execute(JobContext context) { var message = new StatMessage(); Instant now = Instant.now(); message.id = LogManager.ID_GENERATOR.next(now); message.date = now; message.app = app; message.host = host; try { Map<String, String> info = redis.admin().info(); Stats stats = stats(info); message.result = stats.result(); message.stats = stats.stats; message.errorCode = stats.errorCode; message.errorMessage = stats.errorMessage; } catch (Throwable e) { logger.warn(e.getMessage(), e); message.result = "ERROR"; message.errorCode = "FAILED_TO_COLLECT"; message.errorMessage = e.getMessage(); message.info = Map.of("stack_trace", Exceptions.stackTrace(e)); } publisher.publish(message); } RedisMonitorJob(Redis redis, String app, String host, MessagePublisher<StatMessage> publisher); @Override void execute(JobContext context); public double highMemUsageThreshold; }
@Test void publishError() { when(redis.admin()).thenThrow(new Error("mock")); job.execute(null); verify(publisher).publish(argThat(message -> "redis".equals(message.app) && "ERROR".equals(message.result) && "FAILED_TO_COLLECT".equals(message.errorCode))); }