// TODO: Why this method is not async? private Task <bool> RunIndexJobAsync(string currentUserName, string notificationId, bool suppressInsignificantNotifications, IEnumerable <IndexingOptions> allOptions, Func <IndexingOptions, ICancellationToken, Task> indexationFunc, IJobCancellationToken cancellationToken) { var success = false; // Reset progress handler to initial state _progressHandler.Start(currentUserName, notificationId, suppressInsignificantNotifications); // Make sure only one indexation job can run in the cluster. // CAUTION: locking mechanism assumes single threaded execution. try { using (JobStorage.Current.GetConnection().AcquireDistributedLock("IndexationJob", TimeSpan.Zero)) { // Begin indexation try { foreach (var options in allOptions) { indexationFunc(options, new JobCancellationTokenWrapper(cancellationToken)).Wait(); } success = true; } catch (OperationCanceledException) { _progressHandler.Cancel(); } catch (Exception ex) { _progressHandler.Exception(ex); } } } catch { // TODO: Check wait in calling method _progressHandler.AlreadyInProgress(); } return(Task.FromResult(success)); }
private async Task IndexAsync(string currentUserName, string notificationId, bool suppressInsignificantNotifications, IEnumerable <IndexingOptions> allOptions, Func <IndexingOptions, CancellationToken, Task> indexationFunc) { // Reset progress handler to initial state _progressHandler.Start(currentUserName, notificationId, suppressInsignificantNotifications); // Capture the syncronization object (cancelation token source) if it is not captured already. var cancellationTokenSource = StartIndexation(); if (cancellationTokenSource == null) { // Report error and exit _progressHandler.AlreadyInProgress(); } else { // Begin indexation try { foreach (var options in allOptions) { await indexationFunc(options, cancellationTokenSource.Token); } } catch (OperationCanceledException) { _progressHandler.Cancel(); } catch (Exception ex) { _progressHandler.Exception(ex); } finally { // Report indexation summary _progressHandler.Finish(); // Release the syncronization object FinishIndexation(); } } }