@@ -22,23 +22,31 @@ Inherit from `DurableGrain` and inject one or more state machines using the `[Fr
2222
2323``` csharp
2424public 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