Subversion Repositories SmartDukaan

Rev

Rev 36342 | Rev 36354 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36342 Rev 36347
Line 9... Line 9...
9
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Service;
10
import org.springframework.stereotype.Service;
11
 
11
 
12
import java.util.LinkedHashMap;
12
import java.util.LinkedHashMap;
13
import java.util.List;
13
import java.util.List;
-
 
14
import java.util.Set;
-
 
15
import java.util.concurrent.ConcurrentHashMap;
-
 
16
import java.util.concurrent.ExecutorService;
-
 
17
import java.util.concurrent.Executors;
14
 
18
 
15
/**
19
/**
16
 * Orchestrates offer processing with per-partner transaction isolation and batch tracking.
20
 * Orchestrates offer processing with per-partner transaction isolation and batch tracking.
17
 *
21
 *
18
 * NO @Transactional on this class or any of its methods — the orchestrator must not
22
 * NO @Transactional on this class or any of its methods — the orchestrator must not
Line 24... Line 28...
24
@Service
28
@Service
25
public class OfferBatchService {
29
public class OfferBatchService {
26
 
30
 
27
    private static final Logger LOGGER = LogManager.getLogger(OfferBatchService.class);
31
    private static final Logger LOGGER = LogManager.getLogger(OfferBatchService.class);
28
 
32
 
-
 
33
    private static final ExecutorService BATCH_EXECUTOR = Executors.newFixedThreadPool(3, r -> {
-
 
34
        Thread t = new Thread(r, "offer-batch-worker");
-
 
35
        t.setDaemon(true);
-
 
36
        return t;
-
 
37
    });
-
 
38
 
-
 
39
    private final Set<Integer> inFlightOfferIds = ConcurrentHashMap.newKeySet();
-
 
40
 
29
    @Autowired
41
    @Autowired
30
    private OfferService offerService;
42
    private OfferService offerService;
31
 
43
 
32
    @Autowired
44
    @Autowired
33
    private OfferProcessingHelper offerProcessingHelper;
45
    private OfferProcessingHelper offerProcessingHelper;
34
 
46
 
35
    @Autowired
47
    @Autowired
36
    private CronBatchService cronBatchService;
48
    private CronBatchService cronBatchService;
37
 
49
 
-
 
50
    /**
-
 
51
     * Fire-and-forget: schedule the batch on a background worker and return immediately.
-
 
52
     * Prevents duplicate concurrent runs for the same offerId at the JVM level.
-
 
53
     * Used by HTTP controllers; cron CLI keeps using the sync processOfferWithBatch.
-
 
54
     */
-
 
55
    public String submitBatchAsync(int offerId) {
-
 
56
        if (!inFlightOfferIds.add(offerId)) {
-
 
57
            LOGGER.info("Offer {} batch submit ignored — already in flight", offerId);
-
 
58
            return "Offer " + offerId + " is already being processed. Check the batch summary for progress.";
-
 
59
        }
-
 
60
        BATCH_EXECUTOR.submit(() -> {
-
 
61
            try {
-
 
62
                processOfferWithBatch(offerId);
-
 
63
            } catch (Exception e) {
-
 
64
                LOGGER.error("Offer {} batch processing failed: {}", offerId, e.getMessage(), e);
-
 
65
            } finally {
-
 
66
                inFlightOfferIds.remove(offerId);
-
 
67
            }
-
 
68
        });
-
 
69
        LOGGER.info("Offer {} batch submitted to background worker", offerId);
-
 
70
        return "Offer " + offerId + " submitted for processing. Check the batch summary for progress.";
-
 
71
    }
-
 
72
 
38
    public void processOfferWithBatch(int offerId) throws Exception {
73
    public void processOfferWithBatch(int offerId) throws Exception {
39
        CreateOfferRequest createOfferRequest = offerProcessingHelper.loadOfferRequest(offerId);
74
        CreateOfferRequest createOfferRequest = offerProcessingHelper.loadOfferRequest(offerId);
40
        if (createOfferRequest == null) {
75
        if (createOfferRequest == null) {
41
            return;
76
            return;
42
        }
77
        }