Query q = entityManager.createNamedQuery(Agent.QUERY_FIND_ALL_SUSPECT_AGENTS);
q.setParameter("dateThreshold", nowEpoch - maximumQuietTimeAllowed);
records = q.getResultList();
ServerCommunicationsServiceMBean serverComm = null;
for (AgentLastAvailabilityPingComposite record : records) {
long lastReport = record.getLastAvailabilityPing();
long timeSinceLastReport = nowEpoch - lastReport;
// Only show this message a few times so we do not flood the log with the same message if the agent is down a long time
// we show it as soon as we detect it going down (within twice max quiet time allowed) or we show it
// after every 6th hour it's detected to be down (again, within twice max quiet time). Effectively, you'll see
// this message appear about 4 times per 6-hours for a downed agent if using the default max quiet time.
// Note that in here we also make sure the agent client is shutdown. We do it here because, even if the agent
// was already backfilled, we still want to do this in case somehow the client was started again.
if ((timeSinceLastReport % 21600000L) < (maximumQuietTimeAllowed * 2L)) {
if (serverComm == null) {
serverComm = ServerCommunicationsServiceUtil.getService();
}
serverComm.removeDownedAgent(record.getRemoteEndpoint());
}
// we can avoid doing this over and over again for agents that are down a long time by seeing if it's
// already backfilled. Note that we do not log the above warn message down here in this if-statement,
// because I think we still want to log that we think an agent is down periodically.