Skip to content

Commit ae2a85a

Browse files
Update README.md
1 parent 9e4e78a commit ae2a85a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,31 @@ Inherit from `DurableGrain` and inject one or more state machines using the `[Fr
2222

2323
```csharp
2424
public class JobSchedulerGrain(
25-
[FromKeyedServices("job-queue")]
26-
IDurablePriorityQueue<string, int> jobs)
27-
: DurableGrain, IJobSchedulerGrain
25+
[FromKeyedServices("job-queue")] IDurablePriorityQueue<string, int> jobs)
26+
: DurableGrain, IJobSchedulerGrain
2827
{
2928
public async Task AddJob(string jobName, int priority)
3029
{
31-
// 1. Mutate the in-memory state.
30+
// Add to the in-memory state.
3231
jobs.Enqueue(jobName, priority);
3332

34-
// 2. Persist the change to the journal.
33+
// Persist the enqueue operation to the journal.
3534
await WriteStateAsync();
3635
}
3736

38-
public Task<string> GetNextJob()
37+
public async Task<string?> GetNextJob()
3938
{
40-
// Reads are always from the in-memory state.
41-
return Task.FromResult(jobs.Peek());
39+
string? jobName;
40+
41+
// Look inside the in-memory state.
42+
if (jobs.TryDequeue(out jobName, out int priority))
43+
{
44+
// There is a job to work on!
45+
// Persist the dequeue operation to the journal.
46+
await WriteStateAsync();
47+
}
48+
49+
return jobName;
4250
}
4351
}
4452
```

0 commit comments

Comments
 (0)