package de.htwsaar.webshop.cronjob;

import de.htwsaar.webshop.service.SessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static de.htwsaar.webshop.util.TimeUtil.MILLIS_TO_WEEK;

/**
 * CronJob for deleting expired sessions in fixed intervals
 */
@Component
@Slf4j
public class ExpiredSessionDeleteJob {

    private final SessionService sessionService;

    /**
     * Constructor of the class
     * @param sessionService used in the class
     */
    public ExpiredSessionDeleteJob(SessionService sessionService) {
        this.sessionService = sessionService;
    }

    /**
     * Method running the job calling the referring service method
     */
    @Scheduled(fixedRate = MILLIS_TO_WEEK)
    public void runFixedRateTask() {
        log.info("Deleting expired sessions...");
        sessionService.deleteExpired();
        log.info("Deleted expired sessions.");
    }
}