Rev 36338 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.smartdukaan.cron.scheduled;import com.spice.profitmandi.dao.entity.transaction.CronBatch;import com.spice.profitmandi.service.cron.CronBatchService;import com.spice.profitmandi.service.offers.OfferBatchService;import com.spice.profitmandi.service.transaction.PartnerLimitUpdateData;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.util.LinkedHashMap;import java.util.List;/*** Batch-aware scheduled tasks with per-partner transaction isolation.* NO @Transactional at class level — each partner gets its own transaction* via REQUIRES_NEW in the helper beans.** Tracks every run in cron_batch / cron_batch_item tables.* Sends failure summary email on partial failures.*/@Componentpublic class BatchScheduledTasks {private static final Logger LOGGER = LogManager.getLogger(BatchScheduledTasks.class);@Autowiredprivate CronBatchService cronBatchService;@Autowiredprivate OfferBatchService offerBatchService;@Autowiredprivate PartnerLimitHelper partnerLimitHelper;/*** CLI entrypoint for cron: delegates to shared OfferBatchService (also used by web/fofo controllers).*/public void processOfferWithBatch(int offerId) throws Exception {offerBatchService.processOfferWithBatch(offerId);}/*** Recalculates partner credit limits. Only writes to partners where values actually changed.** Flow:* 1. Read phase: calculate limits for all 1,500 partners, compare with current values* 2. Create batch with only changed partners (~50-100 typically)* 3. Per-partner: update in REQUIRES_NEW transaction* 4. Finalize: counts + failure email*/public void updatePartnerLimitWithBatch() throws Exception {List<PartnerLimitUpdateData> changedPartners;try {changedPartners = partnerLimitHelper.calculateChangedPartnerLimits();} catch (Exception e) {LOGGER.error("Failed to calculate partner limits: {}", e.getMessage());return;}if (changedPartners.isEmpty()) {LOGGER.info("No partner limits changed, skipping");return;}LinkedHashMap<Integer, String> fofoIdPartnerNameMap = new LinkedHashMap<>();for (PartnerLimitUpdateData data : changedPartners) {fofoIdPartnerNameMap.put(data.getFofoId(), "fofo-" + data.getFofoId());}CronBatch batch = cronBatchService.createBatch("updatePartnerLimit", fofoIdPartnerNameMap);for (PartnerLimitUpdateData data : changedPartners) {try {partnerLimitHelper.updateSinglePartnerLimit(batch.getId(), data);} catch (Exception e) {LOGGER.error("updatePartnerLimit failed for fofoId={}: {}", data.getFofoId(), e.getMessage());cronBatchService.markItemFailed(batch.getId(), data.getFofoId(), e.getMessage());}}cronBatchService.finalizeBatch(batch.getId());}}