Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OZ-532: Force release liquibase lock before EIP client startup #34

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[NPE] Use Datasource to force release lock
  • Loading branch information
VaishSiddharth committed May 13, 2024
commit a2b9d36b9b045447c048a028086b7cceabcff332
27 changes: 21 additions & 6 deletions app/src/main/java/com/ozonehis/eip/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
*/
package com.ozonehis.eip;

import jakarta.annotation.PostConstruct;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.lockservice.LockServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -23,21 +28,31 @@
@SpringBootApplication(scanBasePackages = {"org.openmrs.eip, com.ozonehis.eip"})
public class Application {

@Value("mngtDataSource")
private static DataSource dataSource;
@Value("${spring.liquibase.enabled}")
private boolean liquibaseEnabled;

@Qualifier("mgmt-datasource")
private DataSource dataSource;

public static void main(final String[] args) {
log.info("Starting EIP Client Application . . .");
releaseLiquibaseLock();
SpringApplication.run(Application.class, args);
}

private static void releaseLiquibaseLock() {
if (dataSource == null) {
log.info("No datasource found, skipping liquibase force release lock step");
@Bean(name = "mgmt-datasource")
@ConfigurationProperties(prefix = "spring.mngt-datasource")
public DataSource dataSource() throws ClassNotFoundException {
return DataSourceBuilder.create().build();
}

@PostConstruct
public void releaseLiquibaseLock() {
if (!liquibaseEnabled) {
log.error("Inside releaseLiquibaseLock return");
return;
}
try (Connection connection = dataSource.getConnection()) {
log.error("Adding test log user name {} driver name {}", dataSource.getConnection().getMetaData().getUserName(), dataSource.getConnection().getMetaData().getDriverName());
Database database = DatabaseFactory.getInstance()
.findCorrectDatabaseImplementation(new JdbcConnection(connection));
LockServiceFactory.getInstance().getLockService(database).forceReleaseLock();
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/resources/eip-client.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ camel.springboot.main-run-controller=true
camel.springboot.name=${spring.application.name}
# ----------------------------------------------------------------------------------------------------------------------

#spring.liquibase.enabled=false TODO: Set env variable to configure
# ---- Liquibase Configuration -----------------------------------------------------------------------------------------
spring.liquibase.enabled=${ENABLE_LIQUIBASE}
Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are you utilizing this property in your code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required to prevent liquibase from searching changelog.xml files and returning error for eip-demo service

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do away with this property since it's already been set here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required otherwise the eip-demo service fails to start
Screenshot 2024-05-13 at 6 37 32 PM

9 changes: 9 additions & 0 deletions app/src/main/resources/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ else
fi
fi


# Check if enable liquibase variable is set
if [ -z "${ENABLE_LIQUIBASE}" ]; then
# ENABLE_LIQUIBASE is not set, setting it to true
ENABLE_LIQUIBASE="true"
fi

export ENABLE_LIQUIBASE

# Ensure that the provided environment is valid
case "$ENV" in
dev|prod|test|staging)
Expand Down
Loading