6
6
7
7
namespace AutoClient
8
8
{
9
- public class Runner
9
+ public class Purchaser
10
10
{
11
11
private readonly ILog log ;
12
12
private readonly HttpClient client ;
@@ -16,7 +16,7 @@ public class Runner
16
16
private readonly Configuration config ;
17
17
private readonly ImageGenerator generator ;
18
18
19
- public Runner ( ILog log , HttpClient client , Address address , CodexApi codex , CancellationToken ct , Configuration config , ImageGenerator generator )
19
+ public Purchaser ( ILog log , HttpClient client , Address address , CodexApi codex , CancellationToken ct , Configuration config , ImageGenerator generator )
20
20
{
21
21
this . log = log ;
22
22
this . client = client ;
@@ -27,33 +27,25 @@ public Runner(ILog log, HttpClient client, Address address, CodexApi codex, Canc
27
27
this . generator = generator ;
28
28
}
29
29
30
- public async Task Run ( )
30
+ public void Start ( )
31
+ {
32
+ Task . Run ( Worker ) ;
33
+ }
34
+
35
+ private async Task Worker ( )
31
36
{
32
37
while ( ! ct . IsCancellationRequested )
33
38
{
34
- log . Log ( "New run!" ) ;
35
-
36
- try
37
- {
38
- await DoRun ( ) ;
39
-
40
- log . Log ( "Run succcessful." ) ;
41
- }
42
- catch ( Exception ex )
43
- {
44
- log . Error ( "Exception during run: " + ex ) ;
45
- }
46
-
47
- await FixedShortDelay ( ) ;
39
+ var pid = await StartNewPurchase ( ) ;
40
+ await WaitTillFinished ( pid ) ;
48
41
}
49
42
}
50
43
51
- private async Task DoRun ( )
44
+ private async Task < string > StartNewPurchase ( )
52
45
{
53
46
var file = await CreateFile ( ) ;
54
47
var cid = await UploadFile ( file ) ;
55
- var pid = await RequestStorage ( cid ) ;
56
- await WaitUntilStarted ( pid ) ;
48
+ return await RequestStorage ( cid ) ;
57
49
}
58
50
59
51
private async Task < string > CreateFile ( )
@@ -66,8 +58,7 @@ private async Task<ContentId> UploadFile(string filename)
66
58
// Copied from CodexNode :/
67
59
using var fileStream = File . OpenRead ( filename ) ;
68
60
69
- var logMessage = $ "Uploading file { filename } ...";
70
- log . Log ( logMessage ) ;
61
+ log . Log ( $ "Uploading file { filename } ...") ;
71
62
var response = await codex . UploadAsync ( fileStream , ct ) ;
72
63
73
64
if ( string . IsNullOrEmpty ( response ) ) FrameworkAssert . Fail ( "Received empty response." ) ;
@@ -91,7 +82,7 @@ private async Task<string> RequestStorage(ContentId cid)
91
82
Tolerance = config . HostTolerance
92
83
} , ct ) ;
93
84
94
- log . Log ( "Response : " + result ) ;
85
+ log . Log ( "Purchase ID : " + result ) ;
95
86
96
87
return result ;
97
88
}
@@ -108,59 +99,46 @@ private async Task<string> RequestStorage(ContentId cid)
108
99
if ( ! string . IsNullOrEmpty ( sp . Error ) ) log . Log ( $ "Purchase { pid } error is { sp . Error } ") ;
109
100
return sp . State ;
110
101
}
111
- catch ( Exception ex )
102
+ catch
112
103
{
113
104
return null ;
114
105
}
115
106
}
116
107
117
- private async Task WaitUntilStarted ( string pid )
108
+ private async Task WaitTillFinished ( string pid )
118
109
{
119
- log . Log ( "Waiting till contract is started, or expired ..." ) ;
110
+ log . Log ( "Waiting..." ) ;
120
111
try
121
112
{
122
113
var emptyResponseTolerance = 10 ;
123
114
while ( true )
124
115
{
125
- await FixedShortDelay ( ) ;
126
- var status = await GetPurchaseState ( pid ) ;
116
+ var status = ( await GetPurchaseState ( pid ) ) ? . ToLowerInvariant ( ) ;
127
117
if ( string . IsNullOrEmpty ( status ) )
128
118
{
129
119
emptyResponseTolerance -- ;
130
120
if ( emptyResponseTolerance == 0 )
131
121
{
132
- log . Log ( "Received 10 empty responses. Applying expiry delay, then carrying on ." ) ;
122
+ log . Log ( "Received 10 empty responses. Stop tracking this purchase ." ) ;
133
123
await ExpiryTimeDelay ( ) ;
134
124
return ;
135
125
}
136
- await FixedShortDelay ( ) ;
137
126
}
138
127
else
139
128
{
140
- if ( status . Contains ( "pending" ) || status . Contains ( "submitted" ) )
129
+ if ( status . Contains ( "cancel" ) ||
130
+ status . Contains ( "error" ) ||
131
+ status . Contains ( "finished" ) )
141
132
{
142
- await FixedShortDelay ( ) ;
143
- }
144
- else if ( status . Contains ( "started" ) )
145
- {
146
- log . Log ( "Started." ) ;
147
- await FixedDurationDelay ( ) ;
148
- }
149
- else if ( status . Contains ( "finished" ) )
150
- {
151
- log . Log ( "Purchase finished." ) ;
152
133
return ;
153
134
}
154
- else if ( status . Contains ( "error " ) )
135
+ if ( status . Contains ( "started " ) )
155
136
{
156
- await FixedShortDelay ( ) ;
157
- return ;
158
- }
159
- else
160
- {
161
- await FixedShortDelay ( ) ;
137
+ await FixedDurationDelay ( ) ;
162
138
}
163
139
}
140
+
141
+ await FixedShortDelay ( ) ;
164
142
}
165
143
}
166
144
catch ( Exception ex )
@@ -172,17 +150,17 @@ private async Task WaitUntilStarted(string pid)
172
150
173
151
private async Task FixedDurationDelay ( )
174
152
{
175
- await Task . Delay ( config . ContractDurationMinutes * 60 * 1000 ) ;
153
+ await Task . Delay ( config . ContractDurationMinutes * 60 * 1000 , ct ) ;
176
154
}
177
155
178
156
private async Task ExpiryTimeDelay ( )
179
157
{
180
- await Task . Delay ( config . ContractExpiryMinutes * 60 * 1000 ) ;
158
+ await Task . Delay ( config . ContractExpiryMinutes * 60 * 1000 , ct ) ;
181
159
}
182
160
183
161
private async Task FixedShortDelay ( )
184
162
{
185
- await Task . Delay ( 15 * 1000 ) ;
163
+ await Task . Delay ( 15 * 1000 , ct ) ;
186
164
}
187
165
}
188
166
}
0 commit comments