Skip to content

Commit

Permalink
[fix] 修正JobService中获取job对象时可能报空引用的异常。该问题不影响任务执行,仅影响执行后的日志写入
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jan 20, 2024
1 parent ad60f97 commit 413abd8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions NewLife.CubeNC/Services/JobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ private void DoJob(Object state)
if (job == null)
{
//将ICacheProvider 改为IServiceProvider注入,避免没有星辰注册导致的Job注入错误
job = new MyJob { Job = item, CacheProvider = _serviceProvider.GetService<ICacheProvider>(), Tracer = _tracer };
job = new MyJob
{
Job = item,
CacheProvider = _serviceProvider.GetService<ICacheProvider>(),
Tracer = _tracer
};
_jobs.Add(job);
}
job.Job = item;
Expand Down Expand Up @@ -111,7 +116,7 @@ private void DoJob(Object state)
#region 辅助
internal static void WriteLog(String action, Boolean success, String remark, CronJob job)
{
if (!job.EnableLog) return;
if (job != null && !job.EnableLog) return;

var log = LogProvider.Provider.CreateLog("JobService", action, success, remark);
if (job != null) log.LinkID = job.Id;
Expand Down Expand Up @@ -178,7 +183,7 @@ public static void BackupDb(String connNames)

sw.Stop();

var job = TimerX.Current?.State as CronJob;
var job = (TimerX.Current?.State as MyJob)?.Job;
WriteLog(nameof(BackupDb), true, $"备份数据库 {name}{bak},耗时 {sw.Elapsed}", job);
}
}
Expand All @@ -205,9 +210,7 @@ internal class MyJob : IDisposable
public void Start()
{
var job = Job;
//using var span = Tracer?.NewSpan($"job:{job}:Start");
//try
//{

// 参数检查
var expession = job.Cron;
if (expession.IsNullOrEmpty()) throw new ArgumentNullException(nameof(job.Cron));
Expand Down Expand Up @@ -237,19 +240,12 @@ public void Start()

// 实例化定时器,原定时器销毁
_timer.TryDispose();
_timer = new TimerX(DoJobWork, job, expession) { Async = true, Tracer = Tracer };
_timer = new TimerX(DoJobWork, this, expession) { Async = true, Tracer = Tracer };

job.NextTime = _timer.NextTime;
job.Update();

_id = id;
//}
//catch (Exception ex)
//{
// span?.SetError(ex, null);

// throw;
//}
}

public void Stop()
Expand Down

0 comments on commit 413abd8

Please sign in to comment.