2025-04-24 18:31:27 +08:00

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="{&quot;MerchantId&quot;: &quot;00000000-0000-0000-0000-000000000009&quot;,&quot;Issuer&quot;: &quot;lxzyl&quot;,&quot;Secret&quot;: &quot;4ea0180d8f8c1ce11343e9104c682bc4&quot;,&quot;SendVerifyCode&quot;:&quot;http://120.78.93.13:30000/InterfaceAPI/sendVerifyCode&quot;, &quot;SendSms&quot;: &quot;http://120.78.93.13:30000/InterfaceAPI/sendSmsMessage&quot;, &quot;ApiTokenUrl&quot;: &quot;http://localhost:8888/oauth2/token&quot;,
&quot;IsDev&quot;: true, &quot;QiNiuCloudApiUrl&quot;: &quot;http://oss-api.loonxierp.com&quot;, &quot;CmpPayCallbackDomain&quot;: &quot;http://bfy-api.wxbinf.com:8095&quot;}" />
<add key="WechatConfig" value="{&quot;ApiAuthDomain&quot;: &quot;http://binfapi-test.wxbinf.com&quot;,&quot;Scope&quot;: &quot;snsapi_base&quot;,&quot;WechatPublic&quot;: &quot;流行美专业测试&quot;,&quot;appHtml&quot;: &quot;https://dx.wxbinf.com/h5/index.html?&quot;} " />
<add key="SmsTemplateConfig" value="{&quot;ConsumptionSuccess&quot;:&quot;亲爱的#Member#会员,您本次消费共#Amount#元,获得#Integral#积分;请关注【#WechatPublic#】公众号查看详情。&quot;, &quot;ServiceFinish&quot;:&quot; #ServiceString#,点击 #AppHtml##RemainingService#&quot;,
&quot;RechangeSuccess&quot;: &quot;亲爱的会员,您已成功充值#RechargeAmount#元;储值余额#Balance#元;请关注【#WechatPublic#】公众号,登录后查看储值明细。&quot;, &quot;PreOrderSuccess&quot;: &quot;亲爱的会员,您在#Month#月#Day#日#Shop#的服务项目已经预约成功,关注【#WechatPublic#】公众号,查看预约详情。&quot;
}" />
*
* */
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; }
}
}
}