diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/CacheConfig.java b/src/main/java/ch/ethz/seb/sebserver/webservice/CacheConfig.java index 19240cfe..c93ba58a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/CacheConfig.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/CacheConfig.java @@ -60,7 +60,5 @@ public class CacheConfig extends JCacheConfigurerSupport { log.error("Failed to initialize caching with EHCache. Fallback to simple caching"); return new ConcurrentMapCacheManager(); } - } - } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/SEBServerMigrationStrategy.java b/src/main/java/ch/ethz/seb/sebserver/webservice/SEBServerMigrationStrategy.java new file mode 100644 index 00000000..6ab49b36 --- /dev/null +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/SEBServerMigrationStrategy.java @@ -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); + } + } + +} diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceConfig.java b/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceConfig.java index febe709a..c1f79837 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceConfig.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceConfig.java @@ -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; - } - } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceInit.java b/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceInit.java index 6ada3aa4..860f8358 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceInit.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/WebserviceInit.java @@ -130,6 +130,7 @@ public class WebserviceInit implements ApplicationListener"); SEBServerInit.INIT_LOGGER.info("----> Unregister Webservice: {}", this.webserviceInfo.getWebserviceUUID()); + this.webserviceInfoDAO.unregister(this.webserviceInfo.getWebserviceUUID()); SEBServerInit.INIT_LOGGER.info("---->"); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/WebserviceInfoDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/WebserviceInfoDAOImpl.java index 08095344..6944c75b 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/WebserviceInfoDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/WebserviceInfoDAOImpl.java @@ -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; } }