Browse Source

pwnotify startup error handling improvements

jrivard@gmail.com 6 năm trước cách đây
mục cha
commit
bccf1acfcb

+ 2 - 9
server/src/main/java/password/pwm/svc/cluster/ClusterService.java

@@ -77,7 +77,7 @@ public class ClusterService implements PwmService
         {
             final ClusterSettings clusterSettings;
             final ClusterDataServiceProvider clusterDataServiceProvider;
-            dataStore = figureDataStorageMethod( pwmApplication );
+            dataStore = pwmApplication.getConfig().readSettingAsEnum( PwmSetting.CLUSTER_STORAGE_MODE, DataStorageMethod.class );
 
             if ( dataStore != null )
             {
@@ -183,11 +183,9 @@ public class ClusterService implements PwmService
         return Collections.emptyList();
     }
 
-    private DataStorageMethod figureDataStorageMethod( final PwmApplication pwmApplication )
+    private void figureDataStorageMethod( final PwmApplication pwmApplication )
             throws PwmUnrecoverableException
     {
-        final DataStorageMethod method = pwmApplication.getConfig().readSettingAsEnum( PwmSetting.CLUSTER_STORAGE_MODE, DataStorageMethod.class );
-        if ( method == DataStorageMethod.LDAP )
         {
             final UserIdentity userIdentity = pwmApplication.getConfig().getDefaultLdapProfile().getTestUser( pwmApplication );
             if ( userIdentity == null )
@@ -195,21 +193,16 @@ public class ClusterService implements PwmService
                 final String msg = "LDAP storage type selected, but LDAP test user not defined.";
                 LOGGER.debug( msg );
                 startupError = new ErrorInformation( PwmError.ERROR_CLUSTER_SERVICE_ERROR, msg );
-                return null;
             }
         }
 
-        if ( method == DataStorageMethod.DB )
         {
             if ( !pwmApplication.getConfig().hasDbConfigured() )
             {
                 final String msg = "DB storage type selected, but remote DB is not configured.";
                 LOGGER.debug( msg );
                 startupError = new ErrorInformation( PwmError.ERROR_CLUSTER_SERVICE_ERROR, msg );
-                return null;
             }
         }
-
-        return method;
     }
 }

+ 3 - 1
server/src/main/java/password/pwm/svc/pwnotify/PwNotifyEngine.java

@@ -65,6 +65,8 @@ public class PwNotifyEngine
 
     private static final SessionLabel SESSION_LABEL = SessionLabel.PW_EXP_NOTICE_LABEL;
 
+    private static final int MAX_LOG_SIZE = 1024 * 1024 * 1024;
+
     private final PwNotifySettings settings;
     private final PwmApplication pwmApplication;
     private final Writer debugWriter;
@@ -355,7 +357,7 @@ public class PwNotifyEngine
         }
 
         internalLog.append( msg );
-        while ( internalLog.length() > 1024 * 1024 * 1024 )
+        while ( internalLog.length() > MAX_LOG_SIZE )
         {
             final int nextLf = internalLog.indexOf( "\n" );
             if ( nextLf > 0 )

+ 10 - 9
server/src/main/java/password/pwm/svc/pwnotify/PwNotifyService.java

@@ -55,7 +55,6 @@ public class PwNotifyService extends AbstractPwmService implements PwmService
 
     private ExecutorService executorService;
     private PwmApplication pwmApplication;
-    private STATUS status = STATUS.NEW;
     private PwNotifyEngine engine;
     private PwNotifySettings settings;
     private Instant nextExecutionTime;
@@ -65,12 +64,14 @@ public class PwNotifyService extends AbstractPwmService implements PwmService
 
     public StoredJobState getJobState() throws PwmUnrecoverableException
     {
-        if ( status != STATUS.OPEN )
+        if ( status() != STATUS.OPEN )
         {
             if ( getStartupError() != null )
             {
                 return StoredJobState.builder().lastError( getStartupError() ).build();
             }
+
+            return StoredJobState.builder().build();
         }
 
         return storageService.readStoredJobState();
@@ -104,7 +105,7 @@ public class PwNotifyService extends AbstractPwmService implements PwmService
         if ( !pwmApplication.getConfig().readSettingAsBoolean( PwmSetting.PW_EXPY_NOTIFY_ENABLE ) )
         {
             LOGGER.trace( SessionLabel.PWNOTIFY_SESSION_LABEL, "will remain closed, pw notify feature is not enabled" );
-            status = STATUS.CLOSED;
+            setStatus( STATUS.CLOSED );
             return;
         }
 
@@ -136,17 +137,17 @@ public class PwNotifyService extends AbstractPwmService implements PwmService
                     JavaHelper.unhandledSwitchStatement( storageMethod );
             }
 
-            engine = new PwNotifyEngine( pwmApplication, storageService, () -> status == STATUS.CLOSED, null );
-
             executorService = JavaHelper.makeBackgroundExecutor( pwmApplication, this.getClass() );
 
+            engine = new PwNotifyEngine( pwmApplication, storageService, () -> status() == STATUS.CLOSED, null );
+
             pwmApplication.scheduleFixedRateJob( new PwNotifyJob(), executorService, TimeDuration.MINUTE, TimeDuration.MINUTE );
 
-            status = STATUS.OPEN;
+            setStatus( STATUS.OPEN );
         }
         catch ( PwmUnrecoverableException e )
         {
-            status = STATUS.CLOSED;
+            setStatus( STATUS.CLOSED );
             LOGGER.trace( SessionLabel.PWNOTIFY_SESSION_LABEL, "will remain closed, pw notify feature is not enabled due to error: " + e.getMessage() );
             setStartupError( e.getErrorInformation() );
         }
@@ -203,7 +204,7 @@ public class PwNotifyService extends AbstractPwmService implements PwmService
     @Override
     public void close( )
     {
-        status = STATUS.CLOSED;
+        setStatus( STATUS.CLOSED );
         JavaHelper.closeAndWaitExecutor( executorService, TimeDuration.of( 5, TimeDuration.Unit.SECONDS ) );
     }
 
@@ -245,7 +246,7 @@ public class PwNotifyService extends AbstractPwmService implements PwmService
 
     public void executeJob( )
     {
-        if ( status != STATUS.OPEN )
+        if ( status() != STATUS.OPEN )
         {
             LOGGER.trace( SessionLabel.PWNOTIFY_SESSION_LABEL, "ignoring job request start, service is not open" );
             return;