138 lines
4.9 KiB
C#
138 lines
4.9 KiB
C#
|
|
using Newtonsoft.Json;
|
|
using Quartz;
|
|
using Quartz.Impl;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Xml;
|
|
using Microsoft.Practices.Unity;
|
|
using MyCode.Project.Infrastructure.UnityExtensions;
|
|
using Quartz.Impl.Triggers;
|
|
using MyCode.Project.ScheduleTask.UnityQuartz;
|
|
using MyCode.Project.Repositories;
|
|
using MyCode.Project.Infrastructure;
|
|
using MyCode.Project.Infrastructure.Common;
|
|
using MyCode.Project.Repositories.Common;
|
|
using MyCode.Project.Infrastructure.Cache;
|
|
using MyCode.Project.Domain.Config;
|
|
|
|
namespace MyCode.Project.ScheduleTask
|
|
{
|
|
/*
|
|
*
|
|
*
|
|
<add key="SysConfig" value="{"MerchantId": "00000000-0000-0000-0000-000000000009","Issuer": "lxzyl","Secret": "4ea0180d8f8c1ce11343e9104c682bc4","SendVerifyCode":"http://120.78.93.13:30000/InterfaceAPI/sendVerifyCode", "SendSms": "http://120.78.93.13:30000/InterfaceAPI/sendSmsMessage", "ApiTokenUrl": "http://localhost:8888/oauth2/token",
|
|
"IsDev": true, "QiNiuCloudApiUrl": "http://oss-api.loonxierp.com", "CmpPayCallbackDomain": "http://bfy-api.wxbinf.com:8095"}" />
|
|
<add key="WechatConfig" value="{"ApiAuthDomain": "http://binfapi-test.wxbinf.com","Scope": "snsapi_base","WechatPublic": "流行美专业测试","appHtml": "https://dx.wxbinf.com/h5/index.html?"} " />
|
|
<add key="SmsTemplateConfig" value="{"ConsumptionSuccess":"亲爱的#Member#会员,您本次消费共#Amount#元,获得#Integral#积分;请关注【#WechatPublic#】公众号查看详情。", "ServiceFinish":" #ServiceString#,点击 #AppHtml##RemainingService#",
|
|
"RechangeSuccess": "亲爱的会员,您已成功充值#RechargeAmount#元;储值余额#Balance#元;请关注【#WechatPublic#】公众号,登录后查看储值明细。", "PreOrderSuccess": "亲爱的会员,您在#Month#月#Day#日#Shop#的服务项目已经预约成功,关注【#WechatPublic#】公众号,查看预约详情。"
|
|
}" />
|
|
*
|
|
* */
|
|
public static class JobsHelp
|
|
{
|
|
private static string ConfigFile = "";
|
|
private static IScheduler sched = null;
|
|
|
|
public static void start(string _ConfigFile)
|
|
{
|
|
IUnityContainer container = UnityHelper.GetUnityContainer();
|
|
|
|
container.RegisterType<MyCodeSqlSugarClient>(new PerThreadLifetimeManager());
|
|
|
|
////注册缓存对象
|
|
//container.RegisterType<IMyCodeCacheService, RedisCache>(new InjectionConstructor(SystemConfig.RedisAddress, SystemConfig.CachePrefix));
|
|
container.RegisterType<IMyCodeCacheService, SystemCache>();
|
|
container.AddNewExtension<QuartzUnityExtension>();
|
|
|
|
ConfigFile = _ConfigFile;
|
|
List<jobinfo> list = new List<jobinfo>();
|
|
try
|
|
{
|
|
if (sched != null)
|
|
{
|
|
stop();
|
|
sched = null;
|
|
}
|
|
//sched = new StdSchedulerFactory().GetScheduler();
|
|
sched = container.Resolve<IScheduler>();
|
|
XmlDocument document = new XmlDocument();
|
|
document.Load(ConfigFile);
|
|
|
|
XmlNode node = document.SelectSingleNode("Jobs");
|
|
if (node.ChildNodes.Count > 0)
|
|
{
|
|
foreach (XmlNode node2 in node.ChildNodes)
|
|
{
|
|
jobinfo item = new jobinfo
|
|
{
|
|
name = node2.Attributes["name"].Value,
|
|
type = node2.Attributes["type"].Value,
|
|
CronExpression = node2.Attributes["CronExpression"].Value,
|
|
enabled = bool.Parse(node2.Attributes["enabled"].Value)
|
|
};
|
|
if (item.enabled)
|
|
{
|
|
list.Add(item);
|
|
IJobDetail jobDetail = JobBuilder.Create(Type.GetType(item.type)).WithIdentity(item.name, item.name + "Group").Build();
|
|
ITrigger trigger = TriggerBuilder.Create().WithIdentity(item.name, item.name + "Group").WithCronSchedule(item.CronExpression).Build();
|
|
|
|
sched.ScheduleJob(jobDetail, trigger);
|
|
}
|
|
}
|
|
if (list.Count > 0)
|
|
{
|
|
sched.Start();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("暂未有计划任务开启1");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("暂未有计划任务开启");
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Console.WriteLine(JsonConvert.SerializeObject(list));
|
|
Console.WriteLine(exception);
|
|
LogHelper.Error("出错", exception);
|
|
|
|
}
|
|
|
|
Console.ReadKey();
|
|
}
|
|
|
|
public static void stop()
|
|
{
|
|
try
|
|
{
|
|
if (sched != null)
|
|
{
|
|
sched.Shutdown(false);
|
|
sched.Clear();
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Console.WriteLine("关闭计划任务失败:" + exception.Message);
|
|
}
|
|
}
|
|
|
|
private class jobinfo
|
|
{
|
|
public string CronExpression { get; set; }
|
|
|
|
public bool enabled { get; set; }
|
|
|
|
public string name { get; set; }
|
|
|
|
public string type { get; set; }
|
|
}
|
|
}
|
|
}
|
|
|