安排任务可以在数据库上执行更新数据

发布于 2025-02-05 05:15:09 字数 2510 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文