Skip to content

Commit

Permalink
Issue #513
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrojdeCTL committed Sep 27, 2018
1 parent 1cb6bed commit 705f180
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
42 changes: 42 additions & 0 deletions mdw-common/src/com/centurylink/mdw/cli/Checkpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,48 @@ public void updateRef(AssetRef ref) throws SQLException, IOException {
}
}

public void updateRefValue() throws SQLException, IOException {
String select = "select name from value where name = ? and owner_type = ? and owner_id = ?";
try (Connection conn = getDbConnection();
PreparedStatement stmt = conn.prepareStatement(select)) {
stmt.setString(1, "CommitID");
stmt.setString(2, "AssetImport");
stmt.setString(3, "0");
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String update = "update value set value = ?, mod_dt = ? where name = ? and owner_type = ? and owner_id = ?";
try (PreparedStatement updateStmt = conn.prepareStatement(update)) {
updateStmt.setString(1, commit);
updateStmt.setDate(2, (java.sql.Date) new Date());
updateStmt.setString(3, "CommitID");
updateStmt.setString(4, "AssetImport");
updateStmt.setString(5, "0");
updateStmt.executeUpdate();
if (!conn.getAutoCommit()) conn.commit();
}
}
else {
String insert = "insert into value (value, name, owner_type, owner_id, create_dt, create_usr, mod_dt, mod_usr, comments) "
+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement insertStmt = conn.prepareStatement(insert)) {
insertStmt.setString(1, commit);
insertStmt.setString(2, "CommitID");
insertStmt.setString(3, "AssetImport");
insertStmt.setString(4, "0");
insertStmt.setDate(5, (java.sql.Date) new Date());
insertStmt.setString(6, "MDWEngine");
insertStmt.setDate(7, (java.sql.Date) new Date());
insertStmt.setString(8, "MDWEngine");
insertStmt.setString(9, "Represents the last time assets were imported");
insertStmt.executeUpdate();
if (!conn.getAutoCommit()) conn.commit();
}
}
}
}
}


private void loadDbDriver() throws IOException {
try {
Class.forName(DbInfo.getDatabaseDriver(dbInfo.getUrl()));
Expand Down
3 changes: 2 additions & 1 deletion mdw-common/src/com/centurylink/mdw/cli/Import.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,11 @@ public void importMDWGit(ProgressMonitor... monitors) throws IOException {
// Clear cached previous asset revisions
versionControl.clear();

// Capture new Refs in ASSET_REF after import (Git pull)
// Capture new Refs in ASSET_REF after import (Git pull) and insert/update VALUE table
Checkpoint checkpoint = new Checkpoint(new File(getProjectDir() + "/" + getAssetLoc()), versionControl, versionControl.getCommit(), pooledConn);
try {
checkpoint.updateRefs();
checkpoint.updateRefValue();
}
catch (SQLException ex) {
throw new IOException(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.centurylink.mdw.timer.startup;

import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

import com.centurylink.mdw.common.service.SystemMessages;
import com.centurylink.mdw.config.PropertyManager;
Expand Down Expand Up @@ -96,39 +96,34 @@ public void start() {
Thread.sleep(interval);

// Check if it needs to trigger an asset import to sync up this instance's assets
// Exclude if head commit in local repo is missing from ASSET_REF DB table (means new local commit - asset saved)
// Exclude if head commit in local repo is newer than commit from VALUE DB table (means new local commit - asset saved)
try (DbAccess dbAccess = new DbAccess()) {
String select = "select ref from asset_ref where archive_dt= (select max(archive_dt) from asset_ref)";
String latestRefCommit = null;
String select = "select value, mod_dt from value where name='CommitID' and owner_type='AssetImport' and owner_id='0'";
String latestCommit = null;
Date lastImportTime = null;
try (ResultSet rs = dbAccess.runSelect(select)) {
if (rs.next())
latestRefCommit = rs.getString("ref");
}
// Proceed if latest commit (Ref) from ASSET_REF doesn't match current local Git commit (Potential import done in other instance)
if (!vcs.getCommit().equals(latestRefCommit)) {
boolean commitExists = false;
select = "select ref from asset_ref where ref = ?";
try (PreparedStatement stmt = dbAccess.getConnection().prepareStatement(select)) {
stmt.setString(1, vcs.getCommit());
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
commitExists = true;
}
}
if (rs.next()) {
latestCommit = rs.getString("value");
lastImportTime = rs.getDate("mod_dt");
}
// Perform import if current local Git commit exists in ASSET_REF table
if (commitExists) {
logger.info("Detected Asset Import in cluster. Performing Asset Import...");
bulletin = SystemMessages.bulletinOn("Asset import in progress...");
Import importer = new Import(gitRoot, vcs, branch, gitHardReset, dbAccess.getConnection());
importer.setAssetLoc(vcs.getRelativePath(assetDir));
importer.importMDWGit();
SystemMessages.bulletinOff(bulletin, "Asset import completed");
bulletin = null;
CacheRegistration.getInstance().refreshCaches(null);
}
// Proceed if latest commit from VALUE table doesn't match current local Git commit (Potential import done in other instance)
if (latestCommit != null && !vcs.getCommit().equals(latestCommit)) {
Date currentCommitTime = new Date(vcs.getCommitTime(vcs.getCommit()));
// Check if import was done after the current local commit was created - Otherwise, means new local commit - asset saved
if (currentCommitTime.getTime() < lastImportTime.getTime()) {
logger.info("Detected Asset Import in cluster. Performing Asset Import...");
bulletin = SystemMessages.bulletinOn("Asset import in progress...");
Import importer = new Import(gitRoot, vcs, branch, gitHardReset, dbAccess.getConnection());
importer.setAssetLoc(vcs.getRelativePath(assetDir));
importer.importMDWGit();
SystemMessages.bulletinOff(bulletin, "Asset import completed");
bulletin = null;
CacheRegistration.getInstance().refreshCaches(null);
}
}
}

}
catch (InterruptedException e) {
if (!_terminating) throw e;
Expand Down

0 comments on commit 705f180

Please sign in to comment.