-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathProgressiveHtmlReader.cs
More file actions
98 lines (82 loc) · 3.1 KB
/
ProgressiveHtmlReader.cs
File metadata and controls
98 lines (82 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
#if !NET40
using System.Threading.Tasks;
#endif
namespace JenkinsNET.Utilities
{
/// <summary>
/// Represents a method that will handle an
/// event when the job console output has changed.
/// </summary>
public delegate void ProgressiveHtmlChangedEventHandler(string newHtml);
/// <summary>
/// Utility class for reading progressive HTML output
/// from a running Jenkins Job.
/// </summary>
public class ProgressiveHtmlReader
{
private readonly JenkinsClient client;
private readonly string jobName;
private readonly string buildNumber;
private int readPos;
/// <summary>
/// Gets whether the reading of HTML has completed.
/// </summary>
public bool IsComplete {get; private set;}
/// <summary>
/// Occurs when the value of <see cref="Html"/> is changed.
/// </summary>
public event ProgressiveHtmlChangedEventHandler HtmlChanged;
/// <summary>
/// Gets the HTML that has been retrieved.
/// </summary>
public string Html {get; private set;}
/// <summary>
/// Creates a new instance of <see cref="ProgressiveHtmlReader"/>
/// attached to the specified Jenkins Job.
/// </summary>
/// <param name="client">The Jenkins client to use for network operations.</param>
/// <param name="jobName">The name of the Jenkins Job.</param>
/// <param name="buildNumber">The build-number of the running Jenkins Job.</param>
public ProgressiveHtmlReader(JenkinsClient client, string jobName, string buildNumber)
{
this.client = client ?? throw new ArgumentNullException(nameof(client));
this.jobName = jobName ?? throw new ArgumentNullException(nameof(jobName));
this.buildNumber = buildNumber ?? throw new ArgumentNullException(nameof(buildNumber));
}
/// <summary>
/// Retrieves and appends any additional HTML
/// returned by the running Jenkins Job.
/// </summary>
public void Update()
{
if (IsComplete) return;
var result = client.Builds.GetProgressiveHtml(jobName, buildNumber, readPos);
if (result.Size > 0) {
Html += result.Html;
HtmlChanged?.Invoke(result.Html);
readPos = result.Size;
}
if (!result.MoreData)
IsComplete = true;
}
#if !NET40 && NET_ASYNC
/// <summary>
/// Retrieves and appends any additional text returned
/// by the running Jenkins Job asynchronously.
/// </summary>
public async Task UpdateAsync()
{
if (IsComplete) return;
var result = await client.Builds.GetProgressiveHtmlAsync(jobName, buildNumber, readPos);
if (result.Size > 0) {
Html += result.Html;
HtmlChanged?.Invoke(result.Html);
readPos = result.Size;
}
if (!result.MoreData)
IsComplete = true;
}
#endif
}
}