package de.ivu.elect.jee.memory;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.ejb.DependsOn;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.jee.cron.AbstractSingleCronTimer;

@Startup
@Singleton
@DependsOn("StartupSingleton")
public class MemoryGcCronTimer extends AbstractSingleCronTimer {

  private static final Logger LOG = LoggerFactory.getLogger(MemoryGcCronTimer.class);

  @Inject
  private transient MemoryGcKonfiguration memoryGcKonfiguration;

  @Inject
  private transient MemoryGc memoryGc;

  @PostConstruct
  public void startup() {
    createTimer();
  }

  @Override
  protected void postCreateTimer() {
    super.postCreateTimer();
    memoryGc.speichereDaten();
  }

  @Nullable
  @Override
  protected String getCrontab() {
    return memoryGcKonfiguration.getCrontab();
  }

  @Override
  protected void timeout() {
    MemoryGcData memoryGcData = memoryGc.speichereDaten();
    if (memoryGcKonfiguration.isLoggeAusgaben()){
      LOG.info("MemoryGcTimeout: " + memoryGcData);
    }
  }

  @Override
  public String getJobName() {
    return memoryGcKonfiguration.getJobName();
  }

  @Override
  protected boolean isLoggeHandleTimeout() {
    return false;
  }
}
