Skip to content

Commit

Permalink
https://github.com/zhaopeiym/quartzui/issues/51
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaopeiym committed Apr 28, 2021
1 parent 03f17c1 commit d4cd3f1
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 56 deletions.
3 changes: 2 additions & 1 deletion QuartzNetAPI/Host/Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="MailKit" Version="2.11.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.2" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.2" />
<PackageReference Include="Microsoft.Data.SQLite" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="MQTTnet" Version="3.0.13" />
Expand Down
5 changes: 3 additions & 2 deletions QuartzNetAPI/Host/Managers/SchedulerCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private async Task InitDBTableAsync()
//TODO 其他数据源...
if (driverDelegateType.Equals(typeof(SQLiteDelegate).AssemblyQualifiedName) ||
driverDelegateType.Equals(typeof(MySQLDelegate).AssemblyQualifiedName) ||
driverDelegateType.Equals(typeof(SqlServerDelegate).AssemblyQualifiedName) ||
driverDelegateType.Equals(typeof(PostgreSQLDelegate).AssemblyQualifiedName))
{
IRepositorie repositorie = RepositorieFactory.CreateRepositorie(driverDelegateType, dbProvider);
Expand Down Expand Up @@ -524,11 +525,11 @@ public async Task<List<JobBriefInfoEntity>> GetAllJobBriefInfoAsync()
jobInfo.JobInfoList.Add(new JobBriefInfo()
{
Name = jobKey.Name,
LastErrMsg = jobDetail.JobDataMap.GetString(Constant.EXCEPTION),
LastErrMsg = jobDetail?.JobDataMap.GetString(Constant.EXCEPTION),
TriggerState = await scheduler.GetTriggerState(triggers.Key),
PreviousFireTime = triggers.GetPreviousFireTimeUtc()?.LocalDateTime,
NextFireTime = triggers.GetNextFireTimeUtc()?.LocalDateTime,
RunNumber = jobDetail.JobDataMap.GetLong(Constant.RUNNUMBER)
RunNumber = jobDetail?.JobDataMap.GetLong(Constant.RUNNUMBER) ?? 0
});
continue;
}
Expand Down
4 changes: 4 additions & 0 deletions QuartzNetAPI/Host/Repositories/RepositorieFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static IRepositorie CreateRepositorie(string driverDelegateType, IDbProvi
{
return new RepositorieOracle(dbProvider);
}
else if (driverDelegateType == typeof(SqlServerDelegate).AssemblyQualifiedName)
{
return new RepositorieSqlServer(dbProvider);
}
else
{
return null;
Expand Down
87 changes: 87 additions & 0 deletions QuartzNetAPI/Host/Repositories/RepositorieSqlServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Dapper;
using Microsoft.Data.SqlClient;
using Newtonsoft.Json.Linq;
using Quartz.Impl.AdoJobStore.Common;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Host.Repositories
{
public class RepositorieSqlServer : IRepositorie
{
private IDbProvider DBProvider { get; }
public RepositorieSqlServer(IDbProvider dbProvider)
{
DBProvider = dbProvider;
}

public async Task<int> InitTable()
{
using (var connection = new SqlConnection(DBProvider.ConnectionString))
{
var check_sql = @"SELECT
COUNT (1)
FROM
sys.tables
WHERE
name IN (
'QRTZ_BLOB_TRIGGERS',
'QRTZ_CALENDARS',
'QRTZ_CRON_TRIGGERS',
'QRTZ_FIRED_TRIGGERS',
'QRTZ_JOB_DETAILS',
'QRTZ_LOCKS',
'QRTZ_PAUSED_TRIGGER_GRPS',
'QRTZ_SCHEDULER_STATE',
'QRTZ_SIMPLE_TRIGGERS',
'QRTZ_SIMPROP_TRIGGERS',
'QRTZ_TRIGGERS'
);";
var count = await connection.QueryFirstOrDefaultAsync<int>(check_sql);
//初始化 建表
if (count == 0)
{
string init_sql = await File.ReadAllTextAsync("Tables/tables_sqlServer.sql");
return await connection.ExecuteAsync(init_sql);
}
}
return 0;
}

public async Task<bool> RemoveErrLogAsync(string jobGroup, string jobName)
{
try
{
using (var connection = new SqlConnection(DBProvider.ConnectionString))
{
string sql = $@"SELECT
JOB_DATA
FROM
QRTZ_JOB_DETAILS
WHERE
JOB_NAME = @jobName
AND JOB_GROUP = @jobGroup";

var byteArray = await connection.ExecuteScalarAsync<byte[]>(sql, new { jobName, jobGroup });
var jsonStr = Encoding.Default.GetString(byteArray);
JObject source = JObject.Parse(jsonStr);
source.Remove("Exception");//移除异常日志
var modifySql = $@"UPDATE QRTZ_JOB_DETAILS
SET JOB_DATA = @jobData
WHERE
JOB_NAME = @jobName
AND JOB_GROUP = @jobGroup";
await connection.ExecuteAsync(modifySql, new { jobName, jobGroup, jobData = Encoding.Default.GetBytes(source.ToString()) });
}

return true;
}
catch (Exception ex)
{
return false;
}
}
}
}

0 comments on commit d4cd3f1

Please sign in to comment.