package com.lanyuanxiaoyao.leopard.server;

import com.lanyuanxiaoyao.leopard.core.repository.TaskRepository;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

/**
 * @author lanyuanxiaoyao
 * @version 20250828
 */
@Slf4j
@SpringBootApplication(scanBasePackages = "com.lanyuanxiaoyao.leopard")
@EnableJpaAuditing
public class LeopardServerApplication implements ApplicationRunner {
  private final TaskRepository taskRepository;

  public LeopardServerApplication(TaskRepository taskRepository) {
    this.taskRepository = taskRepository;
  }

  public static void main(String[] args) {
    SpringApplication.run(LeopardServerApplication.class, args);
  }

  @Transactional(rollbackOn = Throwable.class)
  @Override
  public void run(ApplicationArguments args) {
    log.warn("更新所有未完成的任务状态为失败");
    taskRepository.updateAllRunningTaskToFailure();
  }
}
