@@ -10,12 +10,10 @@ namespace InEngine.Core.Queue
1010 public class Broker
1111 {
1212 public string QueueBaseName { get ; set ; } = "InEngine:Queue" ;
13- public string PrimaryWaitingQueueName { get { return QueueBaseName + ":Primary:Waiting" ; } }
14- public string PrimaryProcessingQueueName { get { return QueueBaseName + ":Primary:Processing" ; } }
15- public string PrimaryFailedQueueName { get { return QueueBaseName + ":Primary:Failed" ; } }
16- public string SecondaryWaitingQueueName { get { return QueueBaseName + ":Secondary:Waiting" ; } }
17- public string SecondaryProcessingQueueName { get { return QueueBaseName + ":Secondary:Processing" ; } }
18- public string SecondaryFailedQueueName { get { return QueueBaseName + ":Secondary:Failed" ; } }
13+ public string QueueName { get ; internal set ; } = "Primary" ;
14+ public string PendingQueueName { get { return QueueBaseName + $ ":{ QueueName } :Pending"; } }
15+ public string InProgressQueueName { get { return QueueBaseName + $ ":{ QueueName } :InProgress"; } }
16+ public string FailedQueueName { get { return QueueBaseName + $ ":{ QueueName } :Failed"; } }
1917 public static Lazy < ConnectionMultiplexer > lazyConnection = new Lazy < ConnectionMultiplexer > ( ( ) => {
2018 var queueSettings = InEngineSettings . Make ( ) . Queue ;
2119 var redisConfig = ConfigurationOptions . Parse ( $ "{ queueSettings . RedisHost } :{ queueSettings . RedisPort } ") ;
@@ -39,17 +37,26 @@ public IDatabase Redis
3937 public static int RedisPort { get ; set ; }
4038 public static string RedisPassword { get ; set ; }
4139
42- public static Broker Make ( )
40+ public Broker ( )
41+ { }
42+
43+ public Broker ( bool useSecondaryQueue ) : this ( )
44+ {
45+ if ( useSecondaryQueue )
46+ QueueName = "Secondary" ;
47+ }
48+
49+ public static Broker Make ( bool useSecondaryQueue = false )
4350 {
44- return new Broker ( ) {
51+ return new Broker ( useSecondaryQueue ) {
4552 QueueBaseName = InEngineSettings . Make ( ) . Queue . QueueName
4653 } ;
4754 }
4855
49- public void Publish ( ICommand command , bool useSecondaryQueue = false )
56+ public void Publish ( ICommand command )
5057 {
5158 Redis . ListLeftPush (
52- useSecondaryQueue ? SecondaryWaitingQueueName : PrimaryWaitingQueueName ,
59+ PendingQueueName ,
5360 new Message ( ) {
5461 CommandClassName = command . GetType ( ) . FullName ,
5562 CommandAssemblyName = command . GetType ( ) . Assembly . GetName ( ) . Name + ".dll" ,
@@ -58,13 +65,9 @@ public void Publish(ICommand command, bool useSecondaryQueue = false)
5865 ) ;
5966 }
6067
61- public bool Consume ( bool useSecondaryQueue = false )
68+ public bool Consume ( )
6269 {
63- var waitingQueueName = useSecondaryQueue ? SecondaryWaitingQueueName : PrimaryWaitingQueueName ;
64- var processingQueueName = useSecondaryQueue ? SecondaryProcessingQueueName : PrimaryProcessingQueueName ;
65- var failedQueueName = useSecondaryQueue ? SecondaryFailedQueueName : PrimaryFailedQueueName ;
66-
67- var stageMessageTask = Redis . ListRightPopLeftPush ( waitingQueueName , processingQueueName ) ;
70+ var stageMessageTask = Redis . ListRightPopLeftPush ( PendingQueueName , InProgressQueueName ) ;
6871 var serializedMessage = stageMessageTask . ToString ( ) ;
6972 if ( serializedMessage == null )
7073 return false ;
@@ -83,94 +86,58 @@ public bool Consume(bool useSecondaryQueue = false)
8386 }
8487 catch ( Exception exception )
8588 {
86- Redis . ListRemove ( processingQueueName , serializedMessage , 1 ) ;
87- Redis . ListLeftPush ( failedQueueName , stageMessageTask ) ;
89+ Redis . ListRemove ( InProgressQueueName , serializedMessage , 1 ) ;
90+ Redis . ListLeftPush ( FailedQueueName , stageMessageTask ) ;
8891 throw new CommandFailedException ( "Consumed command failed." , exception ) ;
8992 }
9093
9194 try
9295 {
93- Redis . ListRemove ( processingQueueName , serializedMessage , 1 ) ;
96+ Redis . ListRemove ( InProgressQueueName , serializedMessage , 1 ) ;
9497 }
9598 catch ( Exception exception )
9699 {
97- throw new CommandFailedException ( $ "Failed to remove completed message from queue: { processingQueueName } ", exception ) ;
100+ throw new CommandFailedException ( $ "Failed to remove completed message from queue: { InProgressQueueName } ", exception ) ;
98101 }
99102
100103 return true ;
101104 }
102105
103- #region Primary Queue Management Methods
104- public long GetPrimaryWaitingQueueLength ( )
105- {
106- return Redis . ListLength ( PrimaryWaitingQueueName ) ;
107- }
108-
109- public long GetPrimaryProcessingQueueLength ( )
110- {
111- return Redis . ListLength ( PrimaryProcessingQueueName ) ;
112- }
113-
114- public long GetPrimaryFailedQueueLength ( )
115- {
116- return Redis . ListLength ( PrimaryFailedQueueName ) ;
117- }
118-
119- public bool ClearPrimaryWaitingQueue ( )
106+ #region Queue Management Methods
107+ public long GetPendingQueueLength ( )
120108 {
121- return Redis . KeyDelete ( PrimaryWaitingQueueName ) ;
109+ return Redis . ListLength ( PendingQueueName ) ;
122110 }
123111
124- public bool ClearPrimaryProcessingQueue ( )
112+ public long GetInProgressQueueLength ( )
125113 {
126- return Redis . KeyDelete ( PrimaryProcessingQueueName ) ;
114+ return Redis . ListLength ( InProgressQueueName ) ;
127115 }
128116
129- public bool ClearPrimaryFailedQueue ( )
117+ public long GetFailedQueueLength ( )
130118 {
131- return Redis . KeyDelete ( PrimaryFailedQueueName ) ;
119+ return Redis . ListLength ( FailedQueueName ) ;
132120 }
133- #endregion
134121
135- #region Secondary Queue Management Methods
136- public long GetSecondaryWaitingQueueLength ( )
122+ public bool ClearPendingQueue ( )
137123 {
138- return Redis . ListLength ( SecondaryWaitingQueueName ) ;
124+ return Redis . KeyDelete ( PendingQueueName ) ;
139125 }
140126
141- public long GetSecondaryProcessingQueueLength ( )
127+ public bool ClearInProgressQueue ( )
142128 {
143- return Redis . ListLength ( SecondaryProcessingQueueName ) ;
129+ return Redis . KeyDelete ( InProgressQueueName ) ;
144130 }
145131
146- public long GetSecondaryFailedQueueLength ( )
132+ public bool ClearFailedQueue ( )
147133 {
148- return Redis . ListLength ( SecondaryFailedQueueName ) ;
134+ return Redis . KeyDelete ( FailedQueueName ) ;
149135 }
150136
151- public bool ClearSecondaryWaitingQueue ( )
137+ public void RepublishFailedMessages ( )
152138 {
153- return Redis . KeyDelete ( SecondaryWaitingQueueName ) ;
154- }
155-
156- public bool ClearSecondaryProcessingQueue ( )
157- {
158- return Redis . KeyDelete ( SecondaryProcessingQueueName ) ;
159- }
160-
161-
162- public bool ClearSecondaryFailedQueue ( )
163- {
164- return Redis . KeyDelete ( SecondaryFailedQueueName ) ;
139+ Redis . ListRightPopLeftPush ( FailedQueueName , PendingQueueName ) ;
165140 }
166141 #endregion
167-
168- public void RepublishFailedMessages ( bool useSecondaryQueue )
169- {
170- Redis . ListRightPopLeftPush (
171- useSecondaryQueue ? SecondaryFailedQueueName : PrimaryFailedQueueName ,
172- useSecondaryQueue ? SecondaryWaitingQueueName : PrimaryWaitingQueueName
173- ) ;
174- }
175142 }
176143}
0 commit comments