Example #1
0
        // 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));
        }