-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
172 lines (138 loc) · 4.4 KB
/
Program.cs
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
// See https://aka.ms/new-console-template for more information
class OctoParallel
{
public OctoParallel(OctoParallelState state)
{
State = state;
}
public OctoParallelState State { get; set; }
public Task Execute(int maxParallelism)
{
var nextItems = State.GetNextItems(maxParallelism);
if (nextItems.Count() == 0)
{
return Task.CompletedTask;
}
var tasks = nextItems.Select(item => Task.Run(() => item.Execute()));
return Task.WhenAll(tasks);
}
public async Task Execute2(int maxParallelism)
{
var nextItems = State.GetNextItems(maxParallelism);
while (nextItems.Count() > 0)
{
var tasks = nextItems.Select(item => Task.Run(() => item.Execute()));
await Task.WhenAll(tasks);
}
}
}
public class WorkItem
{
public Guid Id { get; set; }
protected readonly Action action;
public WorkItem(Guid id, Action action)
{
this.Id = id;
this.action = action;
}
public bool IsCompleted { get; protected set; }
public bool IsExecuting { get; protected set; }
public virtual void Execute()
{
IsExecuting = true;
action.Invoke();
IsCompleted = true;
IsExecuting = false;
}
public virtual void Complete()
{
// NOP
}
}
public class EventDrivenWorkItem : WorkItem
{
public EventDrivenWorkItem(Guid id, Action action) : base(id, action)
{
}
public override void Execute()
{
IsExecuting = true;
action.Invoke();
}
public override void Complete()
{
IsCompleted = true;
IsExecuting = false;
}
}
public class OctoParallelState
{
public List<WorkItem> WorkItems { get; set; } = new List<WorkItem>();
public IEnumerable<WorkItem> GetNextItems(int maxParallelism)
{
var currentlyExecutingCount = WorkItems.Count(i => i.IsExecuting);
var capacity = maxParallelism - currentlyExecutingCount;
return WorkItems
.Where(i => !i.IsExecuting && !i.IsCompleted)
.Take(capacity > 0 ? capacity : 0);
}
}
class ControllerBase
{
public ControllerBase(OctoParallel octoParallel, OctoParallelState state)
{
OctoParallel = octoParallel;
State = state;
}
public OctoParallel OctoParallel { get; set; }
public OctoParallelState State { get; set; }
public async Task ExecuteBase(int maxParallelism)
{
await OctoParallel.Execute(maxParallelism);
}
public async Task PreExecute(Func<Guid, Action, WorkItem> workItemFactory)
{
await Task.CompletedTask;
var items = new List<WorkItem>
{
workItemFactory.Invoke(Guid.NewGuid(), () => Console.WriteLine("Executed 1.")),
workItemFactory.Invoke(Guid.NewGuid(), () => Console.WriteLine("Executed 2.")),
workItemFactory.Invoke(Guid.NewGuid(), () => Console.WriteLine("Executed 3."))
};
State.WorkItems = items;
}
}
class Controller : ControllerBase
{
public Controller(OctoParallel octoParallel, OctoParallelState state) : base(octoParallel, state)
{
}
public async Task Execute(int maxParallelism, Func<Guid, Action, WorkItem>? workItemFactory)
{
await PreExecute(workItemFactory ?? ((id, action) => new WorkItem(id, action)));
await ExecuteBase(maxParallelism);
}
}
public class Execution
{
private int maxParallelism;
public OctoParallelState OctoParallelState { get; set; } = new OctoParallelState();
public async Task Start(int maxParallelism)
{
this.maxParallelism = maxParallelism;
var octoParallel = new OctoParallel(OctoParallelState);
var controller = new Controller(octoParallel, OctoParallelState);
await controller.Execute(maxParallelism, ((id, action) => new EventDrivenWorkItem(id, action)));
}
public async Task StatusCheck(StatusUpdate statusUpdate)
{
if (statusUpdate.IsComplete)
{
var octoParallel = new OctoParallel(OctoParallelState);
var controller = new Controller(octoParallel, OctoParallelState);
OctoParallelState.WorkItems.FirstOrDefault(i => i.Id == statusUpdate.workItemId)?.Complete();
await controller.ExecuteBase(maxParallelism);
}
}
}
public record StatusUpdate(bool IsComplete, Guid workItemId);