package cn.reghao.tnb.search.app.comment.spider;

import cn.reghao.jutil.jdk.http.WebClient;
import cn.reghao.jutil.jdk.http.WebRequest;
import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
import cn.reghao.tnb.search.app.comment.db.mongo.RichTextMongo;
import cn.reghao.tnb.search.app.comment.spider.task.GetNewsDetailTask;
import cn.reghao.tnb.search.app.comment.spider.task.GetNewsIndexTask;
import org.springframework.stereotype.Component;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author reghao
 * @date 2025-07-30 15:48:04
 */
@Component
public class TaskContext {
    private final ScheduledExecutorService scheduler = ThreadPoolWrapper.scheduledThreadPool("get-news", 5);
    private final WebRequest webRequest = new WebClient();
    private final RichTextMongo richTextMongo;

    public TaskContext(RichTextMongo richTextMongo) {
        this.richTextMongo = richTextMongo;
    }

    //@PostConstruct
    public void startTask() {
        GetNewsDetailTask getNewsDetailTask = new GetNewsDetailTask(webRequest, richTextMongo);
        scheduler.scheduleAtFixedRate(getNewsDetailTask, 0, 1, TimeUnit.HOURS);

        int pastHours = 4;
        GetNewsIndexTask getNewsIndexTask = new GetNewsIndexTask(webRequest, richTextMongo, pastHours);
        ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(getNewsIndexTask, 0, pastHours, TimeUnit.HOURS);
    }
}
