@@ -54,28 +54,40 @@ public string FailedQueuePath
5454
5555 public void Publish ( AbstractCommand command )
5656 {
57- if ( ! Directory . Exists ( PendingQueuePath ) )
58- Directory . CreateDirectory ( PendingQueuePath ) ;
59- var serializedMessage = new CommandEnvelope ( ) {
57+ PublishToQueue ( new CommandEnvelope ( ) {
6058 IsCompressed = UseCompression ,
6159 CommandClassName = command . GetType ( ) . FullName ,
6260 PluginName = command . GetType ( ) . Assembly . GetName ( ) . Name ,
6361 SerializedCommand = command . SerializeToJson ( UseCompression )
64- } . SerializeToJson ( ) ;
65- using ( var streamWriter = File . CreateText ( Path . Combine ( PendingQueuePath , Guid . NewGuid ( ) . ToString ( ) ) ) )
66- {
67- streamWriter . Write ( serializedMessage ) ;
68- }
62+ } , PendingQueuePath ) ;
63+ }
64+
65+ void PublishToQueue ( CommandEnvelope commandEnvelope , string queuePath )
66+ {
67+ if ( ! Directory . Exists ( queuePath ) )
68+ Directory . CreateDirectory ( queuePath ) ;
69+
70+ File . WriteAllText (
71+ Path . Combine ( queuePath , Guid . NewGuid ( ) . ToString ( ) ) ,
72+ commandEnvelope . SerializeToJson ( )
73+ ) ;
6974 }
7075
7176 public void Consume ( CancellationToken cancellationToken )
7277 {
7378 try
7479 {
75- while ( true )
80+ while ( true )
7681 {
77- if ( Consume ( ) == null )
78- Thread . Sleep ( 5000 ) ;
82+ try
83+ {
84+ if ( Consume ( ) == null )
85+ Thread . Sleep ( 5000 ) ;
86+ }
87+ catch ( Exception exception )
88+ {
89+ Log . Error ( exception ) ;
90+ }
7991 cancellationToken . ThrowIfCancellationRequested ( ) ;
8092 }
8193 }
@@ -109,11 +121,12 @@ public ICommandEnvelope Consume()
109121 return null ;
110122 }
111123 consumeLock . ReleaseMutex ( ) ;
112-
113- var commandEnvelope = File . ReadAllText ( inProgressFilePath ) . DeserializeFromJson < CommandEnvelope > ( ) as ICommandEnvelope ;
114- var command = commandEnvelope . GetCommandInstance ( ) ;
115- command . CommandLifeCycle . IncrementRetry ( ) ;
116- commandEnvelope . SerializedCommand = command . SerializeToJson ( UseCompression ) ;
124+
125+ var commandEnvelope = File . ReadAllText ( inProgressFilePath ) . DeserializeFromJson < CommandEnvelope > ( ) ;
126+ var command = commandEnvelope . GetCommandInstanceAndIncrementRetry ( ( ) => {
127+ File . Move ( inProgressFilePath , Path . Combine ( FailedQueuePath , fileInfo . Name ) ) ;
128+ } ) ;
129+
117130 try
118131 {
119132 command . WriteSummaryToConsole ( ) ;
@@ -122,8 +135,10 @@ public ICommandEnvelope Consume()
122135 catch ( Exception exception )
123136 {
124137 Log . Error ( exception ) ;
125- if ( command . CommandLifeCycle . ShouldRetry ( ) )
126- File . Move ( inProgressFilePath , Path . Combine ( PendingQueuePath , fileInfo . Name ) ) ;
138+ if ( command . CommandLifeCycle . ShouldRetry ( ) ) {
139+ PublishToQueue ( commandEnvelope , PendingQueuePath ) ;
140+ File . Delete ( Path . Combine ( PendingQueuePath , fileInfo . Name ) ) ;
141+ }
127142 else
128143 File . Move ( inProgressFilePath , Path . Combine ( FailedQueuePath , fileInfo . Name ) ) ;
129144 throw new CommandFailedException ( "Failed to consume command." , exception ) ;
0 commit comments