| 36306 |
amit |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.dao.entity.transaction.CronBatch;
|
| 36338 |
amit |
4 |
import com.spice.profitmandi.service.cron.CronBatchService;
|
| 36343 |
amit |
5 |
import com.spice.profitmandi.service.offers.OfferBatchService;
|
| 36306 |
amit |
6 |
import com.spice.profitmandi.service.transaction.PartnerLimitUpdateData;
|
|
|
7 |
import org.apache.logging.log4j.LogManager;
|
|
|
8 |
import org.apache.logging.log4j.Logger;
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.stereotype.Component;
|
|
|
11 |
|
|
|
12 |
import java.util.LinkedHashMap;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* Batch-aware scheduled tasks with per-partner transaction isolation.
|
|
|
17 |
* NO @Transactional at class level — each partner gets its own transaction
|
|
|
18 |
* via REQUIRES_NEW in the helper beans.
|
|
|
19 |
*
|
|
|
20 |
* Tracks every run in cron_batch / cron_batch_item tables.
|
| 36343 |
amit |
21 |
* Sends failure summary email on partial failures.
|
| 36306 |
amit |
22 |
*/
|
|
|
23 |
@Component
|
|
|
24 |
public class BatchScheduledTasks {
|
|
|
25 |
|
|
|
26 |
private static final Logger LOGGER = LogManager.getLogger(BatchScheduledTasks.class);
|
|
|
27 |
|
|
|
28 |
@Autowired
|
|
|
29 |
private CronBatchService cronBatchService;
|
|
|
30 |
|
|
|
31 |
@Autowired
|
| 36343 |
amit |
32 |
private OfferBatchService offerBatchService;
|
| 36306 |
amit |
33 |
|
|
|
34 |
@Autowired
|
|
|
35 |
private PartnerLimitHelper partnerLimitHelper;
|
|
|
36 |
|
|
|
37 |
/**
|
| 36343 |
amit |
38 |
* CLI entrypoint for cron: delegates to shared OfferBatchService (also used by web/fofo controllers).
|
| 36306 |
amit |
39 |
*/
|
|
|
40 |
public void processOfferWithBatch(int offerId) throws Exception {
|
| 36343 |
amit |
41 |
offerBatchService.processOfferWithBatch(offerId);
|
| 36306 |
amit |
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Recalculates partner credit limits. Only writes to partners where values actually changed.
|
|
|
46 |
*
|
|
|
47 |
* Flow:
|
|
|
48 |
* 1. Read phase: calculate limits for all 1,500 partners, compare with current values
|
|
|
49 |
* 2. Create batch with only changed partners (~50-100 typically)
|
|
|
50 |
* 3. Per-partner: update in REQUIRES_NEW transaction
|
|
|
51 |
* 4. Finalize: counts + failure email
|
|
|
52 |
*/
|
|
|
53 |
public void updatePartnerLimitWithBatch() throws Exception {
|
|
|
54 |
List<PartnerLimitUpdateData> changedPartners;
|
|
|
55 |
try {
|
|
|
56 |
changedPartners = partnerLimitHelper.calculateChangedPartnerLimits();
|
|
|
57 |
} catch (Exception e) {
|
|
|
58 |
LOGGER.error("Failed to calculate partner limits: {}", e.getMessage());
|
|
|
59 |
return;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
if (changedPartners.isEmpty()) {
|
|
|
63 |
LOGGER.info("No partner limits changed, skipping");
|
|
|
64 |
return;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
LinkedHashMap<Integer, String> fofoIdPartnerNameMap = new LinkedHashMap<>();
|
|
|
68 |
for (PartnerLimitUpdateData data : changedPartners) {
|
|
|
69 |
fofoIdPartnerNameMap.put(data.getFofoId(), "fofo-" + data.getFofoId());
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
CronBatch batch = cronBatchService.createBatch("updatePartnerLimit", fofoIdPartnerNameMap);
|
|
|
73 |
|
|
|
74 |
for (PartnerLimitUpdateData data : changedPartners) {
|
|
|
75 |
try {
|
|
|
76 |
partnerLimitHelper.updateSinglePartnerLimit(batch.getId(), data);
|
|
|
77 |
} catch (Exception e) {
|
|
|
78 |
LOGGER.error("updatePartnerLimit failed for fofoId={}: {}", data.getFofoId(), e.getMessage());
|
|
|
79 |
cronBatchService.markItemFailed(batch.getId(), data.getFofoId(), e.getMessage());
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
cronBatchService.finalizeBatch(batch.getId());
|
|
|
84 |
}
|
|
|
85 |
}
|