Subversion Repositories SmartDukaan

Rev

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

Rev 37448 Rev 37472
Line 24... Line 24...
24
import java.io.IOException;
24
import java.io.IOException;
25
import java.nio.charset.StandardCharsets;
25
import java.nio.charset.StandardCharsets;
26
import java.time.LocalDate;
26
import java.time.LocalDate;
27
import java.time.format.DateTimeFormatter;
27
import java.time.format.DateTimeFormatter;
28
import java.util.ArrayList;
28
import java.util.ArrayList;
-
 
29
import java.util.HashSet;
29
import java.util.List;
30
import java.util.List;
-
 
31
import java.util.Set;
-
 
32
import java.util.function.Predicate;
-
 
33
import java.util.stream.Collectors;
30
 
34
 
31
@Component
35
@Component
32
class VivoImeiActivationService {
36
class VivoImeiActivationService {
33
 
37
 
34
    @Autowired
38
    @Autowired
Line 40... Line 44...
40
    @Autowired
44
    @Autowired
41
    ActivatedImeiRepository activatedImeiRepository;
45
    ActivatedImeiRepository activatedImeiRepository;
42
 
46
 
43
    private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
47
    private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
44
 
48
 
-
 
49
    /** Days an imei rests after an ANSWER. 0 = due again in the next day's pass. */
-
 
50
    private static final int DAYS = 0;
-
 
51
 
-
 
52
    /** Safety stop so a mis-set DAYS cannot pull an unbounded list into memory. */
-
 
53
    private static final int POOL_CAP = 20000;
-
 
54
 
-
 
55
    /**
-
 
56
     * ONE FULL PASS PER DAY over everything due, then nothing until tomorrow.
-
 
57
     *
-
 
58
     * This was two @Scheduled jobs running in parallel every 5 minutes, capped at 50
-
 
59
     * (secondary) and 10 (tertiary) imeis a tick -- so it never finished a pass over a
-
 
60
     * 9,510-row pool, and any imei whose lookup failed was retried five minutes later
-
 
61
     * rather than the next day. The pool is snapshotted here and walked to the end; it
-
 
62
     * is never re-queried mid-pass, so no imei is asked twice inside a run.
-
 
63
     *
-
 
64
     * The secondary/tertiary split is only two join paths to the same serial, so they
-
 
65
     * are concatenated into one list. distinct() is insurance: the secondary query
-
 
66
     * already excludes anything carrying a FofoLineItem.
-
 
67
     *
-
 
68
     * The 1st-of-month skip is kept -- vivo's own portal is unreliable on that day.
-
 
69
     */
45
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
70
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
46
    public void checkImeiActivation() throws Exception {
71
    public void checkImeiActivation() throws Exception {
47
        if (LocalDate.now().getDayOfMonth() == 1) return;
72
        if (LocalDate.now().getDayOfMonth() == 1) return;
48
 
73
 
-
 
74
        List<ImeiActivationTimestampModel> pool = new ArrayList<>();
-
 
75
        pool.addAll(activatedImeiRepository.selectImeiActivationPendingByBrand("Vivo", DAYS, POOL_CAP));
-
 
76
        pool.addAll(activatedImeiRepository.selectImeiActivationPendingByBrandTertiary("Vivo", DAYS, POOL_CAP));
-
 
77
 
49
        List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
78
        List<ImeiActivationTimestampModel> imeisActivationList = pool.stream()
-
 
79
                .filter(distinctBySerial())
50
                .selectImeiActivationPendingByBrand("Vivo", 1, 50);
80
                .collect(Collectors.toList());
51
        LOGGER.info("Secondary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());
81
        LOGGER.info("Vivo daily pass starting: {} imeis due", imeisActivationList.size());
52
 
82
 
53
        processImeis(imeisActivationList);
83
        processImeis(imeisActivationList);
54
    }
-
 
55
 
-
 
56
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
-
 
57
    public void checkImeiActivationTertiary() throws Exception {
-
 
58
        if (LocalDate.now().getDayOfMonth() == 1) return;
-
 
59
 
84
 
60
        List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
-
 
61
                .selectImeiActivationPendingByBrandTertiary("Vivo", 1, 10);
-
 
62
        LOGGER.info("Tertiary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());
85
        LOGGER.info("Vivo daily pass finished: {} imeis", imeisActivationList.size());
-
 
86
    }
63
 
87
 
-
 
88
    private static Predicate<ImeiActivationTimestampModel> distinctBySerial() {
64
        processImeis(imeisActivationList);
89
        Set<String> seen = new HashSet<>();
-
 
90
        return m -> seen.add(m.getSerialNumber());
65
    }
91
    }
66
 
92
 
67
    private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
93
    private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
68
        if (imeisActivationList.isEmpty()) return;
94
        if (imeisActivationList.isEmpty()) return;
69
 
95