IndexRebuildJob.java 5.9 KB
package com.yoho.search.consumer.job;

import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.index.rebuild.RebuildFlagService;
import com.yoho.search.consumer.service.daoService.ProductPriceService;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class IndexRebuildJob implements ApplicationEventPublisherAware {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private ApplicationEventPublisher publisher;

    @Autowired
    private IYohoIndexService yohoIndexService;
    @Autowired
    private RebuildFlagService rebuildFlagService;
    @Autowired
    private ProductPriceService productPriceService;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }

    @Scheduled(cron = "0 50 * * * ?")
    public void rebuildTblProductIndex() {
        long begin = System.currentTimeMillis();
        logger.info("indexRebuildJob rebuildTblProductIndex start----[begin={}]", begin);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_TBLPRODUCT);
        logger.info("indexRebuildJob rebuildTblProductIndex end----[end={}][cost={}]", System.currentTimeMillis(), (System.currentTimeMillis() - begin));
    }

    @Scheduled(cron = "0 0 3 * * ?")
    public void rebuildIndexTask() {
        productPriceService.batchUpdateHistoryPrice();
        execute();
    }

    public void execute() {
        long begin = System.currentTimeMillis();
        logger.info("indexRebuildJob execute start----[begin={}]", begin);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PROMOTIONINDEX);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_ACTIVITY_TAG);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_BRAND);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_SIZE);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_COLOR);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_SORT);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_STYLE);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_STANDARD);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_ROBOTQUESTION);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_ROBOTQUESTION_OLD);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_PRICE_PLAN);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_HELPER);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_SHOPS);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_ZQ_NAME);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PROMOTIONTYPE);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_IMAGE_VECTORS);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_BIGDATESIMILARSKNINDEX);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_BRAND);//依赖pi重建过后的向量
        logger.info("indexRebuildJob execute end----[end={}][cost={}]", System.currentTimeMillis(), (System.currentTimeMillis() - begin));
    }

    public void rebuildSuggestIndex() {
        // suggest索引由于需要依赖productindex索引计算关键词的count 由SuggestionJob触发执行
        long begin = System.currentTimeMillis();
        logger.info("indexRebuildJob rebuildSuggestIndex start----[begin={}]", begin);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_SUGGEST);
        logger.info("indexRebuildJob rebuildSuggestIndex end----[cost={}]", System.currentTimeMillis() - begin);
    }

    public void rebuildConversionIndex() {
        // conversion索引由于需要suggest索引的重建 由SpiderJob触发执行
        long begin = System.currentTimeMillis();
        logger.info("indexRebuildJob rebuildConversionIndex start----[begin={}]", begin);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_CONVERSION);
        logger.info("indexRebuildJob rebuildConversionIndex end----[cost={}]", System.currentTimeMillis() - begin);
    }
    
    
    /**
     * 重建品牌索引【for更新向量】
     */
    public void rebuildBrandIndex() {
        long begin = System.currentTimeMillis();
        logger.info("indexRebuildJob rebuildBrandIndex start----[begin={}]", begin);
        this.rebuildIndexWithlog(ISearchConstants.INDEX_NAME_BRAND);
        logger.info("indexRebuildJob rebuildConversionIndex end----[cost={}]", System.currentTimeMillis() - begin);
    }

    private void rebuildIndexWithlog(String indexName) {
        try {
            long begin = System.currentTimeMillis();
            logger.info("rebuild index start----[indexName={}][begin={}]", indexName, begin);
            rebuildFlagService.waitingRebuildingIndex();
            yohoIndexService.rebuild(indexName);
            logger.info("rebuild index end----[indexName={}][end={}][cost={}ms]", indexName, System.currentTimeMillis(), (System.currentTimeMillis() - begin));
        } catch (Exception e) {
            publisher.publishEvent(new SearchEvent(EventReportEnum.INDEXREBUILDJOB_REBUILDINDEXWITHLOG.getEventName(), EventReportEnum.INDEXREBUILDJOB_REBUILDINDEXWITHLOG
                    .getFunctionName(), EventReportEnum.INDEXREBUILDJOB_REBUILDINDEXWITHLOG.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
            logger.error(e.getMessage(), e);
        }
    }

}