11using System ;
22using System . Collections . Generic ;
33using System . Net . Http ;
4+ using System . Net . Http . Headers ;
45using System . Text ;
56using System . Threading ;
67using System . Threading . Tasks ;
@@ -9,45 +10,43 @@ namespace PipelinesTestLogger
910{
1011 internal class LoggerQueue
1112 {
12- private static readonly HttpClient client = new HttpClient ( ) ;
13+ private static readonly HttpClient _client = new HttpClient ( ) ;
1314
14- /// <summary>
15- /// it is localhost with a random port, e.g. http://localhost:9023/
16- /// </summary>
17- private readonly string appveyorApiUrl ;
15+ private readonly string _apiUrl ;
1816
19- private readonly AsyncProducerConsumerCollection < string > queue = new AsyncProducerConsumerCollection < string > ( ) ;
20- private readonly Task consumeTask ;
21- private readonly CancellationTokenSource consumeTaskCancellationSource = new CancellationTokenSource ( ) ;
17+ private readonly AsyncProducerConsumerCollection < string > _queue = new AsyncProducerConsumerCollection < string > ( ) ;
18+ private readonly Task _consumeTask ;
19+ private readonly CancellationTokenSource _consumeTaskCancellationSource = new CancellationTokenSource ( ) ;
2220
2321 private int totalEnqueued = 0 ;
2422 private int totalSent = 0 ;
2523
26- public LoggerQueue ( string appveyorApiUrl )
24+ public LoggerQueue ( string accessToken , string apiUrl )
2725 {
28- this . appveyorApiUrl = appveyorApiUrl ;
29- this . consumeTask = ConsumeItemsAsync ( consumeTaskCancellationSource . Token ) ;
26+ _client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , accessToken ) ;
27+ _apiUrl = apiUrl ;
28+ _consumeTask = ConsumeItemsAsync ( _consumeTaskCancellationSource . Token ) ;
3029 }
3130
3231 public void Enqueue ( string json )
3332 {
34- queue . Add ( json ) ;
33+ _queue . Add ( json ) ;
3534 totalEnqueued ++ ;
3635 }
3736
3837 public void Flush ( )
3938 {
4039 // Cancel any idle consumers and let them return
41- queue . Cancel ( ) ;
40+ _queue . Cancel ( ) ;
4241
4342 try
4443 {
45- // any active consumer will circle back around and batch post the remaining queue.
46- consumeTask . Wait ( TimeSpan . FromSeconds ( 60 ) ) ;
44+ // Any active consumer will circle back around and batch post the remaining queue.
45+ _consumeTask . Wait ( TimeSpan . FromSeconds ( 60 ) ) ;
4746
4847 // Cancel any active HTTP requests if still hasn't finished flushing
49- consumeTaskCancellationSource . Cancel ( ) ;
50- if ( ! consumeTask . Wait ( TimeSpan . FromSeconds ( 10 ) ) )
48+ _consumeTaskCancellationSource . Cancel ( ) ;
49+ if ( ! _consumeTask . Wait ( TimeSpan . FromSeconds ( 10 ) ) )
5150 {
5251 throw new TimeoutException ( "cancellation didn't happen quickly" ) ;
5352 }
@@ -56,48 +55,36 @@ public void Flush()
5655 {
5756 Console . WriteLine ( ex ) ;
5857 }
59-
60- #if DEBUG
61- Console . WriteLine ( "PipelinesTestLogger: {0} test results reported ({1} enqueued)." , totalSent , totalEnqueued ) ;
62- #endif
6358 }
6459
6560 private async Task ConsumeItemsAsync ( CancellationToken cancellationToken )
6661 {
6762 while ( true )
6863 {
69- string [ ] nextItems = await this . queue . TakeAsync ( ) ;
70- if ( nextItems == null || nextItems . Length == 0 ) return ; // Queue is cancelling and and empty.
71-
72- if ( nextItems . Length == 1 ) await PostItemAsync ( nextItems [ 0 ] , cancellationToken ) ;
73- else if ( nextItems . Length > 1 ) await PostBatchAsync ( nextItems , cancellationToken ) ;
64+ string [ ] nextItems = await _queue . TakeAsync ( ) ;
7465
75- if ( cancellationToken . IsCancellationRequested ) return ;
76- }
77- }
78-
79- private async Task PostItemAsync ( string json , CancellationToken cancellationToken )
80- {
81- HttpContent content = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
82- try
83- {
84- var response = await client . PostAsync ( appveyorApiUrl + "api/tests" , content , cancellationToken ) ;
85- response . EnsureSuccessStatusCode ( ) ;
86- totalSent += 1 ;
87- }
88- catch ( Exception e )
89- {
90- Console . WriteLine ( e ) ;
66+ if ( nextItems == null || nextItems . Length == 0 )
67+ {
68+ // Queue is canceling and is empty
69+ return ;
70+ }
71+
72+ await PostResultsAsync ( nextItems , cancellationToken ) ;
73+
74+ if ( cancellationToken . IsCancellationRequested )
75+ {
76+ return ;
77+ }
9178 }
9279 }
9380
94- private async Task PostBatchAsync ( ICollection < string > jsonEntities , CancellationToken cancellationToken )
81+ private async Task PostResultsAsync ( ICollection < string > jsonEntities , CancellationToken cancellationToken )
9582 {
96- var jsonArray = "[" + string . Join ( "," , jsonEntities ) + "]" ;
83+ string jsonArray = "[" + string . Join ( "," , jsonEntities ) + "]" ;
9784 HttpContent content = new StringContent ( jsonArray , Encoding . UTF8 , "application/json" ) ;
9885 try
9986 {
100- var response = await client . PostAsync ( appveyorApiUrl + "api/tests/batch" , content , cancellationToken ) ;
87+ HttpResponseMessage response = await _client . PostAsync ( _apiUrl , content , cancellationToken ) ;
10188 response . EnsureSuccessStatusCode ( ) ;
10289 totalSent += jsonEntities . Count ;
10390 }
0 commit comments