安排任务可以在数据库上执行更新数据
我正在尝试使用Quartz
Nuget软件包执行任务时间表。调度程序按预期工作,但我的班级问题。当我尝试使用HTTP标头发送SMS时,计划工作正常(我正在使用API SMS Sender服务),但是当我尝试使用数据库执行任何操作时,什么都没有预期。波纹管,我正在分享我的代码。
paymentIncrease.cs
using AmitSMS.Models;
using Microsoft.EntityFrameworkCore;
using Quartz;
namespace AmitSMS.Data.Services;
public class PaymentIncrease : IJob
{
private readonly ApplicationDbContext _context;
public PaymentIncrease(ApplicationDbContext context)
{
_context = context;
}
public Task Execute(IJobExecutionContext context)
{
var task = Task.Run(() => Increase());
return task;
}
public void Increase()
{
var students = _context.Set<Student>().AsNoTracking().Where(x => x.Status == 1 && (x.Batch1 == 1 || x.Batch2 == 1)).ToList();
foreach (var student in students)
{
Due due = new()
{
StudentId = student.StudentId,
Recurring = DateTime.Now.ToString("MMMM, yyyy"),
Amount = 800 * (student.Physics + student.HigherMath),
PreviousAmount = student.Due
};
student.Payment += due.Amount;
student.Due += due.Amount;
_context.Update(student);
_context.Add(due);
}
_context.SaveChanges();
}
}
schedulertask.cs
using Quartz;
using Quartz.Impl;
namespace AmitSMS.Data.Services;
public class SchedulerTask
{
private static readonly string ScheduleSMSExpression = "0 * * ? * *";
public static async Task StartAsync()
{
try
{
var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
if (!scheduler.IsStarted)
{
await scheduler.Start();
}
// Increase Payment
var job1 = JobBuilder.Create<PaymentIncrease>().WithIdentity("ExecuteTaskServiceCallJob1", "group1").Build();
var trigger1 = TriggerBuilder.Create().WithIdentity("ExecuteTaskServiceCallTrigger1", "group1").WithCronSchedule(ScheduleSMSExpression).Build();
//Schedule Payment Job
await scheduler.ScheduleJob(job1, trigger1);
}
catch (Exception ex) {
}
}
}
和on program.cs在配置区域上做到了这一点。
SchedulerTask.StartAsync().GetAwaiter().GetResult();
我正在使用dotnet6。请帮忙。抱歉,所有语法错误。 谢谢。
I'm trying to perform a schedule of tasks with Quartz
NuGet packages. The scheduler works as expected but the problem with my class. The schedule job worked fine while I tried to send SMS With HTTP Header (I'm using an API SMS sender service) but while I tried to perform anything with the database nothing worked as expected. Bellow, I'm sharing my codes.
PaymentIncrease.cs
using AmitSMS.Models;
using Microsoft.EntityFrameworkCore;
using Quartz;
namespace AmitSMS.Data.Services;
public class PaymentIncrease : IJob
{
private readonly ApplicationDbContext _context;
public PaymentIncrease(ApplicationDbContext context)
{
_context = context;
}
public Task Execute(IJobExecutionContext context)
{
var task = Task.Run(() => Increase());
return task;
}
public void Increase()
{
var students = _context.Set<Student>().AsNoTracking().Where(x => x.Status == 1 && (x.Batch1 == 1 || x.Batch2 == 1)).ToList();
foreach (var student in students)
{
Due due = new()
{
StudentId = student.StudentId,
Recurring = DateTime.Now.ToString("MMMM, yyyy"),
Amount = 800 * (student.Physics + student.HigherMath),
PreviousAmount = student.Due
};
student.Payment += due.Amount;
student.Due += due.Amount;
_context.Update(student);
_context.Add(due);
}
_context.SaveChanges();
}
}
SchedulerTask.cs
using Quartz;
using Quartz.Impl;
namespace AmitSMS.Data.Services;
public class SchedulerTask
{
private static readonly string ScheduleSMSExpression = "0 * * ? * *";
public static async Task StartAsync()
{
try
{
var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
if (!scheduler.IsStarted)
{
await scheduler.Start();
}
// Increase Payment
var job1 = JobBuilder.Create<PaymentIncrease>().WithIdentity("ExecuteTaskServiceCallJob1", "group1").Build();
var trigger1 = TriggerBuilder.Create().WithIdentity("ExecuteTaskServiceCallTrigger1", "group1").WithCronSchedule(ScheduleSMSExpression).Build();
//Schedule Payment Job
await scheduler.ScheduleJob(job1, trigger1);
}
catch (Exception ex) {
}
}
}
and on Program.cs did this on the configuration area.
SchedulerTask.StartAsync().GetAwaiter().GetResult();
I'm using dotnet6. Please Help. And sorry for all the grammatical errors.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论