Skip to content

Commit ab5323c

Browse files
committed
Remove group worker when raise exceptions.
1 parent 6b41ffc commit ab5323c

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

GroupWorker.cs

+18-9
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ public GroupWorker(BaseHTTPClient[] clients,IMEI.IMEIGenerator generator)
2020
this.clients = clients;
2121
this.IMEIGenerator = generator;
2222
GroupName = clients.First().GetType().Name;
23+
24+
_cache = clients.ToDictionary(
25+
x => x.GetHashCode()
26+
, x => x.GetQueryAsync(
27+
IMEIGenerator.GetValidIMEI(SkipIMEICount++)
28+
.First()
29+
));
2330
}
2431

2532
public async Task<string[]> GetTask()
2633
{
2734
if(_cache.Count==0)
2835
{
29-
_cache = clients.ToDictionary(
30-
x=>x.GetHashCode()
31-
,x=>x.GetQueryAsync(
32-
IMEIGenerator.GetValidIMEI(SkipIMEICount++)
33-
.First()
34-
));
36+
throw new Exception();
3537
}
36-
3738
while(true)
3839
{
3940
await Task.Delay(1);
@@ -48,8 +49,16 @@ public async Task<string[]> GetTask()
4849
var result = await completedTask.Value;
4950
lst.Add(result);
5051

51-
_cache[completedTask.Key] = clients.First(x => x.GetHashCode() == completedTask.Key).GetQueryAsync(IMEIGenerator.GetValidIMEI(SkipIMEICount++)
52-
.First());
52+
try
53+
{
54+
_cache[completedTask.Key] = clients.First(x => x.GetHashCode() == completedTask.Key).GetQueryAsync(IMEIGenerator.GetValidIMEI(SkipIMEICount++)
55+
.First());
56+
}
57+
catch
58+
{
59+
_cache.Remove(completedTask.Key);
60+
}
61+
5362
}
5463
if (lst.Count > 0)
5564
return lst.ToArray();

Worker.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,20 @@ private async Task DoWork()
101101
while (true)
102102
{
103103
await Task.WhenAny(dic.Values);
104+
105+
var errors = dic.Where(x => x.Value.IsFaulted).ToArray();
106+
foreach(var error in errors)
107+
{
108+
dic.Remove(error.Key);
109+
}
110+
104111
foreach(var task in dic.Where(x=>x.Value.IsCompleted).ToArray())
105112
{
106-
dic[task.Key] = groupWorkers
107-
.First(x => x.GroupName == task.Key)
108-
.GetTask();
113+
114+
dic[task.Key] = groupWorkers
115+
.First(x => x.GroupName == task.Key)
116+
.GetTask();
117+
109118

110119
var result = await task.Value;
111120
foreach(var res in result)

0 commit comments

Comments
 (0)