彼此独立地执行作业

时间:2020-07-22 10:04:51

标签: c# .net-core

我有以下代码:

public async Task ExecuteJobs(CancellationToken ct)
{
    var newJobs = (await _jobRepository.GetAsync(ct, x => x.State == State.Created && x.RunDate <= DateTimeOffset.Now)).ToList();
    
    foreach (var job in newJobs)
    {
        try
        {
            UpdateJobState(job, State.Scheduled);
            switch (job.Type)
            {
                case JobType.AnnualReportConfiguration:
                    UpdateJobState(job, State.Running);
                    await _reportConfigurationDataService.GetAndSendData(ct);
                    break;
                case JobType.Assets:
                    UpdateJobState(job, State.Running);
                    await _assetsDataService.GetAndSendData(ct);
                    break;
                case JobType.BrfEconomy:
                    UpdateJobState(job, State.Running);
                    await _brfEconomyDataService.GetAndSendData(ct);
                    break;
                case JobType.Customer:
                    UpdateJobState(job, State.Running);
                    await _customerDataService.GetAndSendData(ct);
                    break;
                case JobType.ElectedRepresentative:
                    UpdateJobState(job, State.Running);
                    await _representativeDataService.GetAndSendData(ct);
                    break;
                case JobType.Person:
                    UpdateJobState(job, State.Running);
                    await _personDataService.GetAndSendData(ct);
                    break;
                case JobType.RealEstate:
                    UpdateJobState(job, State.Running);
                    await _realEstateDataService.GetAndSendData(ct);
                    break;
                case JobType.TextTemplate:
                    UpdateJobState(job, State.Running);
                    await _textTemplateDataService.GetAndSendData(ct);
                    break;
            }

            UpdateJobState(job, State.Finished);
        }
        catch(Exception ex)
        {
            UpdateJobState(job, State.Error);
        }
    }
}

    private void UpdateJobState(Job job, State jobState)
    {
        job.State = jobState;
        _jobRepository.Update(job);
    }

我想知道如何才能彼此独立运行工作,而不是彼此等待吗? 考虑到实体框架不是线程安全的,我该如何以一种好的方式做到这一点?

我尝试过这样的事情:

case JobType.Assets:
 UpdateJobState(job, State.Running);
 var task = Task.Run(() => _assetsDataService.GetAndSendData(ct));
 task.ContinueWith((task) =>
 {
    UpdateJob(job.Id, State.Finished, ct);
 });
 break;

    private async Task UpdateJob(Guid id, State jobState, CancellationToken ct)
    {
        var job = (await _jobRepository.GetAsync(ct, x => x.Id == id)).FirstOrDefault();
        job.State = jobState;
        _jobRepository.Update(job);
    }

但是那是行不通的,因为我得到一个异常,说DbContext在另一个线程中使用。

你们能给我一些有关我应该如何做的反馈吗?

编辑:

我通过实施工厂解决了实体框架问题,并且奏效了。

但是,

我的任务完成时出现以下错误:

Cannot access a disposed object.\r\nObject name: 'IServiceProvider'.

这是执行此代码的时间:

estateTask.ContinueWith((estateTask) => UpdateJob(job.Id, State.Finished, ct));

    private async Task UpdateJob(Guid Id, State jobState, CancellationToken ct)
    {
        try
        {
            var job = (await _jobRepository.Get(x => x.Id == Id, ct)).FirstOrDefault(); // error here
            job.State = jobState;
            _jobRepository.Update(job);
        }
        catch(Exception ex)
        {
            throw;
        }
    }

我该如何解决?

0 个答案:

没有答案