private void mail(JobExecutionContext jec, Set emails, String subject, String mail) { try { log.info("Sending mail to service admins: " + Arrays.toString(emails.toArray())); for (String email : (Set<String>) emails) { if (isInterrupted()) { log.info("Interrupted, ending monitoring job"); return; } try { Mailer.sendMail( (String) jec.getJobDetail().getJobDataMap().get("from.name"), (String) jec.getJobDetail().getJobDataMap().get("from.email"), email, subject, mail); } catch (Exception e) { log.error("Error sending mail to service admin " + email, e); } } } catch (Exception e) { log.error("Error sending mail to service admins", e); } }
public void execute(JobExecutionContext jec) throws JobExecutionException { try { Stripersist.requestInit(); EntityManager em = Stripersist.getEntityManager(); StringBuilder monitoringFailures = new StringBuilder(); int online = 0, offline = 0; // TODO: where monitoringEnabled = true... for (GeoService gs : (List<GeoService>) em.createQuery("from GeoService").getResultList()) { String debugMsg = String.format( "%s service %s (#%d) with URL: %s", gs.getProtocol(), gs.getName(), gs.getId(), gs.getUrl()); try { if (isInterrupted()) { log.info("Interrupted, ending monitoring job"); return; } gs.checkOnline(); online++; gs.setMonitoringStatusOK(true); log.debug("ONLINE: " + debugMsg); } catch (Exception e) { gs.setMonitoringStatusOK(false); offline++; log.debug("OFFLINE: " + debugMsg); if (log.isTraceEnabled()) { log.trace("Exception", e); } String message = e.toString(); Throwable cause = e.getCause(); while (cause != null) { message += "; " + cause.toString(); cause = cause.getCause(); } monitoringFailures.append( String.format( "%s service %s (#%d)\nURL: %s\nFout: %s\n\n", gs.getProtocol(), gs.getName(), gs.getId(), gs.getUrl(), message)); } } em.getTransaction().commit(); log.info( String.format( "Total services %d, online: %d, offline: %d, runtime: %d s", online + offline, online, offline, jec.getJobRunTime() / 1000)); if (offline > 0) { Set emails = new HashSet(); for (User admin : (List<User>) em.createQuery( "select u from User u " + "join u.groups g " + "where g.name = '" + Group.SERVICE_ADMIN + "' ") .getResultList()) { emails.add(admin.getDetails().get(User.DETAIL_EMAIL)); } emails.remove(null); if (!emails.isEmpty()) { StringBuilder mail = new StringBuilder(); SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyy HH:mm:ss"); mail.append( String.format( "Bij een controle op %s zijn in het gegevensregister %d services gevonden waarbij fouten zijn geconstateerd.\n" + "\nDe volgende controle zal worden uitgevoerd op %s.\nHieronder staat de lijst met probleemservices:\n\n", f.format(jec.getFireTime()), offline, f.format(jec.getNextFireTime()))); mail.append(monitoringFailures); mail(jec, emails, offline + " services zijn offline bij controle", mail.toString()); } } } catch (Exception e) { log.error("Error", e); } finally { Stripersist.requestComplete(); } }