1

It woarks!

This commit is contained in:
2018-05-03 14:28:39 +03:00
parent ca6da1dbe9
commit a3e6f119e7
2 changed files with 35 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
package com.devsoap.dbt.handlers package com.devsoap.dbt.handlers
import com.devsoap.dbt.config.DBTConfig
import com.devsoap.dbt.data.BlockTransaction import com.devsoap.dbt.data.BlockTransaction
import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
@@ -9,14 +10,27 @@ import ratpack.exec.Promise
import ratpack.handling.Context import ratpack.handling.Context
import ratpack.handling.Handler import ratpack.handling.Handler
import ratpack.http.Status import ratpack.http.Status
import ratpack.http.client.HttpClient
import ratpack.jdbctx.Transaction import ratpack.jdbctx.Transaction
import javax.inject.Inject
import javax.sql.DataSource import javax.sql.DataSource
import java.sql.ResultSet import java.sql.ResultSet
@Slf4j @Slf4j
class ExecutorHandler implements Handler { 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 @Override
void handle(Context ctx) throws Exception { void handle(Context ctx) throws Exception {
ctx.request.body.then { body -> ctx.request.body.then { body ->
@@ -31,6 +45,18 @@ class ExecutorHandler implements Handler {
executeCommands(ds, mapper, transaction).then { executeCommands(ds, mapper, transaction).then {
transaction.executed = true 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)) ctx.response.send(mapper.writeValueAsString(transaction))
} }
} }

View File

@@ -20,18 +20,18 @@ import javax.inject.Inject
@Slf4j @Slf4j
class LedgerUpdateTransactionHandler implements Handler { class LedgerUpdateTransactionHandler implements Handler {
private final String executorUrl private final DBTConfig config
@Inject @Inject
LedgerUpdateTransactionHandler(DBTConfig config) { LedgerUpdateTransactionHandler(DBTConfig config) {
executorUrl = config.executor.remoteUrl this.config = config
} }
@Override @Override
void handle(Context ctx) throws Exception { void handle(Context ctx) throws Exception {
ctx.with { ctx.with {
if(ctx.request.method == HttpMethod.POST) { if(ctx.request.method == HttpMethod.POST) {
if(!executorUrl) { if(!config.executor.remoteUrl) {
throw new RuntimeException("Executor URL is not set, cannot update transaction") 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" log.info "Transaction $transaction.id exists, updating transaction"
ledgerService.updateTransaction(session, transaction).then { ledgerService.updateTransaction(session, transaction).then {
log.info("Transaction $it updated in ledger") log.info("Transaction $it updated in ledger")
if(transaction.completed){ if(transaction.completed && !transaction.executed){
log.info("Sending transaction $transaction.id to executor at $executorUrl") log.info("Sending transaction $transaction.id to executor at $config.executor.remoteUrl")
redirect(executorUrl) redirect(config.executor.remoteUrl)
} else { } else {
render(Jackson.json(transaction)) render(Jackson.json(transaction))
} }
@@ -57,9 +57,9 @@ class LedgerUpdateTransactionHandler implements Handler {
log.info("Creating new transaction") log.info("Creating new transaction")
ledgerService.newTransaction(session, transaction).then { ledgerService.newTransaction(session, transaction).then {
log.info("Transaction $it added to ledger") log.info("Transaction $it added to ledger")
if(transaction.completed){ if(transaction.completed && !transaction.executed){
log.info("Sending transaction $transaction.id to executor at $executorUrl") log.info("Sending transaction $transaction.id to executor at $config.executor.remoteUrl")
redirect(executorUrl) redirect(config.executor.remoteUrl)
} else { } else {
render(Jackson.json(transaction)) render(Jackson.json(transaction))
} }