Created
September 7, 2023 03:27
-
-
Save dj-nitehawk/02420788fb0a72c4be4752be8bd4c40b to your computer and use it in GitHub Desktop.
Storing `IJobStorageRecord` and `IEventStorageRecord` via EntityFramework Core
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using FastEndpoints; | |
| using MessagePack; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | |
| public class JobRecord : IJobStorageRecord | |
| { | |
| public Guid Id { get; set; } | |
| public string QueueID { get; set; } | |
| public object Command { get; set; } | |
| public DateTime ExecuteAfter { get; set; } | |
| public DateTime ExpireOn { get; set; } | |
| public bool IsComplete { get; set; } | |
| } | |
| public class MyDB : DbContext | |
| { | |
| public DbSet<JobRecord> Jobs { get; set; } | |
| protected override void OnConfiguring(DbContextOptionsBuilder o) | |
| => o.UseSqlite("Data Source=JobDatabase"); | |
| protected override void OnModelCreating(ModelBuilder b) | |
| { | |
| MessagePackSerializer.DefaultOptions | |
| = MessagePack.Resolvers.ContractlessStandardResolver.Options; | |
| var converter = new ValueConverter<object, byte[]>( | |
| v => Serialize(v), | |
| v => Deserialize(v)); | |
| b.Entity<JobRecord>() | |
| .Property(e => e.Command) | |
| .HasConversion(converter) | |
| .HasColumnType("BLOB"); //VARBINARY for sql server | |
| } | |
| static byte[] Serialize(object cmd) => MessagePackSerializer.Serialize(cmd); | |
| static object Deserialize(byte[] cmdBytes) => MessagePackSerializer.Deserialize<object>(cmdBytes); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@JakeCroz1
fyi: https://fast-endpoints.com/docs/job-queues#tracking-job-execution-progress