Exemplo n.º 1
0
        public Task <HttpResponseMessage> ExportFilesystem(StudioTasksController.ExportData smugglerOptionsJson)
        {
            var requestString = smugglerOptionsJson.SmugglerOptions;
            SmugglerFilesOptions smugglerOptions;

            using (var jsonReader = new RavenJsonTextReader(new StringReader(requestString)))
            {
                var serializer = JsonExtensions.CreateDefaultJsonSerializer();
                smugglerOptions = (SmugglerFilesOptions)serializer.Deserialize(jsonReader, typeof(SmugglerFilesOptions));
            }


            var result = GetEmptyMessage();

            // create PushStreamContent object that will be called when the output stream will be ready.
            result.Content = new PushStreamContent(async(outputStream, content, arg3) =>
            {
                try
                {
                    var dataDumper = new FilesystemDataDumper(FileSystem, smugglerOptions);
                    await dataDumper.ExportData(
                        new SmugglerExportOptions <FilesConnectionStringOptions>
                    {
                        ToStream = outputStream
                    }).ConfigureAwait(false);
                }
                finally
                {
                    outputStream.Close();
                }
            });

            var fileName = String.IsNullOrEmpty(smugglerOptions.NoneDefualtFileName) || (smugglerOptions.NoneDefualtFileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) ?
                           string.Format("Dump of {0}, {1}", this.FileSystemName, DateTime.Now.ToString("yyyy-MM-dd HH-mm", CultureInfo.InvariantCulture)) :
                           smugglerOptions.NoneDefualtFileName;

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName + ".ravendump"
            };

            return(new CompletedTask <HttpResponseMessage>(result));
        }
Exemplo n.º 2
0
        public Task <HttpResponseMessage> ExportFilesystem(StudioTasksController.ExportData exportData)
        {
            var requestString = exportData.SmugglerOptions;
            FileSystemSmugglerOptions options;

            using (var jsonReader = new RavenJsonTextReader(new StringReader(requestString)))
            {
                var serializer = JsonExtensions.CreateDefaultJsonSerializer();
                options = (FileSystemSmugglerOptions)serializer.Deserialize(jsonReader, typeof(FileSystemSmugglerOptions));
            }


            var result = GetEmptyMessage();

            // create PushStreamContent object that will be called when the output stream will be ready.
            result.Content = new PushStreamContent((outputStream, content, arg3) =>
            {
                try
                {
                    var smuggler = new FileSystemSmuggler(options);

                    return(smuggler.ExecuteAsync(new EmbeddedSmugglingSource(FileSystem), new StreamSmugglingDestination(outputStream, leaveOpen: true)));
                }
                finally
                {
                    outputStream.Close();
                }
            });

            var fileName = string.IsNullOrEmpty(exportData.FileName) || (exportData.FileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) ?
                           string.Format("Dump of {0}, {1}", FileSystemName, DateTime.Now.ToString("yyyy-MM-dd HH-mm", CultureInfo.InvariantCulture)) :
                           exportData.FileName;

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName + ".ravendump"
            };

            return(new CompletedTask <HttpResponseMessage>(result));
        }
Exemplo n.º 3
0
        public Task <HttpResponseMessage> ExportDocumentsLeftToReplicate([FromBody] StudioTasksController.ExportData optionsJson)
        {
            var result = GetEmptyMessage();
            var taskId = optionsJson.ProgressTaskId;
            var status = new OperationStatus();

            var tcs = new TaskCompletionSource <object>();

            try
            {
                var sp = Stopwatch.StartNew();

                Database.Tasks.AddTask(tcs.Task, status, new TaskActions.PendingTaskDescription
                {
                    StartTime   = SystemTime.UtcNow,
                    TaskType    = TaskActions.PendingTaskType.ExportDocumentsLeftToReplicate,
                    Description = "Monitoring progress of export of docs left to replicate"
                }, taskId, null, skipStatusCheck: true);

                var        requestString = optionsJson.DownloadOptions;
                ServerInfo serverInfo;

                using (var jsonReader = new RavenJsonTextReader(new StringReader(requestString)))
                {
                    var serializer = JsonExtensions.CreateDefaultJsonSerializer();
                    serverInfo = (ServerInfo)serializer.Deserialize(jsonReader, typeof(ServerInfo));
                }

                var documentsToReplicateCalculator = new DocumentsLeftToReplicate(Database);

                if (serverInfo.SourceId != documentsToReplicateCalculator.DatabaseId)
                {
                    throw new InvalidOperationException("Cannot export documents to replicate from a server other than this one!");
                }

                // create PushStreamContent object that will be called when the output stream will be ready.
                result.Content = new PushStreamContent((outputStream, content, arg3) =>
                {
                    using (var writer = new ExcelOutputWriter(outputStream))
                    {
                        try
                        {
                            writer.WriteHeader();
                            writer.Write("document-ids-left-to-replicate");

                            long count             = 0;
                            long skipped           = 0;
                            Action <string> action = (documentId) =>
                            {
                                writer.Write(documentId);

                                if (++count % 1000 != 0)
                                {
                                    return;
                                }

                                status.MarkProgress($"Exported {count:#,#} documents");
                                outputStream.Flush();
                            };

                            Action skippedAction = () =>
                            {
                                if (++skipped % 100 != 0)
                                {
                                    return;
                                }

                                status.MarkProgress($"Skipped {skipped:#,#} documents");
                                outputStream.Flush();
                            };

                            documentsToReplicateCalculator.ExtractDocumentIds(serverInfo, action, skippedAction);

                            var message = $"Completed export of {count:#,#} document ids";
                            status.MarkCompleted(message, sp.Elapsed);
                        }
                        catch (Exception e)
                        {
                            status.ExceptionDetails = e.ToString();
                            status.MarkFaulted(e.ToString());
                            writer.WriteError(e);
                            throw;
                        }
                        finally
                        {
                            tcs.SetResult("Completed");
                            outputStream.Close();
                        }
                    }
                });

                var fileName = $"Documents to replicate from '{serverInfo.SourceUrl}' to '{serverInfo.DestinationUrl}', " +
                               $"{DateTime.Now.ToString("yyyy-MM-dd HH-mm", CultureInfo.InvariantCulture)}";

                foreach (char c in Path.GetInvalidFileNameChars())
                {
                    fileName = fileName.Replace(c, '_');
                }

                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName + ".csv"
                };
            }
            catch (Exception e)
            {
                status.ExceptionDetails = e.ToString();
                status.MarkFaulted(e.ToString());
                tcs.SetResult("Completed");
                throw;
            }

            return(new CompletedTask <HttpResponseMessage>(result));
        }
Exemplo n.º 4
0
        public Task <HttpResponseMessage> ExportFilesystem(StudioTasksController.ExportData smugglerOptionsJson)
        {
            var result = GetEmptyMessage();

            var taskId        = smugglerOptionsJson.ProgressTaskId;
            var requestString = smugglerOptionsJson.DownloadOptions;
            SmugglerFilesOptions smugglerOptions;

            using (var jsonReader = new RavenJsonTextReader(new StringReader(requestString)))
            {
                var serializer = JsonExtensions.CreateDefaultJsonSerializer();
                smugglerOptions = (SmugglerFilesOptions)serializer.Deserialize(jsonReader, typeof(SmugglerFilesOptions));
            }

            var fileName = string.IsNullOrEmpty(smugglerOptions.NoneDefualtFileName) || (smugglerOptions.NoneDefualtFileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) ?
                           $"Dump of {FileSystemName}, {DateTime.Now.ToString("yyyy-MM-dd HH-mm", CultureInfo.InvariantCulture)}" :
                           smugglerOptions.NoneDefualtFileName;

            //create PushStreamContent object that will be called when the output stream will be ready.
            result.Content = new PushStreamContent(async(outputStream, content, arg3) =>
            {
                var status = new DataDumperOperationStatus();
                var tcs    = new TaskCompletionSource <object>();
                var sp     = Stopwatch.StartNew();

                try
                {
                    FileSystem.Tasks.AddTask(tcs.Task, status, new TaskActions.PendingTaskDescription
                    {
                        StartTime = SystemTime.UtcNow,
                        TaskType  = TaskActions.PendingTaskType.ExportFileSystem,
                        Payload   = "Exporting file system, file name: " + fileName
                    }, taskId, smugglerOptions.CancelToken, skipStatusCheck: true);

                    var dataDumper       = new FilesystemDataDumper(FileSystem, smugglerOptions);
                    dataDumper.Progress += s => status.MarkProgress(s);
                    await dataDumper.ExportData(
                        new SmugglerExportOptions <FilesConnectionStringOptions>
                    {
                        ToStream = outputStream
                    }).ConfigureAwait(false);

                    const string message = "Completed export";
                    status.MarkCompleted(message, sp.Elapsed);
                }
                catch (OperationCanceledException e)
                {
                    status.MarkCanceled(e.Message);
                }
                catch (Exception e)
                {
                    status.ExceptionDetails = e.ToString();
                    status.MarkFaulted(e.ToString());

                    throw;
                }
                finally
                {
                    tcs.SetResult("Completed");
                    outputStream.Close();
                }
            });

            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = fileName + ".ravenfsdump"
            };

            return(new CompletedTask <HttpResponseMessage>(result));
        }