Skip to content

Commit 320b410

Browse files
committed
Making download publishers more resilient.
1 parent f32c7aa commit 320b410

File tree

5 files changed

+120
-71
lines changed

5 files changed

+120
-71
lines changed
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
22

33
using JetBrains.Annotations;
4+
using System;
45
using System.Collections.Generic;
6+
using System.Linq;
57

68
namespace PostSharp.Engineering.BuildTools.Build.Publishing;
79

810
[PublicAPI]
9-
public class DocumentationPublisher : InvalidatingS3Publisher
11+
public class DocumentationPublisher : S3Publisher
1012
{
13+
private readonly string _documentationUrl;
14+
1115
public DocumentationPublisher( IReadOnlyCollection<S3PublisherConfiguration> configurations, string documentationUrl )
12-
: base( configurations, $"{documentationUrl}_api/invalidate?%{EnvironmentVariableNames.DocInvalidationKey}%" ) { }
16+
: base( configurations )
17+
{
18+
this._documentationUrl = documentationUrl;
19+
}
20+
21+
private string InvalidateUrl => $"{this._documentationUrl}_api/invalidate?%{EnvironmentVariableNames.DocInvalidationKey}%";
22+
23+
public override void AddDependencies( List<Publisher> publishers, int currentIndex )
24+
{
25+
if ( !publishers.Skip( currentIndex )
26+
.Any( p => p is HttpGetPublisher invalidator && invalidator.Url.StartsWith( this._documentationUrl, StringComparison.Ordinal ) ) )
27+
{
28+
publishers.Add( new HttpGetPublisher( this.InvalidateUrl ) );
29+
}
30+
}
1331
}
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
22

33
using JetBrains.Annotations;
4+
using System;
45
using System.Collections.Generic;
6+
using System.Linq;
57

68
namespace PostSharp.Engineering.BuildTools.Build.Publishing.Downloads;
79

810
[PublicAPI]
9-
public class DownloadPublisher : InvalidatingS3Publisher
11+
public class DownloadPublisher : S3Publisher
1012
{
11-
public DownloadPublisher( IReadOnlyCollection<S3PublisherConfiguration> configurations ) : base(
12-
configurations,
13-
$"https://www.postsharp.net/download/Refresh.ashx?p=%{EnvironmentVariableNames.DownloadsInvalidationKey}%" ) { }
13+
public DownloadPublisher( IReadOnlyCollection<S3PublisherConfiguration> configurations ) : base( configurations ) { }
14+
15+
private static string InvalidationUrl => $"https://www.postsharp.net/download/Refresh.ashx?p=%{EnvironmentVariableNames.DownloadsInvalidationKey}%";
16+
17+
public override void AddDependencies( List<Publisher> publishers, int currentIndex )
18+
{
19+
var invalidationUrl = InvalidationUrl;
20+
21+
if ( !publishers.Skip( currentIndex )
22+
.Any( p => p is HttpGetPublisher invalidator && invalidator.Url.Equals( invalidationUrl, StringComparison.Ordinal ) ) )
23+
{
24+
publishers.Add( new HttpGetPublisher( invalidationUrl ) );
25+
}
26+
}
1427
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2+
3+
using PostSharp.Engineering.BuildTools.Build.Model;
4+
using PostSharp.Engineering.BuildTools.Utilities;
5+
using System;
6+
using System.Net;
7+
using System.Net.Http;
8+
9+
namespace PostSharp.Engineering.BuildTools.Build.Publishing;
10+
11+
public class HttpGetPublisher : Publisher
12+
{
13+
public string Url { get; }
14+
15+
public HttpGetPublisher( string url )
16+
{
17+
this.Url = url;
18+
}
19+
20+
protected override bool Publish(
21+
BuildContext context,
22+
PublishSettings settings,
23+
(string Private, string Public) directories,
24+
BuildConfigurationInfo configuration,
25+
BuildArguments buildArguments,
26+
bool isPublic,
27+
ref bool hasTarget )
28+
{
29+
if ( settings.Dry )
30+
{
31+
return true;
32+
}
33+
34+
try
35+
{
36+
var url = Environment.ExpandEnvironmentVariables( this.Url );
37+
using var httpClient = new HttpClient();
38+
var invalidationResponse = httpClient.GetAsync( url ).GetAwaiter().GetResult();
39+
40+
if ( invalidationResponse.StatusCode != HttpStatusCode.OK )
41+
{
42+
context.Console.WriteError(
43+
$"Failed to invalidate {this.Url}: {invalidationResponse.StatusCode} {invalidationResponse.ReasonPhrase} / {invalidationResponse.Content.ReadAsString()}" );
44+
45+
return false;
46+
}
47+
}
48+
catch ( Exception e )
49+
{
50+
context.Console.WriteError( $"Failed to invalidate {this.Url}: {e.Message}" );
51+
52+
return false;
53+
}
54+
55+
return true;
56+
}
57+
}

src/PostSharp.Engineering.BuildTools/Build/Publishing/InvalidatingS3Publisher.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/PostSharp.Engineering.BuildTools/Build/Publishing/Publisher.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using JetBrains.Annotations;
44
using PostSharp.Engineering.BuildTools.Build.Model;
55
using PostSharp.Engineering.BuildTools.Docker;
6+
using System;
67
using System.Collections.Generic;
78
using System.Linq;
89

@@ -23,6 +24,8 @@ public abstract class Publisher : IBuildComponent
2324

2425
IEnumerable<IBuildComponent> IBuildComponent.Children => [];
2526

27+
public virtual void AddDependencies( List<Publisher> publishers, int currentIndex ) { }
28+
2629
protected abstract bool Publish(
2730
BuildContext context,
2831
PublishSettings settings,
@@ -41,13 +44,18 @@ public static bool PublishDirectory(
4144
bool isPublic,
4245
ref bool hasTarget )
4346
{
44-
var publishers = isPublic ? configuration.PublicPublishers : configuration.PrivatePublishers;
47+
var publishers = isPublic ? configuration.PublicPublishers?.ToList() : configuration.PrivatePublishers?.ToList();
4548

46-
if ( publishers is not { Length: > 0 } )
49+
if ( publishers == null || publishers.Count == 0 )
4750
{
4851
return true;
4952
}
5053

54+
for ( var i = 0; i < publishers.Count; i++ )
55+
{
56+
publishers[i].AddDependencies( publishers, i );
57+
}
58+
5159
var publishingSucceeded = true;
5260

5361
foreach ( var publisher in publishers )
@@ -62,15 +70,23 @@ public static bool PublishDirectory(
6270

6371
context.Console.WriteHeading( $"Publishing with {publisher.GetType().Name}" );
6472

65-
if ( !publisher.Publish(
66-
context,
67-
settings,
68-
directories,
69-
configuration,
70-
buildArguments,
71-
isPublic,
72-
ref hasTarget ) )
73+
try
74+
{
75+
if ( !publisher.Publish(
76+
context,
77+
settings,
78+
directories,
79+
configuration,
80+
buildArguments,
81+
isPublic,
82+
ref hasTarget ) )
83+
{
84+
publishingSucceeded = false;
85+
}
86+
}
87+
catch ( Exception e )
7388
{
89+
context.Console.WriteError( $"Publisher '' failed with {e.GetType().Name}: {e}" );
7490
publishingSucceeded = false;
7591
}
7692
}

0 commit comments

Comments
 (0)