Skip to content

Commit

Permalink
#3231: Pull up logger, and always pass AnetConfiguration to constructor
Browse files Browse the repository at this point in the history
Move worker creation closer to where it's scheduled.
Make some variables final.
  • Loading branch information
gjvoosten committed Oct 29, 2020
1 parent 72e6d40 commit 2436698
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 73 deletions.
37 changes: 19 additions & 18 deletions src/main/java/mil/dds/anet/AnetApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,31 +207,32 @@ public void run(AnetConfiguration configuration, Environment environment)
} else {
logger.info("AnetApplication is starting scheduled workers");
// Schedule any tasks that need to run on an ongoing basis.
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
AnetEmailWorker emailWorker = new AnetEmailWorker(engine.getEmailDao(), configuration);
FutureEngagementWorker futureWorker = new FutureEngagementWorker(engine.getReportDao());
ReportPublicationWorker reportPublicationWorker =
new ReportPublicationWorker(engine.getReportDao(), configuration);
final ReportApprovalWorker reportApprovalWorker =
new ReportApprovalWorker(engine.getReportDao(), configuration);
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

// Check for any reports that need to be published every 5 minutes.
// And run once in 5 seconds from boot-up. (give the server time to boot up).
final ReportPublicationWorker reportPublicationWorker =
new ReportPublicationWorker(configuration, engine.getReportDao());
scheduler.scheduleAtFixedRate(reportPublicationWorker, 5, 5, TimeUnit.MINUTES);
scheduler.schedule(reportPublicationWorker, 5, TimeUnit.SECONDS);

// Check for any emails that need to be sent every 5 minutes.
// And run once in 10 seconds from boot-up. (give the server time to boot up).
final AnetEmailWorker emailWorker = new AnetEmailWorker(configuration, engine.getEmailDao());
scheduler.scheduleAtFixedRate(emailWorker, 5, 5, TimeUnit.MINUTES);
scheduler.schedule(emailWorker, 10, TimeUnit.SECONDS);

// Check for any future engagements every 3 hours.
// And run once in 15 seconds from boot-up. (give the server time to boot up).
final FutureEngagementWorker futureWorker =
new FutureEngagementWorker(configuration, engine.getReportDao());
scheduler.scheduleAtFixedRate(futureWorker, 0, 3, TimeUnit.HOURS);
scheduler.schedule(futureWorker, 15, TimeUnit.SECONDS);

// Check for any reports that need to be approved every 5 minutes.
// And run once in 20 seconds from boot-up. (give the server time to boot up).
final ReportApprovalWorker reportApprovalWorker =
new ReportApprovalWorker(configuration, engine.getReportDao());
scheduler.scheduleAtFixedRate(reportApprovalWorker, 5, 5, TimeUnit.MINUTES);
scheduler.schedule(reportApprovalWorker, 5, TimeUnit.SECONDS);

Expand All @@ -241,22 +242,22 @@ public void run(AnetConfiguration configuration, Environment environment)
// Wait 60 seconds between updates of PostgreSQL materialized views,
// starting 30 seconds after boot-up.
final MaterializedViewRefreshWorker materializedViewRefreshWorker =
new MaterializedViewRefreshWorker(engine.getAdminDao());
new MaterializedViewRefreshWorker(configuration, engine.getAdminDao());
scheduler.scheduleWithFixedDelay(materializedViewRefreshWorker, 30, 60, TimeUnit.SECONDS);
}
}

// Create all of the HTTP Resources.
LoggingResource loggingResource = new LoggingResource();
PersonResource personResource = new PersonResource(engine, configuration);
TaskResource taskResource = new TaskResource(engine, configuration);
LocationResource locationResource = new LocationResource(engine);
OrganizationResource orgResource = new OrganizationResource(engine);
PositionResource positionResource = new PositionResource(engine);
ReportResource reportResource = new ReportResource(engine, configuration);
AdminResource adminResource = new AdminResource(engine, configuration);
HomeResource homeResource = new HomeResource(engine, configuration);
SavedSearchResource savedSearchResource = new SavedSearchResource(engine);
final LoggingResource loggingResource = new LoggingResource();
final PersonResource personResource = new PersonResource(engine, configuration);
final TaskResource taskResource = new TaskResource(engine, configuration);
final LocationResource locationResource = new LocationResource(engine);
final OrganizationResource orgResource = new OrganizationResource(engine);
final PositionResource positionResource = new PositionResource(engine);
final ReportResource reportResource = new ReportResource(engine, configuration);
final AdminResource adminResource = new AdminResource(engine, configuration);
final HomeResource homeResource = new HomeResource(engine, configuration);
final SavedSearchResource savedSearchResource = new SavedSearchResource(engine);
final TagResource tagResource = new TagResource(engine);
final AuthorizationGroupResource authorizationGroupResource =
new AuthorizationGroupResource(engine);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/mil/dds/anet/threads/AbstractWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
import java.time.Instant;
import mil.dds.anet.AnetObjectEngine;
import mil.dds.anet.beans.JobHistory;
import mil.dds.anet.config.AnetConfiguration;
import mil.dds.anet.database.JobHistoryDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractWorker implements Runnable {

private static final Logger logger =
protected static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

protected final AnetConfiguration config;
private final String startMessage;
private JobHistoryDao jobHistoryDao;

public AbstractWorker(String startMessage) {
public AbstractWorker(AnetConfiguration config, String startMessage) {
this.config = config;
this.startMessage = startMessage;
this.jobHistoryDao = AnetObjectEngine.getInstance().getJobHistoryDao();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mil.dds.anet.threads;

import java.lang.invoke.MethodHandles;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
Expand All @@ -19,14 +18,9 @@
import mil.dds.anet.emails.AccountDeactivationWarningEmail;
import mil.dds.anet.utils.AnetAuditLogger;
import mil.dds.anet.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AccountDeactivationWorker extends AbstractWorker {

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final PersonDao dao;

private final List<Integer> daysTillEndOfTourWarnings;
Expand All @@ -36,7 +30,8 @@ public class AccountDeactivationWorker extends AbstractWorker {

public AccountDeactivationWorker(AnetConfiguration config, PersonDao dao,
int warningIntervalInSecs) {
super("Deactivation Warning Worker waking up to check for Future Account Deactivations");
super(config,
"Deactivation Warning Worker waking up to check for Future Account Deactivations");
this.dao = dao;

@SuppressWarnings("unchecked")
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/mil/dds/anet/threads/AnetEmailWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -42,14 +41,9 @@
import org.simplejavamail.api.email.Email;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.MailerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AnetEmailWorker extends AbstractWorker {

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static AnetEmailWorker instance;

private EmailDao dao;
Expand All @@ -70,8 +64,8 @@ public class AnetEmailWorker extends AbstractWorker {
private final List<String> activeDomainNames;

@SuppressWarnings("unchecked")
public AnetEmailWorker(EmailDao dao, AnetConfiguration config) {
super("AnetEmailWorker waking up to send emails!");
public AnetEmailWorker(AnetConfiguration config, EmailDao dao) {
super(config, "AnetEmailWorker waking up to send emails!");
this.dao = dao;
this.mapper = MapperUtils.getDefaultMapper();
this.fromAddr = config.getEmailFromAddr();
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/mil/dds/anet/threads/FutureEngagementWorker.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package mil.dds.anet.threads;

import java.lang.invoke.MethodHandles;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import mil.dds.anet.AnetObjectEngine;
import mil.dds.anet.beans.AnetEmail;
import mil.dds.anet.beans.JobHistory;
import mil.dds.anet.beans.Report;
import mil.dds.anet.config.AnetConfiguration;
import mil.dds.anet.database.ReportDao;
import mil.dds.anet.emails.FutureEngagementUpdated;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FutureEngagementWorker extends AbstractWorker {

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private ReportDao dao;

public FutureEngagementWorker(ReportDao dao) {
super("Future Engagement Worker waking up to check for Future Engagements");
public FutureEngagementWorker(AnetConfiguration config, ReportDao dao) {
super(config, "Future Engagement Worker waking up to check for Future Engagements");
this.dao = dao;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package mil.dds.anet.threads;

import java.lang.invoke.MethodHandles;
import java.time.Instant;
import mil.dds.anet.beans.JobHistory;
import mil.dds.anet.config.AnetConfiguration;
import mil.dds.anet.database.AdminDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MaterializedViewRefreshWorker extends AbstractWorker {

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static final String[] materializedViews =
{"mv_fts_authorizationGroups", "mv_fts_locations", "mv_fts_organizations", "mv_fts_people",
"mv_fts_positions", "mv_fts_reports", "mv_fts_tags", "mv_fts_tasks"};

private final AdminDao dao;

public MaterializedViewRefreshWorker(AdminDao dao) {
super("Refreshing materialized views");
public MaterializedViewRefreshWorker(AnetConfiguration config, AdminDao dao) {
super(config, "Refreshing materialized views");
this.dao = dao;
}

Expand Down
10 changes: 2 additions & 8 deletions src/main/java/mil/dds/anet/threads/ReportApprovalWorker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mil.dds.anet.threads;

import java.lang.invoke.MethodHandles;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
Expand All @@ -17,19 +16,14 @@
import mil.dds.anet.database.ReportDao;
import mil.dds.anet.utils.AnetAuditLogger;
import mil.dds.anet.utils.DaoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReportApprovalWorker extends AbstractWorker {

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final ReportDao dao;
private final Integer nbOfHoursApprovalTimeout;

public ReportApprovalWorker(ReportDao dao, AnetConfiguration config) {
super("Report Approval Worker waking up to check for reports to be approved");
public ReportApprovalWorker(AnetConfiguration config, ReportDao dao) {
super(config, "Report Approval Worker waking up to check for reports to be approved");
this.dao = dao;
this.nbOfHoursApprovalTimeout =
(Integer) config.getDictionaryEntry("reportWorkflow.nbOfHoursApprovalTimeout");
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/mil/dds/anet/threads/ReportPublicationWorker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mil.dds.anet.threads;

import java.lang.invoke.MethodHandles;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
Expand All @@ -15,19 +14,14 @@
import mil.dds.anet.config.AnetConfiguration;
import mil.dds.anet.database.ReportDao;
import mil.dds.anet.utils.AnetAuditLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReportPublicationWorker extends AbstractWorker {

private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final ReportDao dao;
private final Integer nbOfHoursQuarantineApproved;

public ReportPublicationWorker(ReportDao dao, AnetConfiguration config) {
super("Report Publication Worker waking up to check for reports to be published");
public ReportPublicationWorker(AnetConfiguration config, ReportDao dao) {
super(config, "Report Publication Worker waking up to check for reports to be published");
this.dao = dao;
this.nbOfHoursQuarantineApproved =
(Integer) config.getDictionaryEntry("reportWorkflow.nbOfHoursQuarantineApproved");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public static void setUpClass() throws Exception {
"@" + ((List<String>) app.getConfiguration().getDictionaryEntry("domainNames")).get(0);

final AnetObjectEngine engine = AnetObjectEngine.getInstance();
emailWorker = new AnetEmailWorker(engine.getEmailDao(), app.getConfiguration());
futureEngagementWorker = new FutureEngagementWorker(engine.getReportDao());
emailWorker = new AnetEmailWorker(app.getConfiguration(), engine.getEmailDao());
futureEngagementWorker =
new FutureEngagementWorker(app.getConfiguration(), engine.getReportDao());
emailServer = new FakeSmtpServer(app.getConfiguration().getSmtp());

// Flush all reports from previous tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public static void setUpClass() throws Exception {
"@" + ((List<String>) configuration.getDictionaryEntry("domainNames")).get(0);

final AnetObjectEngine engine = AnetObjectEngine.getInstance();
emailWorker = new AnetEmailWorker(engine.getEmailDao(), configuration);
reportPublicationWorker = new ReportPublicationWorker(engine.getReportDao(), configuration);
emailWorker = new AnetEmailWorker(configuration, engine.getEmailDao());
reportPublicationWorker = new ReportPublicationWorker(configuration, engine.getReportDao());
emailServer = new FakeSmtpServer(configuration.getSmtp());

// Flush all reports from previous tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void setUp() throws Exception {

final DropwizardAppExtension<AnetConfiguration> app = TestApp.app;
emailDao = mock(EmailDao.class, Mockito.RETURNS_DEEP_STUBS);
emailWorker = new AnetEmailWorker(emailDao, app.getConfiguration());
emailWorker = new AnetEmailWorker(app.getConfiguration(), emailDao);

whitelistedEmail =
"@" + ((List<String>) app.getConfiguration().getDictionaryEntry("domainNames")).get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void setUpEmailServer() throws Exception {
final List<String> activeDomainNames = (List<String>) dict.get("activeDomainNames");
activeDomainNames.add("dds.mil");
config.setDictionary(dict);
emailWorker = new AnetEmailWorker(AnetObjectEngine.getInstance().getEmailDao(), config);
emailWorker = new AnetEmailWorker(config, AnetObjectEngine.getInstance().getEmailDao());
emailServer = new FakeSmtpServer(config.getSmtp());
}

Expand Down

0 comments on commit 2436698

Please sign in to comment.