private HttpResponseMessage OnBulkOperation(Func <string, IndexQuery, BulkOperationOptions, Action <BulkOperationProgress>, RavenJArray> batchOperation, string index, CancellationTimeout timeout) { if (string.IsNullOrEmpty(index)) { return(GetEmptyMessage(HttpStatusCode.BadRequest)); } var option = new BulkOperationOptions { AllowStale = GetAllowStale(), MaxOpsPerSec = GetMaxOpsPerSec(), StaleTimeout = GetStaleTimeout(), RetrieveDetails = GetRetrieveDetails() }; var indexQuery = GetIndexQuery(maxPageSize: int.MaxValue); var status = new BulkOperationStatus(); long id; var task = Task.Factory.StartNew(() => { using (DocumentCacher.SkipSetDocumentsInDocumentCache()) { status.State["Batch"] = batchOperation(index, indexQuery, option, x => { status.MarkProgress(x); }); } }).ContinueWith(t => { if (timeout != null) { timeout.Dispose(); } if (t.IsFaulted == false) { status.MarkCompleted($"Processed {status.OperationProgress.ProcessedEntries} items"); return; } var exception = t.Exception.ExtractSingleInnerException(); status.MarkFaulted(exception.Message); }); Database.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription { StartTime = SystemTime.UtcNow, TaskType = TaskActions.PendingTaskType.IndexBulkOperation, Description = index }, out id, timeout.CancellationTokenSource); return(GetMessageWithObject(new { OperationId = id }, HttpStatusCode.Accepted)); }
private HttpResponseMessage OnBulkOperation(Func <string, IndexQuery, BulkOperationOptions, RavenJArray> batchOperation, string index, CancellationTimeout timeout) { if (string.IsNullOrEmpty(index)) { return(GetEmptyMessage(HttpStatusCode.BadRequest)); } var option = new BulkOperationOptions { AllowStale = GetAllowStale(), MaxOpsPerSec = GetMaxOpsPerSec(), StaleTimeout = GetStaleTimeout(), RetrieveDetails = GetRetrieveDetails() }; var indexQuery = GetIndexQuery(maxPageSize: int.MaxValue); var status = new BulkOperationStatus(); long id; var task = Task.Factory.StartNew(() => { status.State = batchOperation(index, indexQuery, option); }).ContinueWith(t => { if (timeout != null) { timeout.Dispose(); } if (t.IsFaulted == false) { status.Completed = true; return; } var exception = t.Exception.ExtractSingleInnerException(); status.State = RavenJObject.FromObject(new { Error = exception.Message }); status.Faulted = true; status.Completed = true; }); Database.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription { StartTime = SystemTime.UtcNow, TaskType = TaskActions.PendingTaskType.IndexBulkOperation, Payload = index }, out id, timeout.CancellationTokenSource); return(GetMessageWithObject(new { OperationId = id })); }