11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Threading ;
5+ using System . Threading . Tasks ;
46using InEngine . Core . Exceptions ;
57using InEngine . Core . Queuing . Message ;
68using StackExchange . Redis ;
@@ -9,8 +11,10 @@ namespace InEngine.Core.Queuing.Clients
911{
1012 public class RedisClient : IQueueClient
1113 {
14+ public int Id { get ; set ; } = 0 ;
1215 public string QueueBaseName { get ; set ; } = "InEngineQueue" ;
1316 public string QueueName { get ; set ; } = "Primary" ;
17+ public string RecoveryQueueName { get { return QueueBaseName + $ ":{ QueueName } :Recovery"; } }
1418 public string PendingQueueName { get { return QueueBaseName + $ ":{ QueueName } :Pending"; } }
1519 public string InProgressQueueName { get { return QueueBaseName + $ ":{ QueueName } :InProgress"; } }
1620 public string FailedQueueName { get { return QueueBaseName + $ ":{ QueueName } :Failed"; } }
@@ -29,8 +33,17 @@ public class RedisClient : IQueueClient
2933 public bool UseCompression { get ; set ; }
3034 public int RedisDb { get ; set ; }
3135
36+ public RedisChannel RedisChannel { get ; set ; }
37+
38+ public void InitChannel ( )
39+ {
40+ if ( RedisChannel . IsNullOrEmpty )
41+ RedisChannel = new RedisChannel ( QueueBaseName , RedisChannel . PatternMode . Auto ) ;
42+ }
43+
3244 public void Publish ( AbstractCommand command )
3345 {
46+ InitChannel ( ) ;
3447 Redis . ListLeftPush (
3548 PendingQueueName ,
3649 new CommandEnvelope ( ) {
@@ -40,6 +53,39 @@ public void Publish(AbstractCommand command)
4053 SerializedCommand = command . SerializeToJson ( UseCompression )
4154 } . SerializeToJson ( )
4255 ) ;
56+
57+ var count = PublishToChannel ( $ "published command: { command . Name } ") ;
58+ }
59+
60+ public long PublishToChannel ( string message = "published command." )
61+ {
62+ InitChannel ( ) ;
63+ return Connection . GetSubscriber ( ) . Publish ( RedisChannel , message ) ;
64+ }
65+
66+ public void Recover ( )
67+ {
68+ for ( var i = 0 ; i < Redis . ListLength ( PendingQueueName ) ; i ++ )
69+ PublishToChannel ( ) ;
70+ }
71+
72+ public void Consume ( CancellationToken cancellationToken )
73+ {
74+ try
75+ {
76+ InitChannel ( ) ;
77+ Connection . GetSubscriber ( ) . Subscribe ( RedisChannel , delegate {
78+ Task . Factory . StartNew ( Consume , cancellationToken , TaskCreationOptions . LongRunning , TaskScheduler . Default ) ;
79+ } ) ;
80+ }
81+ catch ( OperationCanceledException )
82+ {
83+ return ;
84+ }
85+ catch ( Exception exception )
86+ {
87+ Console . WriteLine ( exception . Message ) ;
88+ }
4389 }
4490
4591 public ICommandEnvelope Consume ( )
@@ -51,7 +97,7 @@ public ICommandEnvelope Consume()
5197 var commandEnvelope = serializedMessage . DeserializeFromJson < CommandEnvelope > ( ) ;
5298 if ( commandEnvelope == null )
5399 throw new CommandFailedException ( "Could not deserialize the command." ) ;
54-
100+
55101 var command = QueueAdapter . ExtractCommandInstanceFromMessage ( commandEnvelope ) ;
56102 command . CommandLifeCycle . IncrementRetry ( ) ;
57103 commandEnvelope . SerializedCommand = command . SerializeToJson ( UseCompression ) ;
@@ -65,9 +111,10 @@ public ICommandEnvelope Consume()
65111 Redis . ListRemove ( InProgressQueueName , serializedMessage , 1 ) ;
66112 if ( command . CommandLifeCycle . ShouldRetry ( ) )
67113 Redis . ListLeftPush ( PendingQueueName , commandEnvelope . SerializeToJson ( ) ) ;
68- else {
114+ else
115+ {
69116 Redis . ListLeftPush ( FailedQueueName , commandEnvelope . SerializeToJson ( ) ) ;
70- throw new CommandFailedException ( "Failed to run consumed command." , exception ) ;
117+ throw new CommandFailedException ( "Failed to run consumed command." , exception ) ;
71118 }
72119 }
73120
0 commit comments