Subversion Repositories SmartDukaan

Rev

Rev 36253 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30209 amit.gupta 1
package com.smartdukaan.cron.scheduled;
2
 
3
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
4
import org.apache.logging.log4j.LogManager;
5
import org.apache.logging.log4j.Logger;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.stereotype.Service;
8
 
9
import java.time.LocalDate;
10
import java.util.List;
11
import java.util.Map;
12
 
13
@Service
14
public class OppoImeiActivationService {
15
 
16
	private static final Logger LOGGER = LogManager.getLogger(OppoImeiActivationService.class);
17
	@Autowired
18
	ActivatedImeiRepository activatedImeiRepository;
30372 amit.gupta 19
	@Autowired
20
	CheckOppoWarrantyTask checkOppoWarrantyTask;
30209 amit.gupta 21
 
22
	public void updateActivationDate(List<String> imeis) throws Exception {
30372 amit.gupta 23
		Map<String, LocalDate> imeisDateMap = checkOppoWarrantyTask.checkWarranty(imeis);
36253 amit 24
		imeisDateMap.forEach((serialNumber, date) -> {
25
			LOGGER.info("Serial Number {} Date {}", serialNumber, date);
26
			activatedImeiRepository.saveActivation(serialNumber, date != null ? date.atStartOfDay() : null);
30372 amit.gupta 27
		});
37565 amit 28
		restUnanswered(imeis, imeisDateMap);
30372 amit.gupta 29
	}
30
 
37565 amit 31
	/**
32
	 * Stamp the imeis that never reached an answer, so the chunk advances past them.
33
	 *
34
	 * checkWarranty already records a null date for a lookup the portal ANSWERED with no
35
	 * activation, so what is left here is genuine failure: an exception, a captcha never
36
	 * broken, a dead driver. Those wrote no row at all, which left createTimestamp
37
	 * untouched and the imei immediately due again.
38
	 *
39
	 * Under the old snapshotted once-a-day pass that was harmless -- the pass had already
40
	 * walked past them, so they came back tomorrow either way. Under a 20-second tick the
41
	 * pool query IS the cursor, so an unstamped imei is handed back twenty seconds later
42
	 * and the batch never advances. That is the 4.5-asks-per-imei runaway measured on
43
	 * 29-Aug, and at this cadence it is 15x tighter than the 5-minute version that caused it.
44
	 *
45
	 * saveActivation(serial, null) bumps createTimestamp without claiming a date, and it
46
	 * cannot erase one: the setter is only reached for a non-null timestamp. So a failure
47
	 * costs exactly one retry, tomorrow.
48
	 */
49
	private void restUnanswered(List<String> imeis, Map<String, LocalDate> answered) {
50
		for (String serialNumber : imeis) {
51
			if (!answered.containsKey(serialNumber)) {
52
				LOGGER.info("Serial Number {} unanswered, resting until tomorrow", serialNumber);
53
				activatedImeiRepository.saveActivation(serialNumber, null);
54
			}
55
		}
56
	}
57
 
36253 amit 58
}