|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Threading; |
4 | 4 | using System.Threading.Tasks; |
| 5 | +using System.Web.Hosting; |
5 | 6 | using Autofac; |
6 | 7 | using SmartStore.Core.Infrastructure; |
7 | | -using SmartStore.Core.Infrastructure.DependencyManagement; |
| 8 | +using SmartStore.Utilities; |
8 | 9 |
|
9 | 10 | namespace SmartStore.Core.Async |
10 | 11 | { |
11 | 12 |
|
12 | 13 | public static class AsyncRunner |
13 | 14 | { |
| 15 | + private static readonly BackgroundWorkHost _host = new BackgroundWorkHost(); |
| 16 | + |
14 | 17 |
|
15 | 18 | /// <summary> |
16 | 19 | /// Execute's an async Task<T> method which has a void return value synchronously |
@@ -75,59 +78,143 @@ public static TResult RunSync<TResult>(Func<Task<TResult>> func) |
75 | 78 | return ret; |
76 | 79 | } |
77 | 80 |
|
78 | | - public static Task Run(Action<ILifetimeScope> action) |
| 81 | + public static Task Run(Action<ILifetimeScope, CancellationToken> action) |
79 | 82 | { |
80 | 83 | return Run(action, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); |
81 | 84 | } |
82 | 85 |
|
83 | | - public static Task Run(Action<ILifetimeScope> action, CancellationToken cancellationToken) |
| 86 | + public static Task Run(Action<ILifetimeScope, CancellationToken> action, CancellationToken cancellationToken) |
84 | 87 | { |
85 | 88 | return Run(action, cancellationToken, TaskCreationOptions.None, TaskScheduler.Default); |
86 | 89 | } |
87 | 90 |
|
88 | | - public static Task Run(Action<ILifetimeScope> action, TaskCreationOptions options) |
| 91 | + public static Task Run(Action<ILifetimeScope, CancellationToken> action, TaskCreationOptions options) |
89 | 92 | { |
90 | 93 | return Run(action, CancellationToken.None, options, TaskScheduler.Default); |
91 | 94 | } |
92 | 95 |
|
93 | | - public static Task Run(Action<ILifetimeScope> action, CancellationToken cancellationToken, TaskCreationOptions options) |
| 96 | + public static Task Run(Action<ILifetimeScope, CancellationToken> action, CancellationToken cancellationToken, TaskCreationOptions options) |
94 | 97 | { |
95 | 98 | return Run(action, cancellationToken, options, TaskScheduler.Default); |
96 | 99 | } |
97 | 100 |
|
98 | | - public static Task Run(Action<ILifetimeScope> action, TaskScheduler scheduler) |
| 101 | + public static Task Run(Action<ILifetimeScope, CancellationToken> action, TaskScheduler scheduler) |
99 | 102 | { |
100 | 103 | return Run(action, CancellationToken.None, TaskCreationOptions.None, scheduler); |
101 | 104 | } |
102 | | - |
103 | | - public static Task Run(Action<ILifetimeScope> action, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler) |
| 105 | + |
| 106 | + public static Task Run(Action<ILifetimeScope, CancellationToken> action, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler) |
104 | 107 | { |
105 | 108 | Guard.ArgumentNotNull(() => action); |
106 | 109 | Guard.ArgumentNotNull(() => scheduler); |
107 | 110 |
|
| 111 | + var ct = _host.CreateCompositeCancellationTokenSource(cancellationToken).Token; |
| 112 | + options |= TaskCreationOptions.LongRunning; // enforce an exclusive thread (not from pool) |
| 113 | + |
108 | 114 | var t = Task.Factory.StartNew(() => { |
109 | | - using (var container = EngineContext.Current.ContainerManager.Container.BeginLifetimeScope(AutofacLifetimeScopeProvider.HttpRequestTag)) |
| 115 | + var accessor = EngineContext.Current.ContainerManager.ScopeAccessor; |
| 116 | + using (var scope = accessor.BeginContextAwareScope()) |
110 | 117 | { |
111 | | - action(container); |
| 118 | + action(accessor.GetLifetimeScope(null), ct); |
112 | 119 | } |
113 | | - }, cancellationToken, options, scheduler); |
| 120 | + }, ct, options, scheduler); |
| 121 | + |
| 122 | + _host.Register(t, ct); |
114 | 123 |
|
115 | 124 | return t; |
116 | 125 | } |
117 | 126 |
|
118 | | - public static Task Run(Action<ILifetimeScope, object> action, object state, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler) |
| 127 | + public static Task Run(Action<ILifetimeScope, CancellationToken, object> action, object state, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler) |
119 | 128 | { |
120 | 129 | Guard.ArgumentNotNull(() => state); |
121 | 130 | Guard.ArgumentNotNull(() => action); |
122 | 131 | Guard.ArgumentNotNull(() => scheduler); |
123 | 132 |
|
| 133 | + var ct = _host.CreateCompositeCancellationTokenSource(cancellationToken).Token; |
| 134 | + options |= TaskCreationOptions.LongRunning; // enforce an exclusive thread (not from pool) |
| 135 | + |
| 136 | + var t = Task.Factory.StartNew((o) => |
| 137 | + { |
| 138 | + var accessor = EngineContext.Current.ContainerManager.ScopeAccessor; |
| 139 | + using (var scope = accessor.BeginContextAwareScope()) |
| 140 | + { |
| 141 | + action(accessor.GetLifetimeScope(null), ct, o); |
| 142 | + } |
| 143 | + }, state, ct, options, scheduler); |
| 144 | + |
| 145 | + _host.Register(t, ct); |
| 146 | + |
| 147 | + return t; |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, TResult> function) |
| 153 | + { |
| 154 | + return Run(function, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); |
| 155 | + } |
| 156 | + |
| 157 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, TResult> function, CancellationToken cancellationToken) |
| 158 | + { |
| 159 | + return Run(function, cancellationToken, TaskCreationOptions.None, TaskScheduler.Default); |
| 160 | + } |
| 161 | + |
| 162 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, TResult> function, TaskCreationOptions options) |
| 163 | + { |
| 164 | + return Run(function, CancellationToken.None, options, TaskScheduler.Default); |
| 165 | + } |
| 166 | + |
| 167 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, TResult> function, CancellationToken cancellationToken, TaskCreationOptions options) |
| 168 | + { |
| 169 | + return Run(function, cancellationToken, options, TaskScheduler.Default); |
| 170 | + } |
| 171 | + |
| 172 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, TResult> function, TaskScheduler scheduler) |
| 173 | + { |
| 174 | + return Run(function, CancellationToken.None, TaskCreationOptions.None, scheduler); |
| 175 | + } |
| 176 | + |
| 177 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, TResult> function, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler) |
| 178 | + { |
| 179 | + Guard.ArgumentNotNull(() => function); |
| 180 | + Guard.ArgumentNotNull(() => scheduler); |
| 181 | + |
| 182 | + var ct = _host.CreateCompositeCancellationTokenSource(cancellationToken).Token; |
| 183 | + options |= TaskCreationOptions.LongRunning; // enforce an exclusive thread (not from pool) |
| 184 | + |
| 185 | + var t = Task.Factory.StartNew(() => |
| 186 | + { |
| 187 | + var accessor = EngineContext.Current.ContainerManager.ScopeAccessor; |
| 188 | + using (var scope = accessor.BeginContextAwareScope()) |
| 189 | + { |
| 190 | + return function(accessor.GetLifetimeScope(null), ct); |
| 191 | + } |
| 192 | + }, ct, options, scheduler); |
| 193 | + |
| 194 | + _host.Register(t, ct); |
| 195 | + |
| 196 | + return t; |
| 197 | + } |
| 198 | + |
| 199 | + public static Task<TResult> Run<TResult>(Func<ILifetimeScope, CancellationToken, object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler) |
| 200 | + { |
| 201 | + Guard.ArgumentNotNull(() => state); |
| 202 | + Guard.ArgumentNotNull(() => function); |
| 203 | + Guard.ArgumentNotNull(() => scheduler); |
| 204 | + |
| 205 | + var ct = _host.CreateCompositeCancellationTokenSource(cancellationToken).Token; |
| 206 | + options |= TaskCreationOptions.LongRunning; // enforce an exclusive thread (not from pool) |
| 207 | + |
124 | 208 | var t = Task.Factory.StartNew((o) => |
125 | 209 | { |
126 | | - using (var container = EngineContext.Current.ContainerManager.Container.BeginLifetimeScope(AutofacLifetimeScopeProvider.HttpRequestTag)) |
| 210 | + var accessor = EngineContext.Current.ContainerManager.ScopeAccessor; |
| 211 | + using (var scope = accessor.BeginContextAwareScope()) |
127 | 212 | { |
128 | | - action(container, o); |
| 213 | + return function(accessor.GetLifetimeScope(null), ct, o); |
129 | 214 | } |
130 | | - }, state, cancellationToken, options, scheduler); |
| 215 | + }, state, ct, options, scheduler); |
| 216 | + |
| 217 | + _host.Register(t, ct); |
131 | 218 |
|
132 | 219 | return t; |
133 | 220 | } |
@@ -193,4 +280,80 @@ public override SynchronizationContext CreateCopy() |
193 | 280 |
|
194 | 281 | } |
195 | 282 |
|
| 283 | + internal class BackgroundWorkHost : IRegisteredObject |
| 284 | + { |
| 285 | + private CancellationTokenSource _shutdownCancellationTokenSource = new CancellationTokenSource(); |
| 286 | + private int _numRunningWorkItems = 0; |
| 287 | + |
| 288 | + public BackgroundWorkHost() |
| 289 | + { |
| 290 | + HostingEnvironment.RegisterObject(this); |
| 291 | + } |
| 292 | + |
| 293 | + public void Stop(bool immediate) |
| 294 | + { |
| 295 | + int num; |
| 296 | + lock (this) |
| 297 | + { |
| 298 | + _shutdownCancellationTokenSource.Cancel(); |
| 299 | + num = _numRunningWorkItems; |
| 300 | + } |
| 301 | + if (num == 0) |
| 302 | + { |
| 303 | + FinalShutdown(); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + public CancellationTokenSource CreateCompositeCancellationTokenSource(CancellationToken userCancellationToken) |
| 308 | + { |
| 309 | + if (userCancellationToken == CancellationToken.None) |
| 310 | + { |
| 311 | + return _shutdownCancellationTokenSource; |
| 312 | + } |
| 313 | + return CancellationTokenSource.CreateLinkedTokenSource(_shutdownCancellationTokenSource.Token, userCancellationToken); |
| 314 | + } |
| 315 | + |
| 316 | + public void Register(Task work, CancellationToken cancellationToken) |
| 317 | + { |
| 318 | + if (!cancellationToken.IsCancellationRequested) |
| 319 | + { |
| 320 | + lock (this) |
| 321 | + { |
| 322 | + if (cancellationToken.IsCancellationRequested) |
| 323 | + { |
| 324 | + return; |
| 325 | + } |
| 326 | + _numRunningWorkItems++; |
| 327 | + } |
| 328 | + |
| 329 | + work.ContinueWith( |
| 330 | + WorkItemComplete, |
| 331 | + CancellationToken.None, |
| 332 | + TaskContinuationOptions.ExecuteSynchronously, |
| 333 | + TaskScheduler.Default); |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + private void WorkItemComplete(Task work) |
| 338 | + { |
| 339 | + int num; |
| 340 | + bool isCancellationRequested; |
| 341 | + lock (this) |
| 342 | + { |
| 343 | + num = --_numRunningWorkItems; |
| 344 | + isCancellationRequested = _shutdownCancellationTokenSource.IsCancellationRequested; |
| 345 | + } |
| 346 | + if (num == 0 && isCancellationRequested) |
| 347 | + { |
| 348 | + FinalShutdown(); |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + private void FinalShutdown() |
| 353 | + { |
| 354 | + HostingEnvironment.UnregisterObject(this); |
| 355 | + } |
| 356 | + |
| 357 | + } |
| 358 | + |
196 | 359 | } |
0 commit comments