gabriel-farache commented on code in PR #4094:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/4094#discussion_r2581635461


##########
quarkus/addons/token-exchange/runtime/src/main/java/org/kie/kogito/addons/quarkus/token/exchange/cache/TokenPolicyManager.java:
##########
@@ -55,25 +56,37 @@ public long expireAfterRead(String key, CachedTokens value, 
long currentTime, lo
     }
 
     /**
-     * Calculate time to expiration based on token's actual expiration time 
minus proactive refresh buffer
+     * Calculate time to expiration based on token's actual expiration time 
minus proactive refresh buffer.
+     * This method sets the process instance context from the cache key for 
proper logging.
      */
     private static long calculateTimeToExpiration(String cacheKey, 
CachedTokens tokens) {
-        String authName = CacheUtils.extractAuthNameFromCacheKey(cacheKey);
-        long proactiveRefreshSeconds = Math.max(0, 
ConfigReaderUtils.getProactiveRefreshSeconds(authName));
+        // Extract process instance ID from cache key and set context for 
logging
+        String processInstanceId = 
CacheUtils.extractProcessInstanceIdFromCacheKey(cacheKey);
+        if (processInstanceId != null && !processInstanceId.isEmpty()) {
+            ProcessInstanceContext.setProcessInstanceId(processInstanceId);
+        }
 
-        long currentTimeSeconds = 
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
-        long tokenExpirationSeconds = tokens.expirationTime();
+        try {
+            String authName = CacheUtils.extractAuthNameFromCacheKey(cacheKey);
+            long proactiveRefreshSeconds = Math.max(0, 
ConfigReaderUtils.getProactiveRefreshSeconds(authName));
 
-        // Schedule refresh proactiveRefreshSeconds before actual expiration
-        long refreshTimeSeconds = tokenExpirationSeconds - 
proactiveRefreshSeconds;
-        long timeToRefreshSeconds = Math.max(0, refreshTimeSeconds - 
currentTimeSeconds);
+            long currentTimeSeconds = 
TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
+            long tokenExpirationSeconds = tokens.expirationTime();
 
-        // Convert to nanoseconds for Caffeine
-        long timeToRefreshNanos = 
TimeUnit.SECONDS.toNanos(timeToRefreshSeconds);
+            // Schedule refresh proactiveRefreshSeconds before actual 
expiration
+            long refreshTimeSeconds = tokenExpirationSeconds - 
proactiveRefreshSeconds;
+            long timeToRefreshSeconds = Math.max(0, refreshTimeSeconds - 
currentTimeSeconds);
 
-        LOGGER.info("Token for key '{}' will be refreshed in {} seconds 
(expires at {}, refresh buffer {} seconds)",
-                cacheKey, timeToRefreshSeconds, tokenExpirationSeconds, 
proactiveRefreshSeconds);
+            // Convert to nanoseconds for Caffeine
+            long timeToRefreshNanos = 
TimeUnit.SECONDS.toNanos(timeToRefreshSeconds);
 
-        return timeToRefreshNanos;
+            LOGGER.info("Token for key '{}' will be refreshed in {} seconds 
(expires at {}, refresh buffer {} seconds)",
+                    cacheKey, timeToRefreshSeconds, tokenExpirationSeconds, 
proactiveRefreshSeconds);

Review Comment:
   may be useful when debugging so I'll add it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to