Skip to content

Commit 7351b4d

Browse files
committed
Pass MessageWatchdog to MessageWriter and MessageWriters instead of an Action
1 parent d44e14e commit 7351b4d

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

Core/Cleipnir.ResilientFunctions/CoreRuntime/Invocation/InvocationHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ await _functionStore.BulkScheduleFunctions(
389389
}
390390

391391
public MessageWriter CreateMessageWriter(StoredId storedId)
392-
=> new MessageWriter(storedId, _functionStore.MessageStore, Serializer, _replicaId, _messageWatchdog.Notify);
392+
=> new MessageWriter(storedId, _functionStore.MessageStore, Serializer, _replicaId, _messageWatchdog);
393393

394394
public Effect CreateEffect(StoredId storedId, FlowId flowId, IReadOnlyList<StoredEffect> storedEffects, FlowTimeouts flowTimeouts, IStorageSession? storageSession, FlowExecutionState flowExecutionState)
395395
{

Core/Cleipnir.ResilientFunctions/FunctionsRegistry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public FuncRegistration<TParam, TReturn> RegisterFunc<TParam, TReturn>(
284284
_functionStore,
285285
serializer,
286286
ClusterInfo.ReplicaId,
287-
_messageWatchdog.Notify
287+
_messageWatchdog
288288
);
289289

290290
var registration = new FuncRegistration<TParam, TReturn>(
@@ -372,7 +372,7 @@ private ParamlessRegistration RegisterParamless(
372372
_functionStore,
373373
serializer,
374374
ClusterInfo.ReplicaId,
375-
_messageWatchdog.Notify
375+
_messageWatchdog
376376
);
377377

378378
var registration = new ParamlessRegistration(
@@ -460,7 +460,7 @@ public ActionRegistration<TParam> RegisterAction<TParam>(
460460
_functionStore,
461461
serializer,
462462
ClusterInfo.ReplicaId,
463-
_messageWatchdog.Notify
463+
_messageWatchdog
464464
);
465465
var registration = new ActionRegistration<TParam>(
466466
flowType,
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1-
using System;
21
using System.Threading.Tasks;
32
using Cleipnir.ResilientFunctions.CoreRuntime.Serialization;
3+
using Cleipnir.ResilientFunctions.CoreRuntime.Watchdogs;
44
using Cleipnir.ResilientFunctions.Domain;
55
using Cleipnir.ResilientFunctions.Storage;
66

77
namespace Cleipnir.ResilientFunctions.Messaging;
88

9-
public class MessageWriter(StoredId storedIdId, IMessageStore messageStore, ISerializer eventSerializer, ReplicaId publisherReplica, Action? notifyDelivery = null)
9+
public class MessageWriter
1010
{
11+
private readonly StoredId _storedId;
12+
private readonly IMessageStore _messageStore;
13+
private readonly ISerializer _eventSerializer;
14+
private readonly ReplicaId _publisherReplica;
15+
private readonly MessageWatchdog? _messageWatchdog;
16+
17+
internal MessageWriter(StoredId storedId, IMessageStore messageStore, ISerializer eventSerializer, ReplicaId publisherReplica, MessageWatchdog? messageWatchdog = null)
18+
{
19+
_storedId = storedId;
20+
_messageStore = messageStore;
21+
_eventSerializer = eventSerializer;
22+
_publisherReplica = publisherReplica;
23+
_messageWatchdog = messageWatchdog;
24+
}
25+
1126
public async Task AppendMessage<TMessage>(TMessage message, string? idempotencyKey = null, string? sender = null, string? receiver = null) where TMessage : class
1227
{
13-
var eventJson = eventSerializer.Serialize(message, message.GetType());
14-
var eventType = eventSerializer.SerializeType(message.GetType());
28+
var eventJson = _eventSerializer.Serialize(message, message.GetType());
29+
var eventType = _eventSerializer.SerializeType(message.GetType());
1530

16-
var storedMessage = new StoredMessage(eventJson, eventType, Position: 0, Replica: publisherReplica, IdempotencyKey: idempotencyKey, Sender: sender, Receiver: receiver);
17-
await messageStore.AppendMessages([new StoredIdAndMessage(storedIdId, storedMessage)]);
31+
var storedMessage = new StoredMessage(eventJson, eventType, Position: 0, Replica: _publisherReplica, IdempotencyKey: idempotencyKey, Sender: sender, Receiver: receiver);
32+
await _messageStore.AppendMessages([new StoredIdAndMessage(_storedId, storedMessage)]);
1833

1934
// Wake the MessageWatchdog so the appended message is delivered now rather than on the next poll.
20-
notifyDelivery?.Invoke();
35+
_messageWatchdog?.Notify();
2136
}
22-
}
37+
}

Core/Cleipnir.ResilientFunctions/Messaging/MessageWriters.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using Cleipnir.ResilientFunctions.CoreRuntime;
54
using Cleipnir.ResilientFunctions.CoreRuntime.Invocation;
65
using Cleipnir.ResilientFunctions.CoreRuntime.Serialization;
6+
using Cleipnir.ResilientFunctions.CoreRuntime.Watchdogs;
77
using Cleipnir.ResilientFunctions.Domain;
88
using Cleipnir.ResilientFunctions.Storage;
99

@@ -15,31 +15,31 @@ public class MessageWriters
1515
private readonly IFunctionStore _functionStore;
1616
private readonly ISerializer _serializer;
1717
private readonly ReplicaId _publisherReplica;
18-
private readonly Action? _notifyDelivery;
18+
private readonly MessageWatchdog? _messageWatchdog;
1919

20-
public MessageWriters(
20+
internal MessageWriters(
2121
StoredType storedType,
2222
IFunctionStore functionStore,
2323
ISerializer serializer,
2424
ReplicaId publisherReplica,
25-
Action? notifyDelivery = null)
25+
MessageWatchdog? messageWatchdog = null)
2626
{
2727
_storedType = storedType;
2828
_functionStore = functionStore;
2929
_serializer = serializer;
3030
_publisherReplica = publisherReplica;
31-
_notifyDelivery = notifyDelivery;
31+
_messageWatchdog = messageWatchdog;
3232
}
3333

3434
public MessageWriter For(FlowInstance instance)
3535
{
3636
var storedId = StoredId.Create(_storedType, instance.Value);
37-
return new MessageWriter(storedId, _functionStore.MessageStore, _serializer, _publisherReplica, _notifyDelivery);
37+
return new MessageWriter(storedId, _functionStore.MessageStore, _serializer, _publisherReplica, _messageWatchdog);
3838
}
3939

4040
internal MessageWriter For(StoredId storedId)
4141
{
42-
return new MessageWriter(storedId, _functionStore.MessageStore, _serializer, _publisherReplica, _notifyDelivery);
42+
return new MessageWriter(storedId, _functionStore.MessageStore, _serializer, _publisherReplica, _messageWatchdog);
4343
}
4444

4545
public async Task AppendMessages(IReadOnlyList<BatchedMessage> messages)
@@ -57,6 +57,6 @@ public async Task AppendMessages(IReadOnlyList<BatchedMessage> messages)
5757
await _functionStore.MessageStore.AppendMessages(storedIdAndMessages);
5858

5959
// Wake the MessageWatchdog so the appended messages are delivered now rather than on the next poll.
60-
_notifyDelivery?.Invoke();
60+
_messageWatchdog?.Notify();
6161
}
6262
}

0 commit comments

Comments
 (0)