@@ -60,6 +60,7 @@ public class MicroblogController {
6060 private final RedisClient redisClient ;
6161 Logger logger = LoggerFactory .getLogger (MicroblogController .class );
6262 private final UserAuthServiceClient userAuthServiceClient ;
63+ private final Boolean ragServiceEnabled ;
6364 private final RAGServiceClient ragServiceClient ;
6465 private final PostSerializer postSerializer ;
6566 private final ExecutorService ragExecutor = Executors .newFixedThreadPool (
@@ -72,6 +73,7 @@ public MicroblogController(Tracer tracer, PostSerializer postSerializer) {
7273 String userAuthServiceAddress ;
7374 String ragServiceAddress ;
7475 String ragServicePort ;
76+ boolean isRagServiceEnabled = false ;
7577 if (System .getenv ("REDIS_SERVICE_ADDRESS" ) != null ) {
7678 redisServiceAddress = System .getenv ("REDIS_SERVICE_ADDRESS" );
7779 logger .info ("REDIS_SERVICE_ADDRESS set to {}" , redisServiceAddress );
@@ -88,6 +90,13 @@ public MicroblogController(Tracer tracer, PostSerializer postSerializer) {
8890 logger .warn ("No USER_AUTH_SERVICE_ADDRESS environment variable defined, falling back to localhost:9091." );
8991 }
9092
93+ if (System .getenv ("RAG_SERVICE_ENABLED" ) != null ) {
94+ isRagServiceEnabled = Boolean .parseBoolean (System .getenv ("RAG_SERVICE_ENABLED" ));
95+ logger .info ("RAG_SERVICE_ENABLED set to {}" , isRagServiceEnabled );
96+ } else {
97+ logger .warn ("No RAG_SERVICE_ENABLED environment variable defined, falling back to false." );
98+ }
99+
91100 if (System .getenv ("RAG_SERVICE_ADDRESS" ) != null ) {
92101 ragServiceAddress = System .getenv ("RAG_SERVICE_ADDRESS" );
93102 logger .info ("RAG_SERVICE_ADDRESS set to {}" , ragServiceAddress );
@@ -107,6 +116,7 @@ public MicroblogController(Tracer tracer, PostSerializer postSerializer) {
107116 this .redisClient = new RedisClient (redisServiceAddress , this .userAuthServiceClient , tracer );
108117 this .ragServiceClient = new RAGServiceClient (ragServiceAddress , ragServicePort );
109118 this .postSerializer = postSerializer ;
119+ this .ragServiceEnabled = isRagServiceEnabled ;
110120 }
111121
112122 @ RequestMapping ("/timeline" )
@@ -196,22 +206,24 @@ public PostId post(@RequestBody PostForm postForm, @CookieValue(value = "jwt", r
196206 Claims claims = JwtTokensUtils .decodeTokenClaims (jwt );
197207 String postId = redisClient .newPost (claims .get ("userid" ).toString (), postForm .getContent (), postForm .getImageUrl ());
198208
199- CompletableFuture .runAsync (() -> {
200- try {
201- String spamClassificationResult = ragServiceClient .getSpamClassification (postForm .getContent ());
202- if (spamClassificationResult == null || spamClassificationResult .trim ().isEmpty ()) {
203- throw new InvalidSpamPredictionException ("RAG spam classification result is null or empty for post with ID " + postId );
204- } else if (spamClassificationResult .trim ().equalsIgnoreCase ("not_spam" )) {
205- redisClient .setSpamPredictedLabel (postId , false );
206- } else if (spamClassificationResult .trim ().equalsIgnoreCase ("spam" )) {
207- redisClient .setSpamPredictedLabel (postId , true );
208- } else {
209- throw new InvalidSpamPredictionException ("RAG spam classification result is invalid for post with ID " + postId + ": " + spamClassificationResult );
209+ if (Boolean .TRUE .equals (this .ragServiceEnabled )) {
210+ CompletableFuture .runAsync (() -> {
211+ try {
212+ String spamClassificationResult = ragServiceClient .getSpamClassification (postForm .getContent ());
213+ if (spamClassificationResult == null || spamClassificationResult .trim ().isEmpty ()) {
214+ throw new InvalidSpamPredictionException ("RAG spam classification result is null or empty for post with ID " + postId );
215+ } else if (spamClassificationResult .trim ().equalsIgnoreCase ("not_spam" )) {
216+ redisClient .setSpamPredictedLabel (postId , false );
217+ } else if (spamClassificationResult .trim ().equalsIgnoreCase ("spam" )) {
218+ redisClient .setSpamPredictedLabel (postId , true );
219+ } else {
220+ throw new InvalidSpamPredictionException ("RAG spam classification result is invalid for post with ID " + postId + ": " + spamClassificationResult );
221+ }
222+ } catch (Exception e ) {
223+ logger .warn ("RAG spam classification failed for post with ID {}" , postId , e );
210224 }
211- } catch (Exception e ) {
212- logger .warn ("RAG spam classification failed for post with ID {}" , postId , e );
213- }
214- }, ragExecutor );
225+ }, ragExecutor );
226+ }
215227
216228 return new PostId (postId );
217229 }
0 commit comments