package org.jgrades.logging.job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class JobsStarter {
    public static final String CONF_UPDATER_JOB_NAME = "extConfMonitorJob";
    public static final String CONF_UPDATER_JOB_TRIGGER_NAME = "extConfMonitorTrigger";
    public static final int CONF_UPDATER_JOB_INTERVAL_SECONDS = 5;

    public static final String LOG_CLEANER_JOB_NAME = "oldLogFilesCleanerJob";
    public static final String LOG_CLEANER_JOB_TRIGGER_NAME = "oldLogFilesCleanerTrigger";
    public static final String LOG_CLEANER_JOB_CRON_EXP = "0 0 0 * * ?";

    public static final String JOB_GROUP_NAME = "jg-logging";

    public void start() {
        try {
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.start();

            scheduleConfUpdaterJob(scheduler);
            scheduleLogCleanerJob(scheduler);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    private void scheduleConfUpdaterJob(Scheduler scheduler) throws SchedulerException {
        JobDetail confUpdaterJob = newJob(LogConfigurationMonitor.class)
                .withIdentity(CONF_UPDATER_JOB_NAME, JOB_GROUP_NAME)
                .build();

        Trigger confUpdaterTrigger = newTrigger()
                .withIdentity(CONF_UPDATER_JOB_TRIGGER_NAME, JOB_GROUP_NAME)
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(CONF_UPDATER_JOB_INTERVAL_SECONDS)
                        .repeatForever())
                .build();

        scheduler.scheduleJob(confUpdaterJob, confUpdaterTrigger);
    }

    private void scheduleLogCleanerJob(Scheduler scheduler) throws SchedulerException {
        JobDetail logCleanerJob = newJob(OldLogFilesCleaner.class)
                .withIdentity(LOG_CLEANER_JOB_NAME, JOB_GROUP_NAME)
                .build();

        Trigger logCleanerTrigger = newTrigger()
                .withIdentity(LOG_CLEANER_JOB_TRIGGER_NAME, JOB_GROUP_NAME)
                .startNow()
                .withSchedule(
                        cronSchedule(LOG_CLEANER_JOB_CRON_EXP))
                .build();

        scheduler.scheduleJob(logCleanerJob, logCleanerTrigger);
    }
}

@@ -27,16 +27,55 @@
 g.job;%0A%0A
+import com.google.common.collect.Sets;%0A
 import o
@@ -772,32 +772,139 @@
 leanerTrigger%22;%0A
+    public static final String LOG_CLEANER_START_JOB_TRIGGER_NAME = %22oldLogFilesCleanerTrigger_OnStartUp%22;%0A
     public stati
@@ -2503,112 +2503,252 @@
    .
-startNow()%0A                .withSchedule(%0A                        cronSchedule(LOG_CLEANER_JOB_CRON_EXP)
+withSchedule(cronSchedule(LOG_CLEANER_JOB_CRON_EXP))%0A                .build();%0A%0A        Trigger logCleanerStartupTrigger = newTrigger()%0A                .withIdentity(LOG_CLEANER_START_JOB_TRIGGER_NAME, JOB_GROUP_NAME)%0A                .startNow(
 )%0A  
@@ -2817,16 +2817,32 @@
 nerJob, 
+Sets.newHashSet(
 logClean
@@ -2842,27 +2842,60 @@
 ogCleanerTrigger
+, logCleanerStartupTrigger), true
 );%0A    %7D%0A%7D%0A

Fix #32: OldLogFilesCleaner is not starting on application launch