package ervu_business_metrics.service.scheduler;

import javax.annotation.PostConstruct;

import ervu_business_metrics.config.KafkaEnabledCondition;
import ervu_business_metrics.service.SessionService;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.springframework.context.annotation.Conditional;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * @author Adel Kalimullin
 */
@Component
@Conditional(KafkaEnabledCondition.class)
public class SessionScheduledService {
  private final SessionService sessionService;

  public SessionScheduledService(SessionService sessionService) {
    this.sessionService = sessionService;
  }

  @PostConstruct
  public void init() {
      run();
  }

  @Scheduled(cron = "${session.sync.cron:0 */10 * * * *}")
  @SchedulerLock(name = "syncSessions")
  public void run() {
    sessionService.syncSessions();
  }
}
