|
9 | 9 | import com.semosan.api.domain.tracking.repository.TrackingSessionRepository; |
10 | 10 | import lombok.RequiredArgsConstructor; |
11 | 11 | import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.slf4j.MDC; |
12 | 13 | import org.springframework.data.redis.connection.stream.RecordId; |
13 | 14 | import org.springframework.data.redis.connection.stream.StringRecord; |
14 | 15 | import org.springframework.data.redis.core.StringRedisTemplate; |
@@ -40,26 +41,33 @@ public class TrackingGpsPublisher { |
40 | 41 |
|
41 | 42 | @Transactional(readOnly = true) |
42 | 43 | public void publish(Long userId, Long sessionId, GpsPointMessage message) { |
43 | | - TrackingSession session = trackingSessionRepository.findById(sessionId) |
44 | | - .orElseThrow(() -> new GeneralException(ErrorStatus.TRACKING_SESSION_NOT_FOUND)); |
45 | | - if (!session.isOwnedBy(userId)) { |
46 | | - throw new GeneralException(ErrorStatus.TRACKING_SESSION_FORBIDDEN); |
47 | | - } |
48 | | - if (session.getStatus() != TrackingSessionStatus.IN_PROGRESS) { |
49 | | - log.debug("Dropping GPS point: sessionId={} status={}", sessionId, session.getStatus()); |
50 | | - return; |
51 | | - } |
| 44 | + MDC.put("sessionId", String.valueOf(sessionId)); |
| 45 | + MDC.put("userId", String.valueOf(userId)); |
| 46 | + try { |
| 47 | + TrackingSession session = trackingSessionRepository.findById(sessionId) |
| 48 | + .orElseThrow(() -> new GeneralException(ErrorStatus.TRACKING_SESSION_NOT_FOUND)); |
| 49 | + if (!session.isOwnedBy(userId)) { |
| 50 | + throw new GeneralException(ErrorStatus.TRACKING_SESSION_FORBIDDEN); |
| 51 | + } |
| 52 | + if (session.getStatus() != TrackingSessionStatus.IN_PROGRESS) { |
| 53 | + log.info("[GPS] 좌표 드롭 | status={}", session.getStatus()); |
| 54 | + return; |
| 55 | + } |
52 | 56 |
|
53 | | - Map<String, String> body = Map.of( |
54 | | - FIELD_SESSION_ID, String.valueOf(sessionId), |
55 | | - FIELD_USER_ID, String.valueOf(userId), |
56 | | - FIELD_LAT, String.valueOf(message.lat()), |
57 | | - FIELD_LNG, String.valueOf(message.lng()), |
58 | | - FIELD_ALTITUDE, message.altitude() == null ? "" : String.valueOf(message.altitude()), |
59 | | - FIELD_RECORDED_AT, message.recordedAt().toString() |
60 | | - ); |
61 | | - StringRecord record = StringRecord.of(body).withStreamKey(trackingProperties.getStreamKey()); |
62 | | - RecordId id = redisTemplate.opsForStream().add(record); |
63 | | - log.trace("Published GPS point: sessionId={} streamId={}", sessionId, id); |
| 57 | + Map<String, String> body = Map.of( |
| 58 | + FIELD_SESSION_ID, String.valueOf(sessionId), |
| 59 | + FIELD_USER_ID, String.valueOf(userId), |
| 60 | + FIELD_LAT, String.valueOf(message.lat()), |
| 61 | + FIELD_LNG, String.valueOf(message.lng()), |
| 62 | + FIELD_ALTITUDE, message.altitude() == null ? "" : String.valueOf(message.altitude()), |
| 63 | + FIELD_RECORDED_AT, message.recordedAt().toString() |
| 64 | + ); |
| 65 | + StringRecord record = StringRecord.of(body).withStreamKey(trackingProperties.getStreamKey()); |
| 66 | + RecordId id = redisTemplate.opsForStream().add(record); |
| 67 | + log.info("[GPS] 좌표 발행 | streamId={}", id); |
| 68 | + } finally { |
| 69 | + MDC.remove("sessionId"); |
| 70 | + MDC.remove("userId"); |
| 71 | + } |
64 | 72 | } |
65 | 73 | } |
0 commit comments