136 lines
3.7 KiB
Java
136 lines
3.7 KiB
Java
/**
|
|
* Copyright (C) 2017-2018 Christian Pierre MOMON <cmomon@april.org>
|
|
* Copyright (C) 2011-2013,2017 Nicolas Vinot <aeris@imirhil.fr>
|
|
*
|
|
* This file is part of (April) Hebdobot.
|
|
*
|
|
* Hebdobot is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Hebdobot is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
package org.april.hebdobot.cron;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import org.april.hebdobot.bot.Hebdobot;
|
|
import org.quartz.JobDataMap;
|
|
import org.quartz.Scheduler;
|
|
import org.quartz.SchedulerException;
|
|
import org.quartz.impl.JobDetailImpl;
|
|
import org.quartz.impl.StdSchedulerFactory;
|
|
import org.quartz.impl.triggers.CronTriggerImpl;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* The Class CronManager.
|
|
*/
|
|
public class CronManager
|
|
{
|
|
private static final Logger logger = LoggerFactory.getLogger(CronManager.class);
|
|
|
|
private CronSettings crons;
|
|
private Scheduler scheduler;
|
|
private Hebdobot bot;
|
|
|
|
/**
|
|
* Instantiates a new cron manager.
|
|
*
|
|
* @param bot
|
|
* the bot
|
|
* @param crons
|
|
* the crons
|
|
* @throws SchedulerException
|
|
* the scheduler exception
|
|
* @throws ParseException
|
|
* the parse exception
|
|
*/
|
|
public CronManager(final Hebdobot bot, final CronSettings crons) throws SchedulerException, ParseException
|
|
{
|
|
this.bot = bot;
|
|
this.crons = crons;
|
|
this.scheduler = new StdSchedulerFactory().getScheduler();
|
|
|
|
for (CronValue cron : crons)
|
|
{
|
|
addCron(cron);
|
|
}
|
|
this.scheduler.start();
|
|
}
|
|
|
|
/**
|
|
* Adds the cron.
|
|
*
|
|
* @param cron
|
|
* the cron
|
|
* @throws SchedulerException
|
|
* the scheduler exception
|
|
* @throws ParseException
|
|
* the parse exception
|
|
*/
|
|
public void addCron(final CronValue cron) throws SchedulerException, ParseException
|
|
{
|
|
{
|
|
final JobDetailImpl job = new JobDetailImpl();
|
|
job.setName(cron.getName());
|
|
job.setDescription(cron.getDescription());
|
|
job.setJobClass(NotifyJob.class);
|
|
|
|
JobDataMap data = new JobDataMap();
|
|
data.put("bot", this.bot);
|
|
data.put("cron", cron);
|
|
job.setJobDataMap(data);
|
|
|
|
CronTriggerImpl trigger = new CronTriggerImpl();
|
|
trigger.setName(cron.getName());
|
|
trigger.setDescription(cron.getDescription());
|
|
trigger.setCronExpression(cron.getCron());
|
|
|
|
this.scheduler.scheduleJob(job, trigger);
|
|
}
|
|
|
|
logger.info("Added cron value: " + cron.getName());
|
|
}
|
|
|
|
/**
|
|
* Gets the crons.
|
|
*
|
|
* @return the crons
|
|
*/
|
|
public CronSettings getCrons()
|
|
{
|
|
return this.crons;
|
|
}
|
|
|
|
/**
|
|
* Shutdown.
|
|
*
|
|
* @throws SchedulerException
|
|
* the scheduler exception
|
|
*/
|
|
public void shutdown() throws SchedulerException
|
|
{
|
|
this.scheduler.shutdown();
|
|
}
|
|
|
|
/**
|
|
* Start.
|
|
*
|
|
* @throws SchedulerException
|
|
* the scheduler exception
|
|
*/
|
|
public void start() throws SchedulerException
|
|
{
|
|
this.scheduler.start();
|
|
}
|
|
}
|