It woarks!
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user