diff --git a/src/main/groovy/com/devsoap/dbt/handlers/ExecutorHandler.groovy b/src/main/groovy/com/devsoap/dbt/handlers/ExecutorHandler.groovy index 0ddea49..e1c16d1 100644 --- a/src/main/groovy/com/devsoap/dbt/handlers/ExecutorHandler.groovy +++ b/src/main/groovy/com/devsoap/dbt/handlers/ExecutorHandler.groovy @@ -1,5 +1,6 @@ package com.devsoap.dbt.handlers +import com.devsoap.dbt.config.DBTConfig import com.devsoap.dbt.data.BlockTransaction import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper @@ -9,14 +10,27 @@ import ratpack.exec.Promise import ratpack.handling.Context import ratpack.handling.Handler import ratpack.http.Status +import ratpack.http.client.HttpClient import ratpack.jdbctx.Transaction +import javax.inject.Inject import javax.sql.DataSource import java.sql.ResultSet @Slf4j class ExecutorHandler implements Handler { + private final DBTConfig config + private final HttpClient client + private final ObjectMapper mapper + + @Inject + ExecutorHandler(DBTConfig config, HttpClient client, ObjectMapper mapper) { + this.mapper = mapper + this.client = client + this.config = config + } + @Override void handle(Context ctx) throws Exception { ctx.request.body.then { body -> @@ -31,6 +45,18 @@ class ExecutorHandler implements Handler { executeCommands(ds, mapper, transaction).then { transaction.executed = true + + // Notify ledger of result + log.info("Updating ledger with execution result") + client.post(config.ledger.remoteUrl.toURI(), { spec -> + spec.body.text(mapper.writeValueAsString(transaction)) + }).then { + if(it.status != Status.OK) { + log.error("Failed to update ledger with execution result for transaction $transaction.id") + } + } + + // Return transaction with result ctx.response.send(mapper.writeValueAsString(transaction)) } } diff --git a/src/main/groovy/com/devsoap/dbt/handlers/LedgerUpdateTransactionHandler.groovy b/src/main/groovy/com/devsoap/dbt/handlers/LedgerUpdateTransactionHandler.groovy index b54e5ec..5efb193 100644 --- a/src/main/groovy/com/devsoap/dbt/handlers/LedgerUpdateTransactionHandler.groovy +++ b/src/main/groovy/com/devsoap/dbt/handlers/LedgerUpdateTransactionHandler.groovy @@ -20,18 +20,18 @@ import javax.inject.Inject @Slf4j class LedgerUpdateTransactionHandler implements Handler { - private final String executorUrl + private final DBTConfig config @Inject LedgerUpdateTransactionHandler(DBTConfig config) { - executorUrl = config.executor.remoteUrl + this.config = config } @Override void handle(Context ctx) throws Exception { ctx.with { if(ctx.request.method == HttpMethod.POST) { - if(!executorUrl) { + if(!config.executor.remoteUrl) { throw new RuntimeException("Executor URL is not set, cannot update transaction") } @@ -46,9 +46,9 @@ class LedgerUpdateTransactionHandler implements Handler { log.info "Transaction $transaction.id exists, updating transaction" ledgerService.updateTransaction(session, transaction).then { log.info("Transaction $it updated in ledger") - if(transaction.completed){ - log.info("Sending transaction $transaction.id to executor at $executorUrl") - redirect(executorUrl) + if(transaction.completed && !transaction.executed){ + log.info("Sending transaction $transaction.id to executor at $config.executor.remoteUrl") + redirect(config.executor.remoteUrl) } else { render(Jackson.json(transaction)) } @@ -57,9 +57,9 @@ class LedgerUpdateTransactionHandler implements Handler { log.info("Creating new transaction") ledgerService.newTransaction(session, transaction).then { log.info("Transaction $it added to ledger") - if(transaction.completed){ - log.info("Sending transaction $transaction.id to executor at $executorUrl") - redirect(executorUrl) + if(transaction.completed && !transaction.executed){ + log.info("Sending transaction $transaction.id to executor at $config.executor.remoteUrl") + redirect(config.executor.remoteUrl) } else { render(Jackson.json(transaction)) }