Skip to content

Commit 4465b10

Browse files
authored
Add WorkerClass to PersistentJobState (#896)
Seems useful to store.
2 parents 7d8b363 + 1b7d6d8 commit 4465b10

7 files changed

Lines changed: 49 additions & 7 deletions

File tree

Refresh.Database/GameDatabaseContext.Workers.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ public bool MarkWorkerContacted(int id)
4141
return true;
4242
}
4343

44-
public object? GetJobState(string jobId, Type type)
44+
public object? GetJobState(string jobId, Type type, WorkerClass workerClass)
4545
{
46-
PersistentJobState? state = this.JobStates.FirstOrDefault(s => s.JobId == jobId);
46+
PersistentJobState? state = this.JobStates.FirstOrDefault(s => s.JobId == jobId && s.Class == workerClass);
4747
if (state == null)
4848
return null;
4949

5050
return JsonConvert.DeserializeObject(state.State, type);
5151
}
5252

53-
public void UpdateOrCreateJobState(string jobId, object state)
53+
public void UpdateOrCreateJobState(string jobId, object state, WorkerClass workerClass)
5454
{
55-
PersistentJobState? jobState = this.JobStates.FirstOrDefault(s => s.JobId == jobId);
55+
PersistentJobState? jobState = this.JobStates.FirstOrDefault(s => s.JobId == jobId && s.Class == workerClass);
5656
if (jobState == null)
5757
{
5858
jobState = new PersistentJobState
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore.Infrastructure;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
namespace Refresh.Database.Migrations
7+
{
8+
[DbContext(typeof(GameDatabaseContext))]
9+
[Migration("20250726043747_AddClassToJobState")]
10+
/// <inheritdoc />
11+
public partial class AddClassToJobState : Migration
12+
{
13+
/// <inheritdoc />
14+
protected override void Up(MigrationBuilder migrationBuilder)
15+
{
16+
migrationBuilder.AddColumn<int>(
17+
name: "Class",
18+
table: "JobStates",
19+
type: "integer",
20+
nullable: false,
21+
defaultValue: 0);
22+
}
23+
24+
/// <inheritdoc />
25+
protected override void Down(MigrationBuilder migrationBuilder)
26+
{
27+
migrationBuilder.DropColumn(
28+
name: "Class",
29+
table: "JobStates");
30+
}
31+
}
32+
}

Refresh.Database/Migrations/GameDatabaseContextModelSnapshot.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
15941594
b.Property<string>("JobId")
15951595
.HasColumnType("text");
15961596

1597+
b.Property<int>("Class")
1598+
.HasColumnType("integer");
1599+
15971600
b.Property<string>("State")
15981601
.IsRequired()
15991602
.HasColumnType("jsonb");

Refresh.Database/Models/Workers/PersistentJobState.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class PersistentJobState
44
{
55
[Key, Required] public string JobId { get; set; } = null!;
6+
public WorkerClass Class { get; set; } = WorkerClass.Refresh;
67
[Column(TypeName = "jsonb"), Required] public string State { get; set; } = null!;
78
}

Refresh.Workers/MigrationJob.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore.Storage;
22
using Refresh.Core;
3+
using Refresh.Database.Models.Workers;
34
using Refresh.Workers.State;
45

56
namespace Refresh.Workers;
@@ -9,6 +10,7 @@ public abstract class MigrationJob<TEntity> : WorkerJob, IJobStoresState where T
910
public virtual string JobId => this.GetType().Name;
1011
public object JobState { get; set; } = null!;
1112
public Type JobStateType => typeof(MigrationJobState);
13+
public WorkerClass JobClass => WorkerClass.Refresh;
1214

1315
public MigrationJobState? MigrationJobState => JobState as MigrationJobState;
1416

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace Refresh.Workers.State;
1+
using Refresh.Database.Models.Workers;
2+
3+
namespace Refresh.Workers.State;
24

35
public interface IJobStoresState
46
{
57
public string JobId { get; }
68
public object JobState { get; set; }
79
public Type JobStateType { get; }
10+
public WorkerClass JobClass { get; }
811
}

Refresh.Workers/WorkerManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using NotEnoughLogs;
33
using Refresh.Core;
44
using Refresh.Database;
5+
using Refresh.Database.Models.Workers;
56
using Refresh.Workers.State;
67

78
namespace Refresh.Workers;
@@ -58,7 +59,7 @@ private void RunWorkCycle()
5859
IJobStoresState? jobWithState = job as IJobStoresState;
5960
if (jobWithState != null)
6061
{
61-
object? jobState = context.Database.GetJobState(jobWithState.JobId, jobWithState.JobStateType);
62+
object? jobState = context.Database.GetJobState(jobWithState.JobId, jobWithState.JobStateType, jobWithState.JobClass);
6263
jobState ??= Activator.CreateInstance(jobWithState.JobStateType);
6364

6465
jobWithState.JobState = jobState!;
@@ -82,7 +83,7 @@ private void RunWorkCycle()
8283
}
8384

8485
if (jobWithState != null)
85-
context.Database.UpdateOrCreateJobState(jobWithState.JobId, jobWithState.JobState);
86+
context.Database.UpdateOrCreateJobState(jobWithState.JobId, jobWithState.JobState, jobWithState.JobClass);
8687
}
8788

8889
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

0 commit comments

Comments
 (0)