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