Skip to content

Commit

Permalink
Fix the shutdown procedure. However, later I'll move the cleanup to t…
Browse files Browse the repository at this point in the history
…he startup.

See #18
  • Loading branch information
OndraZizka committed Oct 4, 2018
1 parent d2e4d89 commit 2ac4163
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/main/java/cz/dynawest/csvcruncher/Cruncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,32 @@ public void crunch() throws Exception
}
finally
{
if (reachedStage.passed(ReachedCrunchStage.INPUT_TABLES_CREATED)) {
LOG.info(" *** SHUTDOWN CLEANUP SEQUENCE ***");

//if (reachedStage.passed(ReachedCrunchStage.INPUT_TABLES_CREATED))
// I'm removing these stage checks, since the table might have been left
// from previous run. Later let's implement a cleanup at start.
{
for (Map.Entry<String, File> tableAndFile : tablesToFiles.entrySet()) {
this.detachTable(tableAndFile.getKey(), false, true);
try {
this.detachTable(tableAndFile.getKey(), false, true);
} catch (Exception ex) {
LOG.severe("Could not delete the input table: " + ex.getMessage());
}
}
}

if (reachedStage.passed(ReachedCrunchStage.OUTPUT_TABLE_CREATED)) {
this.detachTable(TABLE_NAME__OUTPUT, false, true);
//if (reachedStage.passed(ReachedCrunchStage.OUTPUT_TABLE_CREATED))
{
try {
this.detachTable(TABLE_NAME__OUTPUT, false, true);
} catch (Exception ex) {
LOG.severe("Could not delete the output table: " + ex.getMessage());
}
}

if (reachedStage.passed(ReachedCrunchStage.OUTPUT_TABLE_FILLED)) {
//if (reachedStage.passed(ReachedCrunchStage.OUTPUT_TABLE_FILLED))
{
executeDbCommand("DROP SCHEMA PUBLIC CASCADE", "Failed to delete the database: ");
this.conn.close();
}
Expand All @@ -255,6 +270,7 @@ enum ReachedCrunchStage {

public boolean passed(ReachedCrunchStage stage)
{
LOG.info(String.format("%s >= %s?", this.ordinal(), stage.ordinal()));
return (this.ordinal() >= stage.ordinal());
}
}
Expand Down Expand Up @@ -314,14 +330,15 @@ private void executeDbCommand(String sql, String errorMsg) throws SQLException
{
try (Statement stmt = this.conn.createStatement()){
stmt.execute(sql);
} catch (Exception ex) {
}
catch (Exception ex) {
if (StringUtils.isBlank(errorMsg))
throw ex;
else
else {
throw new RuntimeException(errorMsg +
"\n " + sql +
"\n " + ex.getMessage() +
"\n " + this.conn.getWarnings().getNextWarning(), ex);
"\n SQL: " + sql +
"\n DB error: " + ex.getClass().getSimpleName() + " " + ex.getMessage());
}
}
}

Expand Down

0 comments on commit 2ac4163

Please sign in to comment.