Skip to content

Commit

Permalink
1、将ICacheProvider 改为IServiceProvider注入,避免没有星辰注册导致的Job注入错误
Browse files Browse the repository at this point in the history
2、升级组件
  • Loading branch information
zwllh123 committed Oct 12, 2023
1 parent 7dd1810 commit 83cb4e6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CubeDemo/CubeDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" />
<PackageReference Include="NewLife.Stardust.Extensions" Version="2.9.2023.1001" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion CubeDemoNC/CubeDemoNC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.12" />
<PackageReference Include="NewLife.Redis.Extensions" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Stardust.Extensions" Version="2.9.2023.1001" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Cube/NewLife.Cube.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<PackageReference Include="NewLife.IP" Version="2.1.2023.704-beta0221" />
<PackageReference Include="NewLife.Redis.Extensions" Version="5.5.2023.1001" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1001" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1001" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1012" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion NewLife.CubeNC/NewLife.CubeNC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
<PackageReference Include="NewLife.IP" Version="2.1.2023.704-beta0221" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1001" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1001" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1012" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 7 additions & 6 deletions NewLife.CubeNC/Services/JobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ public class JobService : IHostedService
#region 核心控制

private static readonly IList<MyJob> _jobs = new List<MyJob>();
private readonly ICacheProvider _cacheProvider;
private readonly IServiceProvider _serviceProvider;
private readonly ITracer _tracer;

/// <summary>实例化作业服务</summary>
/// <param name="cacheProvider"></param>
/// <param name="serviceProvider"></param>
/// <param name="tracer"></param>
public JobService(ICacheProvider cacheProvider, ITracer tracer)
public JobService(IServiceProvider serviceProvider, ITracer tracer)
{
_tracer = tracer;
_cacheProvider = cacheProvider;
_serviceProvider = serviceProvider;
}

private static TimerX _timer;
Expand Down Expand Up @@ -79,7 +79,8 @@ private void DoJob(Object state)
var job = _jobs.FirstOrDefault(e => e.Job.Id == item.Id);
if (job == null)
{
job = new MyJob { Job = item, CacheProvider = _cacheProvider, Tracer = _tracer };
//将ICacheProvider 改为IServiceProvider注入,避免没有星辰注册导致的Job注入错误
job = new MyJob { Job = item, CacheProvider = _serviceProvider.GetService<ICacheProvider>(), Tracer = _tracer };
_jobs.Add(job);
}
job.Job = item;
Expand Down Expand Up @@ -268,7 +269,7 @@ private Boolean CheckRunning(CronJob job)
{
// 检查分布式锁,避免多节点重复执行
var key = $"Job:{job.Id}";
if (!CacheProvider.Cache.Add(key, job.Name, 10)) return false;
if (CacheProvider!=null&&!CacheProvider.Cache.Add(key, job.Name, 10)) return false;

// 有时候可能并没有配置Redis,借助数据库事务实现去重,需要20230804版本的XCode
using var tran = CronJob.Meta.CreateTrans();
Expand Down

0 comments on commit 83cb4e6

Please sign in to comment.