-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Panos Karampis
committed
Oct 10, 2024
1 parent
138b78f
commit 538b3d1
Showing
3 changed files
with
75 additions
and
83 deletions.
There are no files selected for viewing
152 changes: 72 additions & 80 deletions
152
src/main/java/dk/panos/promofacie/TransactionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,72 @@ | ||
//package dk.panos.promofacie; | ||
// | ||
//import dk.panos.promofacie.kafka.KafkaVerificationService; | ||
//import dk.panos.promofacie.redis.RedisVerificationService; | ||
//import dk.panos.promofacie.redis.redis_model.Verification; | ||
//import dk.panos.promofacie.service.BlockchainVerificationService; | ||
//import io.quarkus.arc.profile.IfBuildProfile; | ||
//import io.quarkus.runtime.StartupEvent; | ||
//import io.smallrye.mutiny.Multi; | ||
//import io.smallrye.mutiny.Uni; | ||
//import io.smallrye.mutiny.tuples.Tuple2; | ||
//import jakarta.enterprise.context.ApplicationScoped; | ||
//import jakarta.enterprise.event.Observes; | ||
//import jakarta.inject.Inject; | ||
//import org.slf4j.Logger; | ||
//import org.slf4j.LoggerFactory; | ||
// | ||
//import java.time.Duration; | ||
//import java.time.temporal.ChronoUnit; | ||
//import java.util.function.Function; | ||
// | ||
//@ApplicationScoped | ||
////@IfBuildProfile("prod") | ||
//public class TransactionListener { | ||
// private static Logger log = LoggerFactory.getLogger(TransactionListener.class); | ||
// @Inject | ||
// RedisVerificationService verificationService; | ||
// @Inject | ||
// BlockchainVerificationService blockchainVerificationService; | ||
// @Inject | ||
// KafkaVerificationService kafkaVerificationService; | ||
// | ||
// public void listen(@Observes StartupEvent ev) { | ||
// Multi<Tuple2<Verification, Boolean>> transactionExistsCheck = verificationService.process() | ||
// .onItem().invoke(items -> log.debug("I have the following items for verification {}", items)) | ||
// .toMulti() | ||
// .onItem() | ||
// .transformToIterable(Function.identity()) | ||
// .onItem() | ||
// .invoke(verification -> log.debug("Picked {} for verification", verification)) | ||
// .onItem() | ||
// .transformToUni(verification -> | ||
// blockchainVerificationService.transactionExists(verification.address(), verification.discordId(), verification.dateTime()) | ||
// .onFailure().invoke(err -> log.error(err.getMessage())) | ||
// .onFailure().retry().withBackOff(Duration.ofMillis(100)) | ||
// .atMost(100) | ||
// .map(outcome -> Tuple2.of(verification, outcome)) | ||
// ) | ||
// .merge(); | ||
// | ||
// Multi.createFrom().ticks().every(Duration.of(5, ChronoUnit.MINUTES)) | ||
// .onItem() | ||
// .invoke(tick -> log.debug("Checking for new transactions on tick {}", tick)).onItem() | ||
// .transformToMulti(tick -> transactionExistsCheck) | ||
// .merge() | ||
// .onItem() | ||
// .transformToUni(outcome -> { | ||
// Verification verification = outcome.getItem1(); | ||
// boolean success = outcome.getItem2(); | ||
// if (success) { | ||
// log.debug("Sending success for {}", verification); | ||
// return kafkaVerificationService.sendSuccess(verification); | ||
// } else { | ||
// return verificationService.queue(verification) | ||
// .onItem().transformToUni(itemsAdded -> { | ||
// if (itemsAdded > 0) { | ||
// log.debug("Retrying {}", verification); | ||
// return Uni.createFrom().voidItem(); | ||
// } else { | ||
// log.debug("Sending failure for {} due to reached max amount of tries", verification); | ||
// return kafkaVerificationService.sendFailure(verification); | ||
// } | ||
// }); | ||
// } | ||
// }).merge() | ||
// .onFailure().retry().indefinitely() | ||
// .subscribe().with(nothing -> { | ||
// }); | ||
// } | ||
//} | ||
package dk.panos.promofacie; | ||
|
||
import dk.panos.promofacie.kafka.KafkaVerificationService; | ||
import dk.panos.promofacie.redis.RedisVerificationService; | ||
import dk.panos.promofacie.redis.redis_model.Verification; | ||
import dk.panos.promofacie.service.BlockchainVerificationService; | ||
import io.quarkus.arc.profile.IfBuildProfile; | ||
import io.quarkus.runtime.StartupEvent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.tuples.Tuple2; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Observes; | ||
import jakarta.inject.Inject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.function.Function; | ||
|
||
@ApplicationScoped | ||
@IfBuildProfile("prod") | ||
public class TransactionListener { | ||
private static Logger log = LoggerFactory.getLogger(TransactionListener.class); | ||
@Inject | ||
RedisVerificationService verificationService; | ||
@Inject | ||
BlockchainVerificationService blockchainVerificationService; | ||
@Inject | ||
KafkaVerificationService kafkaVerificationService; | ||
|
||
@Scheduled(every = "5m") | ||
Uni<Void> listen() { | ||
return verificationService.process() | ||
.onItem().invoke(items -> log.debug("I have the following items for verification {}", items)) | ||
.toMulti() | ||
.onItem() | ||
.transformToIterable(Function.identity()) | ||
.onItem() | ||
.invoke(verification -> log.debug("Picked {} for verification", verification)) | ||
.onItem() | ||
.transformToUni(verification -> | ||
blockchainVerificationService.transactionExists(verification.address(), verification.discordId(), verification.dateTime()) | ||
.onFailure().invoke(err -> log.error(err.getMessage())) | ||
.onFailure().retry().withBackOff(Duration.ofMillis(100)) | ||
.atMost(100) | ||
.map(outcome -> Tuple2.of(verification, outcome)) | ||
).merge(4).onItem() | ||
.transformToUni(outcome -> { | ||
Verification verification = outcome.getItem1(); | ||
boolean success = outcome.getItem2(); | ||
if (success) { | ||
log.debug("Sending success for {}", verification); | ||
return kafkaVerificationService.sendSuccess(verification); | ||
} else { | ||
return verificationService.queue(verification) | ||
.onItem().transformToUni(itemsAdded -> { | ||
if (itemsAdded > 0) { | ||
log.debug("Retrying {}", verification); | ||
return Uni.createFrom().voidItem(); | ||
} else { | ||
log.debug("Sending failure for {} due to reached max amount of tries", verification); | ||
return kafkaVerificationService.sendFailure(verification); | ||
} | ||
}); | ||
} | ||
}) | ||
.concatenate().collect().asList().replaceWithVoid(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters