-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathSubscriptionsTaskFetchAlgorithm.cs
More file actions
542 lines (483 loc) · 25.5 KB
/
Copy pathSubscriptionsTaskFetchAlgorithm.cs
File metadata and controls
542 lines (483 loc) · 25.5 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
using Grayjay.ClientServer.Controllers;
using Grayjay.ClientServer.States;
using Grayjay.ClientServer.SubsExchange;
using Grayjay.ClientServer.Threading;
using Grayjay.Desktop.POC;
using Grayjay.Desktop.POC.Port.States;
using Grayjay.Engine;
using Grayjay.Engine.Exceptions;
using Grayjay.Engine.Models.Capabilities;
using Grayjay.Engine.Models.Feed;
using Grayjay.Engine.Pagers;
using System.Diagnostics;
using System.Linq;
using static System.Formats.Asn1.AsnWriter;
using Logger = Grayjay.Desktop.POC.Logger;
namespace Grayjay.ClientServer.Subscriptions.Algorithms
{
public abstract class SubscriptionsTaskFetchAlgorithm : SubscriptionFetchAlgorithm
{
private const string TAG = "SubscriptionsTaskFetchAlgorithm";
private ManagedThreadPool _pool = null;
private SubsExchangeClient _subsExchangeClient = null;
protected SubscriptionsTaskFetchAlgorithm(bool allowFailure = false, bool withCacheFallback = true, ManagedThreadPool pool = null, SubsExchangeClient subsExchangeClient = null) : base(allowFailure, withCacheFallback)
{
_pool = pool;
_subsExchangeClient = subsExchangeClient;
}
public override Dictionary<GrayjayPlugin, int> CountRequests(Dictionary<Subscription, List<string>> subs)
{
return GetSubscriptionTasks(subs)
.GroupBy(task => task.Client)
.ToDictionary(group => group.Key, group => group.Count(task => !task.FromCache));
}
public override Result GetSubscriptions(Dictionary<Subscription, List<string>> subs)
{
var tasks = GetSubscriptionTasks(subs);
var tasksGrouped = tasks.GroupBy(task => task.Client);
Logger.i(TAG, $"Starting Subscriptions Fetch:\n{string.Join("\n", tasksGrouped.Select(group => $" {group.Key.Config.Name}: {group.Count(task => !task.FromCache)}, Cached({group.Count(task => task.FromCache && !task.FromPeek)}), Peek({group.Count(task => task.FromPeek)})"))}");
try
{
foreach (var clientTasks in tasksGrouped)
{
var clientTaskCount = clientTasks.Count(task => !task.FromCache);
var clientCacheCount = clientTasks.Count(task => task.FromCache && !task.FromPeek);
var clientPeekCount = clientTasks.Count(task => !task.FromPeek);
var limit = clientTasks.Key.GetSubscriptionRateLimit();
/*
if (clientCacheCount > 0 && clientTaskCount > 0 && limit != null && clientTaskCount >= limit && StateApp.instance.contextOrNull?.let { it is MainActivity && it.isFragmentActive<SubscriptionsFeedFragment>() } == true)
{
UIDialogs.toast($"[{clientTasks.Key.Name}] only updating {clientTaskCount} most urgent channels (rqs). ({clientCacheCount} cached)");
}
*/
}
}
catch (Exception e)
{
Logger.e(TAG, "Error occurred in task.", e);
}
var exs = new List<Exception>();
ExchangeContract contract = null;
List<SubscriptionTask> providedTasks = null;
try
{
var contractableTasks = tasks.Where(x => !x.FromPeek && !x.FromCache && (x.Type == ResultCapabilities.TYPE_VIDEOS || x.Type == ResultCapabilities.TYPE_MIXED)).ToList();
contract = (contractableTasks.Count > 10) ? _subsExchangeClient?.RequestContract(contractableTasks.Select(y => new ChannelRequest(y.Url)).ToArray()) : null;
if ((contract?.Provided?.Length ?? 0) > 0)
Logger.i<SubscriptionFetchAlgorithm>($"Received subscription exchange contract (Requires {contract?.Required?.Length}, Provides {contract?.Provided?.Length}), ID: {contract.ID}");
if (contract != null && contract.Required.Length > 0)
{
providedTasks = new List<SubscriptionTask>();
foreach (var task in tasks.ToList())
{
if (!task.FromCache && !task.FromPeek && contract.Provided.Contains(task.Url))
{
providedTasks.Add(task);
tasks.Remove(task);
}
}
}
}
catch(Exception ex)
{
Logger.e<SubscriptionFetchAlgorithm>("Failed to retrieve SubsExchange contract due to: " + ex.Message, ex);
}
var failedPlugins = new List<string>();
var cachedChannels = new List<string>();
var forkTasks = ExecuteSubscriptionTasks(tasks, failedPlugins, cachedChannels);
var taskResults = new List<SubscriptionTaskResult>();
int resolveCount = 0;
long resolveTime = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
foreach (var task in forkTasks)
{
try
{
task.Wait();
var result = task.Result;
if (result != null)
{
if (result.Pager != null)
{
taskResults.Add(result);
}
if (result.Exception != null)
{
var ex = result.Exception;
Exception nonRuntimeEx = ex;// findNonRuntimeException(ex);
if (nonRuntimeEx != null && (nonRuntimeEx is PluginException || nonRuntimeEx is ChannelException))
{
exs.Add(nonRuntimeEx);
}
else
{
throw ex.InnerException ?? ex;
}
}
}
}
catch (Exception ex)
{
Exception nonRuntimeEx = ex;// findNonRuntimeException(ex.InnerException);
if (nonRuntimeEx != null && (nonRuntimeEx is PluginException || nonRuntimeEx is ChannelException))
{
exs.Add(nonRuntimeEx);
}
else
{
throw ex.InnerException ?? ex;
}
}
}
sw.Stop();
int timeTotal = ((int)sw.ElapsedMilliseconds);
//Resolve Subscription Exchange
if(contract != null)
{
void Resolve()
{
try
{
Stopwatch sw = Stopwatch.StartNew();
var resolves = taskResults.Where(x => x.Pager != null && (x.Task.Type == ResultCapabilities.TYPE_MIXED || x.Task.Type == ResultCapabilities.TYPE_VIDEOS) && contract.Required.Contains(x.Task.Url))
.Select(x => new ChannelResolve(x.Task.Url, x.Pager.GetResults().Where(x => x is PlatformVideo).ToArray()))
.ToArray();
var resolveRequestStart = DateTime.Now;
var resolve = _subsExchangeClient.ResolveContract(contract, resolves);
Logger.i<Subscription>($"Subscription Exchange contract resolved request in {DateTime.Now.Subtract(resolveRequestStart).TotalMilliseconds}ms");
if (resolve != null)
{
resolveCount = resolves.Length;
Logger.i(TAG, $"SubsExchange resolved (Res: {resolves.Length}, Prov: {resolve.Length})");
foreach (var result in resolve)
{
var task = providedTasks?.FirstOrDefault(x => x.Url == result.ChannelUrl);
if (task != null)
{
taskResults.Add(new SubscriptionTaskResult(task, new AdhocPager<PlatformContent>((i) => new PlatformContent[0], result.Content), null));
providedTasks.Remove(task);
}
}
}
if (providedTasks != null)
{
foreach (var task in providedTasks)
{
taskResults.Add(new SubscriptionTaskResult(task, null, new InvalidOperationException("No data received from exchange")));
}
}
sw.Stop();
resolveTime = (long)sw.Elapsed.TotalMilliseconds;
Logger.i<SubscriptionFetchAlgorithm>($"Subscription Exchange contract resolved in {resolveTime}ms");
}
catch(Exception ex)
{
Logger.e<SubscriptionFetchAlgorithm>("Failed to resolve Subscription Exchange contract due to: " + ex.Message, ex);
}
}
try
{
if ((providedTasks?.Count ?? 0) == 0)
{
StateApp.ThreadPool.Run(() =>
{
Resolve();
});
}
else
Resolve();
}
catch(Exception ex)
{
Logger.e<SubscriptionFetchAlgorithm>($"Resolve failed", ex);
}
}
Logger.i("StateSubscriptions", $"Subscriptions results in {timeTotal}ms");
var groupedPagers = taskResults
.GroupBy(result => result.Task.Sub.Channel.Url)
.Select(entry =>
{
try
{
var sub = entry.Any() ? entry.First().Task.Sub : null;
var liveTasks = entry.Where(result => !result.Task.FromCache).ToList();
var cachedTasks = entry.Where(result => result.Task.FromCache);
var failedLiveTasks = liveTasks.Where(x => x.Exception != null);
liveTasks = liveTasks.Where(x => x.Exception == null).ToList();
if (failedLiveTasks.Any())
{
foreach (var item in failedLiveTasks)
Logger.e<SubscriptionFetchAlgorithm>($"Subscription live task failed for [{item.Task.Sub.Channel.Name}]", item.Exception);
exs.AddRange(failedLiveTasks.Select(x => x.Exception).DistinctBy(x => x.Message).ToList());
}
var innerLivePager = new MultiChronoContentPager(liveTasks.Select(result => result.Pager), true);
innerLivePager.Initialize();
var livePager = liveTasks.Any() ? StateCache.CachePagerResults(innerLivePager, it => SetNewCacheHit(sub, it)) : null;
IPager<PlatformContent> cachedPager = cachedTasks.Any() ? new MultiChronoContentPager(cachedTasks.Select(result => result.Pager).ToList(), true) : null;
if (cachedPager != null && cachedPager is MultiPager<PlatformContent> mcp)
mcp.Initialize();
if (livePager != null && cachedPager == null)
return livePager;
else if (livePager == null && cachedPager != null)
return cachedPager;
else if (cachedPager == null)
return new EmptyPager<PlatformContent>();
else
{
var mergedPager = new MultiChronoContentPager(new List<IPager<PlatformContent>>() { livePager, cachedPager }, true);
mergedPager.Initialize();
return mergedPager;
}
}
catch(Exception ex)
{
Logger.e<SubscriptionFetchAlgorithm>($"Failed to resolve a pager for subscription task", ex);
return new EmptyPager<PlatformContent>();
}
}).ToList();
var pager = new MultiChronoContentPager(groupedPagers, AllowFailure, 30);
pager.Initialize();
return new Result(new DedupContentPager(pager), exs);
}
public List<Task<SubscriptionTaskResult>> ExecuteSubscriptionTasks(List<SubscriptionTask> tasks, List<string> failedPlugins, List<string> cachedChannels)
{
var forkTasks = new List<Task<SubscriptionTaskResult>>();
var finished = 0;
foreach (var task in tasks)
{
Task<SubscriptionTaskResult> forkTask = null;
if (_pool == null)
{
forkTask = new Task<SubscriptionTaskResult>(() =>
{
if(task.FromPeek)
{
try
{
var peekResults = StatePlatform.PeekChannelContents(task.Client, task.Url, task.Type);
var mostRecent = peekResults.FirstOrDefault();
task.Sub.LastPeekVideo = mostRecent?.DateTime ?? DateTime.MinValue;
task.Sub.SaveAsync();
var cacheItems = peekResults.Where(x => x.DateTime != default(DateTime) && x.DateTime > task.Sub.LastVideoUpdate);
foreach(var item in cacheItems)
{
if (item.Author.Thumbnail == null || item.Author.Thumbnail.Length == 0)
item.Author.Thumbnail = task.Sub.Channel.Thumbnail;
}
StateCache.CacheContents(cacheItems, false);
}
catch(Exception ex)
{
Logger.e(nameof(SubscriptionsTaskFetchAlgorithm), $"Subscription peek [{task.Sub.Channel.Name}] failed", ex);
}
}
lock (cachedChannels)
{
if (task.FromCache || task.FromPeek)
{
finished++;
SetProgress(finished, forkTasks.Count);
if (!cachedChannels.Contains(task.Url))
{
return new SubscriptionTaskResult(task, null, null);
}
else
{
cachedChannels.Add(task.Url);
return new SubscriptionTaskResult(task, StateCache.GetChannelCachePager(task.Url), null);
}
}
}
var shouldIgnore = failedPlugins.Contains(task.Client.ID);
if (shouldIgnore)
{
return new SubscriptionTaskResult(task, null, null); //skipped
}
Exception taskEx = null;
IPager<PlatformContent> pager = null;
try
{
Stopwatch watch = new Stopwatch();
watch.Start();
pager = StatePlatform.GetChannelContent(task.Client,
task.Url, task.Type, ResultCapabilities.ORDER_CHONOLOGICAL);
var initialPage = pager.GetResults();
task.Sub.UpdateSubscriptionState(task.Type, initialPage);
task.Sub.Save();
finished++;
SetProgress(finished, forkTasks.Count);
watch.Stop();
Logger.i("StateSubscriptions", $"Subscription [{task.Sub.Channel.Name}]:{task.Type} results in {watch.ElapsedMilliseconds}ms");
return new SubscriptionTaskResult(task, pager, null);
}
catch (Exception ex)
{
Logger.e(nameof(StateSubscriptions), $"Subscription [{task.Sub.Channel.Name}] failed", ex);
var channelEx = new ChannelException(task.Sub.Channel, ex);
finished++;
SetProgress(finished, forkTasks.Count);
if (ex is ScriptCaptchaRequiredException capEx)
{
lock (failedPlugins)
{
if (capEx.Config is PluginConfig && !failedPlugins.Contains(capEx.Config.ID))
{
Logger.w(nameof(StateSubscriptions), $"Subscriptions ignoring plugin [{capEx.Config.Name}] due to Captcha");
failedPlugins.Add(capEx.Config.ID);
}
}
}
else if (ex is ScriptCriticalException critEx)
{
lock (failedPlugins)
{
if (critEx.Config is PluginConfig && !failedPlugins.Contains(critEx.Config.ID))
{
Logger.w(nameof(StateSubscriptions), $"Subscriptions ignoring plugin [{critEx.Config.Name}] due to critical exception:\n{ex.Message}");
failedPlugins.Add(critEx.Config.ID);
}
}
}
if (!WithCacheFallback)
throw channelEx;
else
{
Logger.i(nameof(StateSubscriptions), $"Channel {task.Sub.Channel.Name} failed, substituting with cache");
pager = StateCache.GetChannelCachePager(task.Sub.Channel.Url);
taskEx = ex;
return new SubscriptionTaskResult(task, pager, taskEx);
}
}
});
forkTask.Start();//TODO: Threadpooled
}
else
{
TaskCompletionSource<SubscriptionTaskResult> source = new TaskCompletionSource<SubscriptionTaskResult>();
Action act = () =>
{
lock (cachedChannels)
{
if (task.FromCache)
{
finished++;
SetProgress(finished, forkTasks.Count);
if (!cachedChannels.Contains(task.Url))
{
source.SetResult(new SubscriptionTaskResult(task, null, null));
return;
}
else
{
cachedChannels.Add(task.Url);
source.SetResult(new SubscriptionTaskResult(task, StateCache.GetChannelCachePager(task.Url), null));
return;
}
}
}
var shouldIgnore = failedPlugins.Contains(task.Client.ID);
if (shouldIgnore)
{
source.SetResult(new SubscriptionTaskResult(task, null, null)); //skipped
return;
}
Exception taskEx = null;
IPager<PlatformContent> pager = null;
try
{
Stopwatch watch = new Stopwatch();
watch.Start();
pager = StatePlatform.GetChannelContent(task.Client,
task.Url, task.Type, ResultCapabilities.ORDER_CHONOLOGICAL);
var initialPage = pager.GetResults();
task.Sub.UpdateSubscriptionState(task.Type, initialPage);
task.Sub.Save();
finished++;
SetProgress(finished, forkTasks.Count);
watch.Stop();
Logger.i("StateSubscriptions", $"Subscription [{task.Sub.Channel.Name}]:{task.Type} results in {watch.ElapsedMilliseconds}ms");
source.SetResult(new SubscriptionTaskResult(task, pager, null));
return;
}
catch (Exception ex)
{
Logger.e(nameof(StateSubscriptions), $"Subscription [{task.Sub.Channel.Name}] failed", ex);
var channelEx = new ChannelException(task.Sub.Channel, ex);
finished++;
SetProgress(finished, forkTasks.Count);
if (ex is ScriptCaptchaRequiredException capEx)
{
lock (failedPlugins)
{
if (capEx.Config is PluginConfig && !failedPlugins.Contains(capEx.Config.ID))
{
Logger.w(nameof(StateSubscriptions), $"Subscriptions ignoring plugin [{capEx.Config.Name}] due to Captcha");
failedPlugins.Add(capEx.Config.ID);
}
}
}
else if (ex is ScriptCriticalException critEx)
{
lock (failedPlugins)
{
if (critEx.Config is PluginConfig && !failedPlugins.Contains(critEx.Config.ID))
{
Logger.w(nameof(StateSubscriptions), $"Subscriptions ignoring plugin [{critEx.Config.Name}] due to critical exception:\n{ex.Message}");
failedPlugins.Add(critEx.Config.ID);
}
}
}
if (!WithCacheFallback)
throw channelEx;
else
{
Logger.i(nameof(StateSubscriptions), $"Channel {task.Sub.Channel.Name} failed, substituting with cache");
pager = StateCache.GetChannelCachePager(task.Sub.Channel.Url);
taskEx = ex;
source.SetResult(new SubscriptionTaskResult(task, pager, taskEx));
return;
}
}
};
_pool.Run(act);
forkTask = source.Task;
}
forkTasks.Add(forkTask);
}
return forkTasks;
}
public abstract List<SubscriptionTask> GetSubscriptionTasks(Dictionary<Subscription, List<string>> subs);
public class SubscriptionTask
{
public GrayjayPlugin Client { get; }
public Subscription Sub { get; }
public string Url { get; }
public string Type { get; }
public bool FromPeek { get; set; }
public bool FromCache { get; set; }
public int Urgency { get; set; }
public SubscriptionTask(GrayjayPlugin client, Subscription sub, string url, string type, bool fromCache = false, int urgency = 0)
{
Client = client;
Sub = sub;
Url = url;
Type = type;
FromCache = fromCache;
Urgency = urgency;
}
}
public class SubscriptionTaskResult
{
public SubscriptionTask Task { get; }
public IPager<PlatformContent> Pager { get; }
public Exception Exception { get; }
public SubscriptionTaskResult(SubscriptionTask task, IPager<PlatformContent> pager, Exception exception)
{
Task = task;
Pager = pager;
Exception = exception;
}
}
}
}