@@ -53,14 +53,20 @@ public WarService(
53
53
54
54
protected override async Task ExecuteScheduledTaskAsync ( CancellationToken cancellationToken )
55
55
{
56
+ Logger . LogInformation ( "Executing war service" ) ;
57
+
56
58
SetDateVariables ( ) ;
57
59
60
+ Logger . LogInformation ( "Setting variables" ) ;
61
+
58
62
WarServiceOptions options = Options . Value . Wars ;
59
63
60
64
using var scope = ScopeFactory . CreateScope ( ) ;
61
65
62
66
CacheDbContext dbContext = scope . ServiceProvider . GetRequiredService < CacheDbContext > ( ) ;
63
67
68
+ Logger . LogInformation ( "Querying wars" ) ;
69
+
64
70
List < CachedWar > cachedWars = await dbContext . Wars
65
71
. Where ( w =>
66
72
string . IsNullOrWhiteSpace ( w . WarTag ) &&
@@ -85,6 +91,8 @@ protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancel
85
91
tags . Add ( cachedWar . OpponentTag ) ;
86
92
}
87
93
94
+ Logger . LogInformation ( "Querying clans" ) ;
95
+
88
96
List < CachedClan > allCachedClans = await dbContext . Clans
89
97
. Where ( c => tags . Contains ( c . Tag ) )
90
98
. AsNoTracking ( )
@@ -97,6 +105,8 @@ protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancel
97
105
98
106
IClansApi clansApi = ApiFactory . Create < IClansApi > ( ) ;
99
107
108
+ Logger . LogInformation ( "Iterating wars" ) ;
109
+
100
110
try
101
111
{
102
112
foreach ( CachedWar cachedWar in cachedWars )
@@ -117,8 +127,12 @@ protected override async Task ExecuteScheduledTaskAsync(CancellationToken cancel
117
127
tasks . Add ( SendWarAnnouncementsAsync ( cachedWar , cachedClans . Select ( c => c . CurrentWar ) . ToArray ( ) , cancellationToken ) ) ;
118
128
}
119
129
130
+ Logger . LogInformation ( "Waiting for tasks to complete" ) ;
131
+
120
132
await Task . WhenAll ( tasks ) . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
121
133
134
+ Logger . LogInformation ( "Tasks are complete" ) ;
135
+
122
136
await dbContext . SaveChangesAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
123
137
}
124
138
finally
@@ -139,6 +153,8 @@ private async Task UpdateWarAsync(
139
153
CachedClan ? cachedOpponent ,
140
154
CancellationToken cancellationToken )
141
155
{
156
+ Logger . LogInformation ( "Creating cached clans" ) ;
157
+
142
158
if ( cachedClan == null && cachedOpponent ? . CurrentWar . IsExpired == true )
143
159
cachedClan = await CreateCachedClan ( clansApi , cachedWar . ClanTag , dbContext , cancellationToken ) . ConfigureAwait ( false ) ;
144
160
@@ -158,17 +174,21 @@ private async Task UpdateWarAsync(
158
174
. OrderByDescending ( c => c ? . CurrentWar . ExpiresAt )
159
175
. FirstOrDefault ( c => c ? . CurrentWar . PreparationStartTime == cachedWar . PreparationStartTime ) ;
160
176
161
- if ( cachedWar . Content != null &&
162
- clan ? . CurrentWar . Content != null &&
177
+ if ( cachedWar . Content != null &&
178
+ clan ? . CurrentWar . Content != null &&
163
179
CachedWar . HasUpdated ( cachedWar , clan . CurrentWar ) &&
164
180
ClanWarUpdated != null )
181
+ {
182
+ Logger . LogInformation ( "sending the war updated event" ) ;
183
+
165
184
await ClanWarUpdated . Invoke ( this , new ClanWarUpdatedEventArgs (
166
185
cachedWar . Content ,
167
186
clan . CurrentWar . Content ,
168
187
cachedClan ? . Content ,
169
188
cachedOpponent ? . Content ,
170
189
cancellationToken ) )
171
190
. ConfigureAwait ( false ) ;
191
+ }
172
192
173
193
if ( clan != null )
174
194
{
@@ -183,6 +203,9 @@ private async Task UpdateWarAsync(
183
203
c . CurrentWar . PreparationStartTime > cachedWar . PreparationStartTime
184
204
) ) )
185
205
cachedWar . IsFinal = true ;
206
+
207
+
208
+ Logger . LogInformation ( "Done sending war updated event" ) ;
186
209
}
187
210
188
211
private async Task < CachedClan ? > CreateCachedClan ( IClansApi clansApi , string tag , CacheDbContext dbContext , CancellationToken cancellationToken )
@@ -194,6 +217,8 @@ private async Task UpdateWarAsync(
194
217
{
195
218
_unmonitoredClans . Add ( tag ) ;
196
219
220
+ Logger . LogInformation ( "Creating cached clan war from current war" ) ;
221
+
197
222
CachedClanWar cachedClanWar = await CachedClanWar
198
223
. FromCurrentWarResponseAsync ( tag , Options . Value . RealTime == null ? default : new ( Options . Value . RealTime . Value ) , TimeToLiveProvider , clansApi , cancellationToken )
199
224
. ConfigureAwait ( false ) ;
@@ -240,7 +265,11 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
240
265
cachedWar . Announcements |= Announcements . WarStartingSoon ;
241
266
242
267
if ( ClanWarStartingSoon != null )
268
+ {
269
+ Logger . LogInformation ( "Clan war starting soon" ) ;
270
+
243
271
await ClanWarStartingSoon . Invoke ( this , new WarEventArgs ( cachedWar . Content , cancellationToken ) ) . ConfigureAwait ( false ) ;
272
+ }
244
273
}
245
274
246
275
if ( cachedWar . Announcements . HasFlag ( Announcements . WarEndingSoon ) == false &&
@@ -250,7 +279,10 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
250
279
cachedWar . Announcements |= Announcements . WarEndingSoon ;
251
280
252
281
if ( ClanWarEndingSoon != null )
282
+ {
283
+ Logger . LogInformation ( "Clan war ending soon" ) ;
253
284
await ClanWarEndingSoon . Invoke ( this , new WarEventArgs ( cachedWar . Content , cancellationToken ) ) . ConfigureAwait ( false ) ;
285
+ }
254
286
}
255
287
256
288
if ( cachedWar . Announcements . HasFlag ( Announcements . WarEndNotSeen ) == false &&
@@ -264,7 +296,11 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
264
296
cachedWar . Announcements |= Announcements . WarEndNotSeen ;
265
297
266
298
if ( ClanWarEndNotSeen != null )
299
+ {
300
+ Logger . LogInformation ( "Clan war end not seen" ) ;
301
+
267
302
await ClanWarEndNotSeen . Invoke ( this , new WarEventArgs ( cachedWar . Content , cancellationToken ) ) . ConfigureAwait ( false ) ;
303
+ }
268
304
}
269
305
270
306
if ( cachedWar . Announcements . HasFlag ( Announcements . WarEnded ) == false &&
@@ -274,8 +310,14 @@ private async Task SendWarAnnouncementsAsync(CachedWar cachedWar, CachedClanWar[
274
310
cachedWar . Announcements |= Announcements . WarEnded ;
275
311
276
312
if ( ClanWarEnded != null )
313
+ {
314
+ Logger . LogInformation ( "Clan war ended" ) ;
315
+
277
316
await ClanWarEnded . Invoke ( this , new WarEventArgs ( cachedWar . Content , cancellationToken ) ) . ConfigureAwait ( false ) ;
317
+ }
278
318
}
319
+
320
+ Logger . LogInformation ( "done sending war announcement" ) ;
279
321
}
280
322
catch ( Exception e )
281
323
{
0 commit comments