AspNetCore.JobQueue 2.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package AspNetCore.JobQueue --version 2.0.2
                    
NuGet\Install-Package AspNetCore.JobQueue -Version 2.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AspNetCore.JobQueue" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AspNetCore.JobQueue" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="AspNetCore.JobQueue" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AspNetCore.JobQueue --version 2.0.2
                    
#r "nuget: AspNetCore.JobQueue, 2.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AspNetCore.JobQueue@2.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AspNetCore.JobQueue&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=AspNetCore.JobQueue&version=2.0.2
                    
Install as a Cake Tool

Install Package

dotnet add package AspNetCore.JobQueue

Create Job

创建你的Job,继承于IJob

public class OrderJob : IJob
{
    public string OrderId { get; set; }
    public string OrderName { get; set; }
}

Create JobHandler

创建你的job handler,用来执行job,在ExecuteAsync方法里面写执行的代码

public class OrderJobHandler : IJobHandler<OrderJob>
{
    public async Task ExecuteAsync(OrderJob job, CancellationToken ct)
    {
        await Task.Delay(2000);
        Console.WriteLine("exec job: {0} {1}", job.OrderId, job.OrderName);
    }
}

Create JobRecord

JobRecord用来做队列的持久化,根据项目的特点自行调整。 此处用entity framework code first做示例

[Table("job_record", Schema = "myjob")]
public class JobRecord : IJobStorageRecord
{
    [Key]
    [Column("id")]
    public Guid ID { get; set; }
    [Column("queue_id")]
    public string QueueID { get; set; }
    [NotMapped]
    public object JobData { get; set; }
    [Column("j_command")]
    public JsonElement JCommand { get; set; }
    [Column("execute_after")]
    public DateTime ExecuteAfter { get; set; }
    [Column("expire_on")]
    public DateTime ExpireOn { get; set; }
    [Column("execute_at")]
    public DateTime ExecuteAt { get; set; }
    [Column("is_complete")]
    public bool IsComplete { get; set; }
}

Create JobRecordStorage

创建你的JobRecordStorage,用来实现JobRecord的持久化操作

public class JobRecordStorage : IJobStorageProvider<JobRecord>
{
    private readonly IDbContextFactory<JobDbContext> _dbContextFactory;

    public JobRecordStorage(IDbContextFactory<JobDbContext> dbContextFactory)
    {
        _dbContextFactory = dbContextFactory;
    }

    public async Task StoreJobAsync(JobRecord r, CancellationToken ct)
    {
        using var dbContext = await _dbContextFactory.CreateDbContextAsync(ct);

        r.JCommand = JsonSerializer.SerializeToElement(r.JobData);
        await dbContext.JobRecords.AddAsync(r, ct);
        await dbContext.SaveChangesAsync(ct);
    }
    public async Task<IEnumerable<JobRecord>> GetNextBatchAsync(PendingJobSearchParams<JobRecord> parameters)
    {
        using var dbContext = await _dbContextFactory.CreateDbContextAsync(parameters.CancellationToken);

        var result = await dbContext.JobRecords.AsNoTracking()
                        .Where(parameters.Match)
                        .Take(parameters.Limit)
                        .ToListAsync(parameters.CancellationToken);
        result.ForEach(p => p.JobData = JsonSerializer.Deserialize(p.JCommand, parameters.TypeOfJob));
        return result;
    }

    public async Task MarkJobAsCompleteAsync(JobRecord r, CancellationToken ct)
    {
        using var dbContext = await _dbContextFactory.CreateDbContextAsync(ct);

        await dbContext.JobRecords.Where(p => p.ID == r.ID)
            .ExecuteUpdateAsync(p => p.SetProperty(x => x.IsComplete, true), ct);
    }

    public async Task OnHandlerExecutionFailureAsync(JobRecord r, Exception exception, CancellationToken ct)
    {
        using var dbContext = await _dbContextFactory.CreateDbContextAsync(ct);

        await dbContext.JobRecords.Where(p => p.ID == r.ID)
            .ExecuteUpdateAsync(p => p.SetProperty(x => x.ExecuteAfter, DateTime.Now.AddMinutes(1)), ct);
    }

    public async Task PurgeStaleJobsAsync(StaleJobSearchParams<JobRecord> parameters)
    {
        using var dbContext = await _dbContextFactory.CreateDbContextAsync(parameters.CancellationToken);

        await dbContext.JobRecords.Where(parameters.Match)
            .ExecuteDeleteAsync(parameters.CancellationToken);
    }
}

Inject JobQueue

builder.Services.AddJobQueues<JobRecord, JobRecordStorage>();
...
app.UseJobQueues();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.4 234 9/13/2023
2.0.3 197 9/13/2023
2.0.2 174 9/12/2023
2.0.1 203 9/6/2023
2.0.0 215 9/6/2023
1.0.2 204 9/5/2023
1.0.1 211 9/5/2023
1.0.0 209 9/5/2023