using quartz;
using quartz.impl;
using quartz.spi;
using system;
using system.collections.generic;
using system.linq;
using system.reflection;
using system.text;
using system.threading;
using system.threading.tasks;
using wpf.quartz.converters;
namespace wpf.quartz
{
public class simplerunner<t> : baserunner,irun where t : ijob
{
public simplerunner()
{
base.name = this.name;
}
private static readonly log4net.ilog log = log4net.logmanager.getlogger(system.reflection.methodbase.getcurrentmethod().declaringtype);
public new string name
{
//get { return gettype().name; }
get { return typeof(t).name; }
}
private readonly semaphoreslim locker = new semaphoreslim(1);
public virtual async task start()
{
if (statusenableconverter.iseanblestate(triggerstate, "start") == false)
{
return;
}
await locker.waitasync();
try
{
if (string.isnullorempty(cronexpression))//如果为空,则设置为立刻执行一次
{
cronexpression = cronhelper.datetime2cron(datetime.now.addseconds(3));
}
info((displayname ?? name) + "开始启动!");
triggerstate= await quartzhelper.instance.startjobexecutebycron<t>(cronexpression, name, this, this.starttime, this.endtime);
info((displayname ?? name) + "启动完毕!");
nextruntimes = quartzhelper.instance.getnextruntime(cronexpression, starttime, endtime, 5);
isedit = false;
}
catch (exception ex)
{
log.fatal(ex);
info((displayname ?? name) + "启动失败!" + ex.message);
}
finally
{
triggerstate = await quartzhelper.instance.getjobstate(name);
locker.release();
}
}
public virtual async task restart()
{
if (statusenableconverter.iseanblestate(triggerstate, "restart") == false)
{
return;
}
await locker.waitasync();
try
{
if (string.isnullorempty(cronexpression))//如果为空,则设置为立刻执行一次
{
cronexpression = cronhelper.datetime2cron(datetime.now.addseconds(3));
}
info(displayname??name + "开始重新启动!");
triggerstate = await quartzhelper.instance.removejob(name);
triggerstate = await quartzhelper.instance.startjobexecutebycron<t>(cronexpression, name, this, this.starttime, this.endtime);
info((displayname ?? name) + "重新启动完毕!");
nextruntimes = quartzhelper.instance.getnextruntime(cronexpression, starttime, endtime, 5);
isedit = false;
}
catch (exception ex)
{
log.fatal(ex);
info((displayname ?? name) + "重新启动失败!" + ex.message);
}
finally
{
triggerstate = await quartzhelper.instance.getjobstate(name);
locker.release();
}
}
public virtual async task pause()
{
if (statusenableconverter.iseanblestate(triggerstate, "pause") == false)
{
return;
}
try
{
info((displayname ?? name) + "开始暂停!");
triggerstate = await quartzhelper.instance.pausejob(name);
info((displayname ?? name) + "暂停完毕!");
}
catch (exception ex)
{
log.fatal(ex);
info((displayname ?? name) + "暂停失败!" + ex.message);
}
}
public virtual async task resume()
{
if (statusenableconverter.iseanblestate(triggerstate, "resume") == false)
{
return;
}
try
{
info((displayname ?? name) + "开始恢复!");
triggerstate = await quartzhelper.instance.resumejob(name);
info((displayname ?? name) + "恢复完毕!");
}
catch (exception ex)
{
log.fatal(ex);
info((displayname ?? name) + "恢复失败!" + ex.message);
}
}
public virtual async task stop()
{
if (statusenableconverter.iseanblestate(triggerstate, "stop") == false)
{
return;
}
try
{
info((displayname ?? name) + "开始停止!");
triggerstate = await quartzhelper.instance.removejob(name);
info((displayname ?? name) + "停止完毕!");
}
catch (exception ex)
{
log.fatal(ex);
info((displayname ?? name) + "停止失败!" + ex.message);
}
}
public virtual async task runnowonetime()
{
if (statusenableconverter.iseanblestate(triggerstate, "run") == false)
{
return;
}
if (locker.currentcount == 0)
{
info((displayname ?? name) + "正在执行,请稍后再执行!");
return;
}
await locker.waitasync();
try
{
info((displayname ?? name) + "开始执行一次!");
ischedulerfactory sf = new stdschedulerfactory();
ischeduler scheduler = await sf.getscheduler();
await scheduler.start();
ijobdetail job = await scheduler.getjobdetail(new jobkey(name, "group1"));
if (job == null)
{
job = jobbuilder.create<hellojob>()
.withidentity(name, "group1")
.build();
}
itrigger trigger = await scheduler.gettrigger(new triggerkey(name, "group1"));
if (trigger == null)
{
trigger = triggerbuilder.create()
.withidentity(name, "group1")
.startnow()
.withsimpleschedule(x => x
.withintervalinseconds(1))
.build();
}
await scheduler.schedulejob(job, trigger);
info((displayname ?? name) + string.format("执行一次完毕"));
//await task.delay(timespan.fromseconds(5));
//await scheduler.shutdown();
//schedulermetadata metadata = await scheduler.getmetadata();
//info(string.format("执行完毕{0}个任务.", metadata.numberofjobsexecuted));
}
catch (exception ex)
{
log.fatal(ex);
info((displayname ?? name) + string.format("执行一次失败") + ex.message);
}
finally
{
locker.release();
}
}
}
}