public SagaJsonMapperTests()
 {
     var connectionString = Environment.GetEnvironmentVariable("AzureStorageQueueTransport.ConnectionString");
     var account = CloudStorageAccount.Parse(connectionString);
     var cloudTableClient = account.CreateCloudTableClient();
     var tableName = typeof(SagaDownloaderTestState).Name;
     cloudTable = cloudTableClient.GetTableReference(tableName);
     cloudTable.CreateIfNotExists();
     mapper = new SagaJsonMapper(connectionString, tableName);
 }
Exemplo n.º 2
0
        private void UploadResolvedConflicts(string sagaTypeName)
        {
            var sagaDirectory = GetSagaDirectory(sagaTypeName);
            var cloudTable = GetTable(sagaTypeName);
            if (cloudTable.Exists() == false)
            {
                return;
            }

            Console.WriteLine();
            var mapper = new SagaJsonMapper(connectionString, sagaTypeName);

            foreach (var duplicatedSagaToResolve in new DirectoryInfo(sagaDirectory).GetDirectories())
            {
                var sagaInstances = duplicatedSagaToResolve.GetFiles();

                Console.WriteLine();
                Console.WriteLine("Resolving conflict between following sagas:");

                var anySuccess = false;
                var toDelete = new List<Guid>();

                foreach (var instance in sagaInstances)
                {
                    using (var reader = new JsonTextReader(new StreamReader(instance.OpenRead())))
                    {
                        var id = Guid.Parse(instance.Name);

                        var status = mapper.TryUpload(id, reader);
                        if (status == SagaJsonMapper.UploadStatus.Selected)
                        {
                            anySuccess = true;
                        }
                        else
                        {
                            toDelete.Add(id);
                        }
                        Console.WriteLine(" * {0} {1}", instance.Name, status);
                    }
                }

                Console.WriteLine();
                if (anySuccess)
                {
                    Console.WriteLine(" Resolving successful. Deleting following duplicates:");
                    foreach (var id in toDelete)
                    {
                        var status = (HttpStatusCode) cloudTable.Execute(TableOperation.Delete(new TableEntity(id.ToString(), id.ToString())
                        {
                            ETag = "*"
                        })).HttpStatusCode;
                        if (status == HttpStatusCode.OK || status == HttpStatusCode.NoContent)
                        {
                            Console.WriteLine(" * {0}", id);
                        }
                    }
                }
                else
                {
                    Console.WriteLine(" No conflicting saga marked with $Choose_this_saga: true");
                }
            }
        }
Exemplo n.º 3
0
        private void DownloadConflictingSagas(string sagaTypeName, string propertyName)
        {
            var sagaDirectory = GetSagaDirectory(sagaTypeName);
            Console.WriteLine();
            if (EnsureEmptyDirectoryExists(sagaDirectory) == false)
            {
                Console.WriteLine("Directory '{0}' must be empty", sagaTypeName);
                return;
            }

            var cloudTable = GetTable(sagaTypeName);
            if (cloudTable.Exists() == false)
            {
                Console.WriteLine("Table for saga '{0}' does not exist", sagaDirectory);
                return;
            }

            var mapper = new SagaJsonMapper(connectionString, sagaTypeName);
            var sagaIndexer = SagaIndexer.Get(connectionString, sagaTypeName, propertyName);

            Console.WriteLine($"Dowloading duplicates of saga '{sagaTypeName}' to '{sagaDirectory}'.");
            sagaIndexer.SearchForDuplicates((collidingPropertyValueHash, collidingSagas) =>
            {
                var alreadyDownloaded = new HashSet<Guid>();
                var workDir = Path.Combine(sagaDirectory, collidingPropertyValueHash.ToString());
                if (Directory.Exists(workDir) == false)
                {
                    Directory.CreateDirectory(workDir);
                }
                else
                {
                    var files = Directory.EnumerateFiles(workDir).Select(filePath => filePath.Substring(filePath.LastIndexOf("\\", StringComparison.Ordinal) + 1)).Select(Guid.Parse);

                    foreach (var sagaId in files)
                    {
                        alreadyDownloaded.Add(sagaId);
                    }
                }

                foreach (var sagaId in collidingSagas.ToArray().Except(alreadyDownloaded))
                {
                    using (var file = File.OpenWrite(Path.Combine(workDir, sagaId.ToString())))
                    {
                        using (var sw = new StreamWriter(file))
                        {
                            Console.WriteLine($"* Downloading saga {sagaId}");
                            mapper.Download(sagaId, sw);
                        }
                    }
                }
            });
        }