如何取消后台作业或让新作业不重试?
How to cancel BackgroundJob or let a newjob don't retry?
我的情况与示例类似,用户按下 'report' 按钮以启动长 运行 报告作业。您将此作业添加到队列中,并在完成后通过电子邮件将报告结果发送给您的用户。但是,我不需要在作业失败时重试作业执行。我想停止工作并发送通知。并且可以取消服务。
public void CancelAllMyJob()
{
var jobs = backgroundJobStore.GetWaitingJobsAsync(int.MaxValue).Result.Where(o => o.JobType.Contains(nameof(MyJob))).ToList();
if (jobs.Count > 0)
{
foreach (BackgroundJobInfo job in jobs)
{
backgroundJobStore.DeleteAsync(job).Wait();
}
}
}
public async Task StartMyJob(MyJobInput input)
{
MyJobArgs args = new MyJobArgs
{
TenantId = AbpSession.TenantId,
UserId = AbpSession.UserId.Value,
};
var id = await backgroundJobManager.EnqueueAsync<MyJob, MyJobArgs >(args);
(await backgroundJobStore.GetAsync(Convert.ToInt64(id))).TryCount = 0;
}
public async Task StartMyJob(MyJobInput input)
{
MyJobArgs args = new MyJobArgs
{
TenantId = AbpSession.TenantId,
UserId = AbpSession.UserId.Value,
};
await Task.Run(() =>
{
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
{
myJob.Execute(args);
}
});
}
成功了,谢谢!
我的情况与示例类似,用户按下 'report' 按钮以启动长 运行 报告作业。您将此作业添加到队列中,并在完成后通过电子邮件将报告结果发送给您的用户。但是,我不需要在作业失败时重试作业执行。我想停止工作并发送通知。并且可以取消服务。
public void CancelAllMyJob()
{
var jobs = backgroundJobStore.GetWaitingJobsAsync(int.MaxValue).Result.Where(o => o.JobType.Contains(nameof(MyJob))).ToList();
if (jobs.Count > 0)
{
foreach (BackgroundJobInfo job in jobs)
{
backgroundJobStore.DeleteAsync(job).Wait();
}
}
}
public async Task StartMyJob(MyJobInput input)
{
MyJobArgs args = new MyJobArgs
{
TenantId = AbpSession.TenantId,
UserId = AbpSession.UserId.Value,
};
var id = await backgroundJobManager.EnqueueAsync<MyJob, MyJobArgs >(args);
(await backgroundJobStore.GetAsync(Convert.ToInt64(id))).TryCount = 0;
}
public async Task StartMyJob(MyJobInput input)
{
MyJobArgs args = new MyJobArgs
{
TenantId = AbpSession.TenantId,
UserId = AbpSession.UserId.Value,
};
await Task.Run(() =>
{
using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
{
myJob.Execute(args);
}
});
}
成功了,谢谢!