package cn.reghao.tnb.content.app.data.spider;

import cn.reghao.jutil.jdk.http.WebRequest;
import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
import cn.reghao.jutil.tool.http.DefaultWebRequest;
import cn.reghao.tnb.content.app.data.db.mongo.RichTextMongo;
import cn.reghao.tnb.content.app.data.spider.task.GetNewsDetailTask;
import cn.reghao.tnb.content.app.data.spider.task.GetNewsIndexTask;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
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 DefaultWebRequest();
    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);
    }
}
