Example #1
0
        private async Task GetTaskConfigurationsAsync <T>(OngoingTaskType type, Func <BlittableJsonReaderObject, T> converter)
            where T : IDynamicJsonValueConvertible
        {
            var taskName = GetStringQueryString("name", required: false);

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (context.OpenReadTransaction())
                    await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        var blittables = ServerStore.Cluster.GetServerWideConfigurations(context, type, taskName);
                        var result     = new ServerWideTasksResult <T>();

                        foreach (var blittable in blittables)
                        {
                            var configuration = converter(blittable);
                            result.Results.Add(configuration);
                        }

                        context.Write(writer, result.ToJson());
                    }
        }
        public Task GetServerWideTasksForStudio()
        {
            var taskName     = GetStringQueryString("name", required: false);
            var typeAsString = GetStringQueryString("type", required: false);
            var tryParse     = Enum.TryParse(typeAsString, out OngoingTaskType type);

            if (typeAsString != null && tryParse == false)
            {
                throw new ArgumentException($"{typeAsString} is unknown task type.");
            }

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (context.OpenReadTransaction())
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        var result = new ServerWideTasksResult();

                        var blittables = ServerStore.Cluster.GetServerWideConfigurations(context, OngoingTaskType.Backup, taskName);
                        foreach (var blittable in blittables)
                        {
                            var configuration = JsonDeserializationCluster.ServerWideBackupConfiguration(blittable);

                            if (taskName != null && type == OngoingTaskType.Backup &&
                                string.Equals(taskName, configuration.Name) == false)
                            {
                                continue;
                            }

                            result.Tasks.Add(new ServerWideTasksResult.ServerWideBackupTask
                            {
                                TaskId             = configuration.TaskId,
                                TaskName           = configuration.Name,
                                TaskState          = configuration.Disabled ? OngoingTaskState.Disabled : OngoingTaskState.Enabled,
                                ExcludedDatabases  = configuration.ExcludedDatabases,
                                BackupType         = configuration.BackupType,
                                RetentionPolicy    = configuration.RetentionPolicy,
                                BackupDestinations = configuration.GetDestinations(),
                                IsEncrypted        = configuration.BackupEncryptionSettings != null &&
                                                     configuration.BackupEncryptionSettings.EncryptionMode != EncryptionMode.None
                            });
                        }

                        blittables = ServerStore.Cluster.GetServerWideConfigurations(context, OngoingTaskType.Replication, taskName);
                        foreach (var blittable in blittables)
                        {
                            var configuration = JsonDeserializationCluster.ServerWideExternalReplication(blittable);

                            if (taskName != null && type == OngoingTaskType.Replication &&
                                string.Equals(taskName, configuration.Name) == false)
                            {
                                continue;
                            }

                            result.Tasks.Add(new ServerWideTasksResult.ServerWideExternalReplicationTask
                            {
                                TaskId                = configuration.TaskId,
                                TaskName              = configuration.Name,
                                TaskState             = configuration.Disabled ? OngoingTaskState.Disabled : OngoingTaskState.Enabled,
                                ExcludedDatabases     = configuration.ExcludedDatabases,
                                TopologyDiscoveryUrls = configuration.TopologyDiscoveryUrls,
                                DelayReplicationFor   = configuration.DelayReplicationFor,
                            });
                        }

                        context.Write(writer, result.ToJson());
                        writer.Flush();

                        return(Task.CompletedTask);
                    }
        }