开源框架YiShaAdmin如何使用任务计划
1.在Startup添加 new JobCenter().Start();(红色字体,下同)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (!string.IsNullOrEmpty(GlobalContext.SystemConfig.VirtualDirectory))
{
app.UsePathBase(new PathString(GlobalContext.SystemConfig.VirtualDirectory)); // 让 Pathbase 中间件成为第一个处理请求的中间件, 才能正确的模拟虚拟路径
}
if (WebHostEnvironment.IsDevelopment())
{
GlobalContext.SystemConfig.Debug = true;
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} string resource = Path.Combine(env.ContentRootPath, "Resource");
FileHelper.CreateDirectory(resource); app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = GlobalContext.SetCacheControl
});
app.UseStaticFiles(new StaticFileOptions
{
RequestPath = "/Resource",
FileProvider = new PhysicalFileProvider(resource),
OnPrepareResponse = GlobalContext.SetCacheControl
});
app.UseSession();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("areas", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
GlobalContext.ServiceProvider = app.ApplicationServices;
new JobCenter().Start(); }
2.在YiSha.Business.AutoJob的Job文件夹新建TestConsoleJob.cs
using System.Threading.Tasks;
using YiSha.Util.Model; namespace YiSha.Business.AutoJob
{
internal class TestConsoleJob : IJobTask
{
public async Task<TData> Start()
{
TData obj = new TData();
obj.Tag = 1;
obj.Message = "测试调用成功";
return obj;
}
}
}
3.在JobExecute中加入新添加的任务
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl.Triggers;
using YiSha.Entity.SystemManage;
using YiSha.Enum;
using YiSha.Service.SystemManage;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model; namespace YiSha.Business.AutoJob
{
public class JobExecute : IJob
{
private AutoJobService autoJobService = new AutoJobService();
private AutoJobLogService autoJobLogService = new AutoJobLogService(); public Task Execute(IJobExecutionContext context)
{
return Task.Run(async () =>
{
TData obj = new TData();
long jobId = 0;
JobDataMap jobData = null;
AutoJobEntity dbJobEntity = null;
try
{
jobData = context.JobDetail.JobDataMap;
jobId = jobData["Id"].ParseToLong();
// 获取数据库中的任务
dbJobEntity = await autoJobService.GetEntity(jobId);
if (dbJobEntity != null)
{
if (dbJobEntity.JobStatus == StatusEnum.Yes.ParseToInt())
{
CronTriggerImpl trigger = context.Trigger as CronTriggerImpl;
if (trigger != null)
{
if (trigger.CronExpressionString != dbJobEntity.CronExpression)
{
// 更新任务周期
trigger.CronExpressionString = dbJobEntity.CronExpression;
await JobScheduler.GetScheduler().RescheduleJob(trigger.Key, trigger);
} #region 执行任务
switch (context.JobDetail.Key.Name)
{
case "数据库备份":
obj = await new DatabasesBackupJob().Start();
break;
case "测试任务计划":
obj = await new TestConsoleJob().Start();
break;
}
#endregion
}
}
}
}
catch (Exception ex)
{
obj.Message = ex.GetOriginalException().Message;
LogHelper.Error(ex);
} try
{
if (dbJobEntity != null)
{
if (dbJobEntity.JobStatus == StatusEnum.Yes.ParseToInt())
{
#region 更新下次运行时间
await autoJobService.SaveForm(new AutoJobEntity
{
Id = dbJobEntity.Id,
NextStartTime = context.NextFireTimeUtc.Value.DateTime.AddHours(8)
});
#endregion #region 记录执行状态
await autoJobLogService.SaveForm(new AutoJobLogEntity
{
JobGroupName = context.JobDetail.Key.Group,
JobName = context.JobDetail.Key.Name,
LogStatus = obj.Tag,
Remark = obj.Message
});
#endregion
}
}
}
catch (Exception ex)
{
obj.Message = ex.GetOriginalException().Message;
LogHelper.Error(ex);
}
});
}
}
}
4.添加任务计划,测试


开源框架YiShaAdmin如何使用任务计划的更多相关文章
- Pyhton开源框架(加强版)
info:Djangourl:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 ...
- Python开源框架
info:更多Django信息url:https://www.oschina.net/p/djangodetail: Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC) ...
- Scut游戏服务器免费开源框架-3
Scut游戏服务器免费开源框架--快速开发(3) Scut快速开发(3) 1 开发环境 需要安装的软件 a) 消息队列 b) 数据库,Sql2005以上版本 ...
- 值得学习的C/C++开源框架(转)
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
- .Net开源框架列表
API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务 ...
- 值得推荐的C/C++开源框架和库
值得推荐的C/C++开源框架和库 转自:http://www.cnblogs.com/lidabo/p/5514155.html - 1. Webbench Webbench是一个在Linux下 ...
- C++的一些不错开源框架,可以学习和借鉴
from https://www.cnblogs.com/charlesblc/p/5703557.html [本文系外部转贴,原文地址:http://coolshell.info/c/c++/201 ...
- 关于Farseer.net轻量级ORM开源框架 V1.0 概念版本开发的消息
V0.2版的开源距离今天(05年03月)已有近3年的时间.可以说这个版本已经有点落伍的感觉了,呵呵. V0.2版至今一直处于BUG的修复及一些细小功能的增加,所以版本号上一直没有变化. 其实在这1.2 ...
- Android开源框架ViewPageIndicator和ViewPager实现Tab导航
前言: 关于使用ViewPageIndicator和ViewPager实现Tab导航,在开发社区里已经有一堆的博客对其进行了介绍,假设我还在这里写怎样去实现.那简直就是老生常谈,毫无新奇感,并且.我也 ...
随机推荐
- github新手使用指南
常用命令: Git 速查表(摘自 AI有道) 一.常见命令 git init : 初始化 git 仓库,即将一个文件夹初始化为一个 git 仓库.具体的操作是创建一个 .git 隐藏文件夹 git ...
- 建立META-INF/spring.factories文件的意义何在
平常我们如何将Bean注入到容器当中 @Configuration @EnableConfigurationProperties(HelloProperties.class) public class ...
- cornerstone 忽略不必要文件
转:https://www.jianshu.com/p/f48207baa0cd
- 说说三元运算和if...else的相同之处
三元运算符和if-else语句:不同之处. a) 三元运算符是必须要有返回值,而if-else语句并不一定有返回值,其执行结果可能是赋值语句或者打印输出语句. b) java三元表达式有字符强转(双目 ...
- AOP——基于AspectJ的注解来实现AOP操作
1.使用注解方式实现AOP操作 第一步:创建对象 <!-- 创建对象 --> <bean id="book" class="com.bjxb.aop.B ...
- django-debug-toolbar 开发利器的使用教程
django-debug-toolbar介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. 下载安装 ...
- redis持久存储RDB和AOF的区别及优缺点
1.前言 最近在项目中使用到Redis做缓存,方便多个业务进程之间共享数据.由于Redis的数据都存放在内存中,如果没有配置持久化,redis重启后数据就全丢失了,于是需要开启redis的持久化功能, ...
- resin服务之二----整合resin+Apache服务
整合resin+Apache服务 1.为什么要整合Apache和resin? a. 早期的resin,tomcat对httpd服务支持不好. b. tomcat,resin对rewrite,expi ...
- SpringCloud个人笔记-02-Feign初体验
项目结构 sb_cloud_product <?xml version="1.0" encoding="UTF-8"?> <project x ...
- 【代码开源】GreaterWMS 抖音SDK调用教程
应用介绍 GreaterWMS 抖音SDK调用教程 SDK具体功能: 1,一仓多店,多仓多店 2,库存同步,商品同步 3,快递发货,物流轨迹 4,订单拦截 5,字节云仓 6,精选联盟 7,供应分销 8 ...