Subversion Repositories SmartDukaan

Rev

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

Rev 36347 Rev 36354
Line 1... Line 1...
1
package com.spice.profitmandi.service.offers;
1
package com.spice.profitmandi.service.offers;
2
 
2
 
3
import com.spice.profitmandi.dao.entity.transaction.CronBatch;
3
import com.spice.profitmandi.dao.entity.transaction.CronBatch;
4
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
4
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
5
import com.spice.profitmandi.dao.model.CreateOfferRequest;
5
import com.spice.profitmandi.dao.model.CreateOfferRequest;
-
 
6
import com.spice.profitmandi.dao.repository.transaction.CronBatchRepository;
6
import com.spice.profitmandi.service.cron.CronBatchService;
7
import com.spice.profitmandi.service.cron.CronBatchService;
7
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
9
import org.apache.logging.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Service;
11
import org.springframework.stereotype.Service;
Line 45... Line 46...
45
    private OfferProcessingHelper offerProcessingHelper;
46
    private OfferProcessingHelper offerProcessingHelper;
46
 
47
 
47
    @Autowired
48
    @Autowired
48
    private CronBatchService cronBatchService;
49
    private CronBatchService cronBatchService;
49
 
50
 
-
 
51
    @Autowired
-
 
52
    private CronBatchRepository cronBatchRepository;
-
 
53
 
50
    /**
54
    /**
51
     * Fire-and-forget: schedule the batch on a background worker and return immediately.
55
     * 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.
56
     * Prevents duplicate concurrent runs for the same offerId at the JVM level.
53
     * Used by HTTP controllers; cron CLI keeps using the sync processOfferWithBatch.
57
     * Used by HTTP controllers; cron CLI keeps using the sync processOfferWithBatch.
54
     */
58
     */
Line 69... Line 73...
69
        LOGGER.info("Offer {} batch submitted to background worker", offerId);
73
        LOGGER.info("Offer {} batch submitted to background worker", offerId);
70
        return "Offer " + offerId + " submitted for processing. Check the batch summary for progress.";
74
        return "Offer " + offerId + " submitted for processing. Check the batch summary for progress.";
71
    }
75
    }
72
 
76
 
73
    public void processOfferWithBatch(int offerId) throws Exception {
77
    public void processOfferWithBatch(int offerId) throws Exception {
-
 
78
        List<CronBatch> running = cronBatchRepository.selectRunningForOffer(offerId);
-
 
79
        if (!running.isEmpty()) {
-
 
80
            CronBatch existing = running.get(0);
-
 
81
            LOGGER.info("Offer {} already running (batch {}, started {}); skipping duplicate run",
-
 
82
                    offerId, existing.getId(), existing.getStartedAt());
-
 
83
            return;
-
 
84
        }
-
 
85
 
74
        CreateOfferRequest createOfferRequest = offerProcessingHelper.loadOfferRequest(offerId);
86
        CreateOfferRequest createOfferRequest = offerProcessingHelper.loadOfferRequest(offerId);
75
        if (createOfferRequest == null) {
87
        if (createOfferRequest == null) {
76
            return;
88
            return;
77
        }
89
        }
78
        LOGGER.info("Processing offer {} (type={}) with batch tracking", offerId, createOfferRequest.getSchemeType());
90
        LOGGER.info("Processing offer {} (type={}) with batch tracking", offerId, createOfferRequest.getSchemeType());