SEBSERV-208 fixed migration strategy for prod and distributed setup
This commit is contained in:
parent
c043f0460e
commit
ce270f3057
5 changed files with 66 additions and 30 deletions
|
@ -60,7 +60,5 @@ public class CacheConfig extends JCacheConfigurerSupport {
|
|||
log.error("Failed to initialize caching with EHCache. Fallback to simple caching");
|
||||
return new ConcurrentMapCacheManager();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2021 ETH Zürich, Educational Development and Technology (LET)
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package ch.ethz.seb.sebserver.webservice;
|
||||
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile;
|
||||
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.WebserviceInfoDAO;
|
||||
|
||||
@Component
|
||||
@WebServiceProfile
|
||||
public class SEBServerMigrationStrategy implements FlywayMigrationStrategy {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SEBServerMigrationStrategy.class);
|
||||
|
||||
private final boolean cleanDBOnStartup;
|
||||
private final WebserviceInfo webserviceInfo;
|
||||
private final WebserviceInfoDAO webserviceInfoDAO;
|
||||
|
||||
public SEBServerMigrationStrategy(
|
||||
final WebserviceInfo webserviceInfo,
|
||||
final WebserviceInfoDAO webserviceInfoDAO,
|
||||
@Value("${sebserver.webservice.clean-db-on-startup:false}") final boolean cleanDBOnStartup) {
|
||||
|
||||
this.webserviceInfo = webserviceInfo;
|
||||
this.webserviceInfoDAO = webserviceInfoDAO;
|
||||
this.cleanDBOnStartup = cleanDBOnStartup;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void migrate(final Flyway flyway) {
|
||||
try {
|
||||
// If we are in a distributed setup only apply migration task if this is the master service
|
||||
if (this.webserviceInfo.isDistributed()
|
||||
&& !this.webserviceInfoDAO.isMaster(this.webserviceInfo.getWebserviceUUID())) {
|
||||
|
||||
log.info(
|
||||
"Skip migration task since this is not a master instance: {}",
|
||||
this.webserviceInfo.getWebserviceUUID());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.cleanDBOnStartup) {
|
||||
flyway.clean();
|
||||
}
|
||||
flyway.migrate();
|
||||
} catch (final Exception e) {
|
||||
log.error("Failed to apply migration task: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -10,13 +10,9 @@ package ch.ethz.seb.sebserver.webservice;
|
|||
|
||||
import org.cryptonode.jncryptor.AES256JNCryptor;
|
||||
import org.cryptonode.jncryptor.JNCryptor;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import ch.ethz.seb.sebserver.gbl.Constants;
|
||||
import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile;
|
||||
|
@ -25,9 +21,6 @@ import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile;
|
|||
@WebServiceProfile
|
||||
public class WebserviceConfig {
|
||||
|
||||
@Value("${sebserver.webservice.clean-db-on-startup:false}")
|
||||
boolean cleanDBOnStartup;
|
||||
|
||||
@Lazy
|
||||
@Bean
|
||||
public JNCryptor jnCryptor() {
|
||||
|
@ -36,24 +29,4 @@ public class WebserviceConfig {
|
|||
return aes256jnCryptor;
|
||||
}
|
||||
|
||||
/** For test, development and demo profile, we want to always clean up and
|
||||
* Start the migration from scratch to work with the same data.
|
||||
*
|
||||
* @return FlywayMigrationStrategy for "dev-ws", "test", "demo" profiles */
|
||||
@Bean
|
||||
@Profile(value = { "dev-ws", "test", "demo" })
|
||||
public FlywayMigrationStrategy cleanMigrateStrategy() {
|
||||
final FlywayMigrationStrategy strategy = new FlywayMigrationStrategy() {
|
||||
@Override
|
||||
public void migrate(final Flyway flyway) {
|
||||
if (WebserviceConfig.this.cleanDBOnStartup) {
|
||||
flyway.clean();
|
||||
}
|
||||
flyway.migrate();
|
||||
}
|
||||
};
|
||||
|
||||
return strategy;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -130,6 +130,7 @@ public class WebserviceInit implements ApplicationListener<ApplicationReadyEvent
|
|||
|
||||
SEBServerInit.INIT_LOGGER.info("---->");
|
||||
SEBServerInit.INIT_LOGGER.info("----> Unregister Webservice: {}", this.webserviceInfo.getWebserviceUUID());
|
||||
|
||||
this.webserviceInfoDAO.unregister(this.webserviceInfo.getWebserviceUUID());
|
||||
|
||||
SEBServerInit.INIT_LOGGER.info("---->");
|
||||
|
|
|
@ -173,7 +173,7 @@ public class WebserviceInfoDAOImpl implements WebserviceInfoDAO {
|
|||
.execute();
|
||||
return true;
|
||||
} catch (final Exception e) {
|
||||
log.error("Failed to register webservice: uuid: {}", uuid, e);
|
||||
log.warn("Failed to unregister webservice: uuid: {}, cause: ", uuid, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue