Subversion Repositories SmartDukaan

Rev

Rev 37448 | Rev 37472 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.smartdukaan.cron.scheduled;

import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StandAlone {

        @Autowired
        private OppoImeiActivationService oppoImeiActivationService;

        @Autowired
        private RealmeImeiActivationService realmeImeiActivationService;

        @Autowired
        private MotorolaImeiActivationService motorolaImeiActivationService;

        @Autowired
        private ActivatedImeiRepository activatedImeiRepository;

        private static final Logger LOGGER = LogManager.getLogger(StandAlone.class);

        public void checkOppoImeiStatus() throws Exception {
                List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByBrand("Oppo", 1, 25);
                List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
                LOGGER.info("Oppo secondary imeis {}", imeis);
                oppoImeiActivationService.updateActivationDate(imeis);
        }

        public void checkOppoImeiStatusTertiary() throws Exception {
                List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Oppo", 1, 25);
                List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
                LOGGER.info("Oppo tertiary imeis {}", imeis);
                oppoImeiActivationService.updateActivationDate(imeis);
        }

        public void checkRealmeImeiStatus() throws Exception {
                List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByRealme("Realme", 1, 12);
                List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
                LOGGER.info("Realme secondary imeis {}", imeis);
                realmeImeiActivationService.updateActivationDate(imeis);
        }

        public void checkRealmeImeiStatusTertiary() throws Exception {
                List<ImeiActivationTimestampModel> imeiActivationTimestampModels = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Realme", 1, 12);
                List<String> imeis = imeiActivationTimestampModels.stream().map(x -> x.getSerialNumber()).collect(Collectors.toList());
                LOGGER.info("Realme tertiary imeis {}", imeis);
                realmeImeiActivationService.updateActivationDate(imeis);
        }

        /**
         * Secondary + tertiary in ONE browser session per brand.
         *
         * They were separate @Scheduled jobs, so each brand span up two ChromeDrivers at
         * once and the fleet ran four. Each driver tree costs roughly 850MB, and this box
         * co-hosts a 9.4GB tomcat with ~3GB available -- it has been OOM-killed twice this
         * month and tomcat was the victim both times. Halving concurrent drivers is the
         * single biggest memory saving available here.
         *
         * Nothing else changes: the two pools differ only in which named query fills them,
         * and both already funnel into the same updateActivationDate -> checkWarranty ->
         * saveActivation path. The pools are disjoint by construction (secondary excludes
         * anything with a FofoLineItem, tertiary is FofoLineItem-based); distinct() is cheap
         * insurance rather than a fix for a known overlap.
         *
         *
         * Sizing: merging SERIALISES work that used to run in parallel, so keeping the old
         * batch sizes would cost throughput. Measured post-cap at 10.2s/imei (oppo) and
         * 14.2s/imei (realme), solving M * 86400 / (300 + M*t):
         *   oppo    2 parallel jobs x10 = 4,299/day  ->  merged 30 = 4,277/day  (parity)
         *   realme  2 parallel jobs x10 = 3,910/day  ->  merged 20 = 2,959/day  (-24%)
         * Oppo is sized to hold parity because it is already short of its 4,798/day need.
         * Realme is left at 20 -- it needs 2,243/day, so it can absorb the dip in exchange
         * for shorter batches.
         *
         * Note what this does and does not save: total driver-SECONDS are unchanged (that is
         * the point of resizing), but PEAK concurrent drivers halves from 4 to 2. Peak is
         * what triggers the OOM killer, not average.
         *
         * The per-pool log lines are kept so existing log analysis still works.
         */
        public void checkOppoImeiStatusCombined() throws Exception {
                List<String> secondary = activatedImeiRepository.selectImeiActivationPendingByBrand("Oppo", 1, 15)
                                .stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
                List<String> tertiary = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Oppo", 1, 15)
                                .stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
                LOGGER.info("Oppo secondary imeis {}", secondary);
                LOGGER.info("Oppo tertiary imeis {}", tertiary);
                List<String> all = new ArrayList<>(secondary);
                all.addAll(tertiary);
                all = all.stream().distinct().collect(Collectors.toList());
                if (all.isEmpty()) {
                        LOGGER.info("Oppo: nothing pending, not starting a browser");
                        return;
                }
                oppoImeiActivationService.updateActivationDate(all);
        }

        public void checkRealmeImeiStatusCombined() throws Exception {
                List<String> secondary = activatedImeiRepository.selectImeiActivationPendingByRealme("Realme", 1, 12)
                                .stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
                List<String> tertiary = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Realme", 1, 12)
                                .stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
                LOGGER.info("Realme secondary imeis {}", secondary);
                LOGGER.info("Realme tertiary imeis {}", tertiary);
                List<String> all = new ArrayList<>(secondary);
                all.addAll(tertiary);
                all = all.stream().distinct().collect(Collectors.toList());
                if (all.isEmpty()) {
                        LOGGER.info("Realme: nothing pending, not starting a browser");
                        return;
                }
                realmeImeiActivationService.updateActivationDate(all);
        }

        /**
         * Motorola: secondary + tertiary in ONE browser session, same shape as the
         * oppo/realme combined jobs.
         *
         * Cadence: the pool query defers an imei for `days` after each attempt
         * (saveActivation bumps createTimestamp even when no date came back), so
         * days=2 gives the requested "retry everything every two days".
         *
         * Sizing: the pending pool measured 1,534 (1,182 secondary + 352 tertiary).
         * At the ~10-14s/imei the oppo and realme jobs measure, 60 per invocation is
         * roughly 12 minutes of driver time, and clearing 1,534 inside 48h needs
         * about 26 invocations -- i.e. an OS cron entry every 90 minutes, with
         * headroom. Do NOT schedule it inside the oppo/realme window: each driver
         * tree costs ~850MB and this box has been OOM-killed twice with tomcat the
         * victim, so peak concurrent drivers is the number that matters.
         */
        public void checkMotorolaImeiStatusCombined() throws Exception {
                List<String> secondary = activatedImeiRepository.selectImeiActivationPendingByBrand("Motorola", 2, 30)
                                .stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
                List<String> tertiary = activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Motorola", 2, 30)
                                .stream().map(ImeiActivationTimestampModel::getSerialNumber).collect(Collectors.toList());
                LOGGER.info("Motorola secondary imeis {}", secondary);
                LOGGER.info("Motorola tertiary imeis {}", tertiary);
                List<String> all = new ArrayList<>(secondary);
                all.addAll(tertiary);
                all = all.stream().distinct().collect(Collectors.toList());
                if (all.isEmpty()) {
                        LOGGER.info("Motorola: nothing pending, not starting a browser");
                        return;
                }
                motorolaImeiActivationService.updateActivationDate(all);
        }

}