/// <summary>
        /// Get the list of children Items for the Parent.
        /// </summary>
        /// <param name="parentID"></param>
        private static void GetListItems(string parentID)
        {
            //GET the RepositoryInfo
            var configuration = GetClientConfig();
            var instance      = new RepositoryApi(configuration);
            var response      = instance.ApiV1RepositoryInfoGet();
            //GET the Sets
            var  instanceSets = new SetApi(configuration);
            Guid guid         = new Guid(parentID);
            var  responseSets = instanceSets.ApiV1SetByAgencyByIdByVersionGet("int.example", guid, 1);

            //GET the list of GUID
            GetLatestItemsRequest request = new GetLatestItemsRequest
            {
                Identifiers = new List <IdentifierInRequest>()
            };

            foreach (IdentifierTriple itemTriple in responseSets)
            {
                request.Identifiers.Add(new IdentifierInRequest(itemTriple.Item3, itemTriple.Item1, itemTriple.Item2));
            }
            var instanceList = new ItemApi(configuration);
            var responseList = instanceList.ApiV1ItemGetListLatestPost(request);

            foreach (RepositoryItem item in responseList)
            {
                if (item != null)
                {
                    Console.WriteLine(item.ToJson());
                }
            }
        }
Example #2
0
        public void OnCreateWithoutBasePath()
        {
            RepositoryApi repositoryApi = new RepositoryApi();

            Assert.NotNull(repositoryApi);
            Assert.NotNull(repositoryApi.Configuration);
        }
Example #3
0
        private static DeploymentServerState CreateState(IActorRefFactory context, ISharpRepositoryConfiguration configuration, IDirectory fileSystem, DataTransferManager manager,
                                                         RepositoryApi repositoryApi)
        {
            var changeTracker = context.ActorOf("Change-Tracker", ChangeTrackerActor.New());

            var trashBin = configuration.GetInstance <ToDeleteRevision, string>(CleanUpManager.RepositoryKey);

            var cleanUp = context.ActorOf("CleanUp-Manager", CleanUpManager.New(configuration, fileSystem));

            cleanUp.Tell(CleanUpManager.Initialization);

            var router = new SmallestMailboxPool(Environment.ProcessorCount)
                         .WithSupervisorStrategy(SupervisorStrategy.DefaultStrategy);

            var queryProps = Feature.Props(AppQueryHandler.New(configuration.GetInstance <AppData, string>(), fileSystem, manager, changeTracker))
                             .WithRouter(router);
            var query = context.ActorOf(queryProps, "QueryRouter");

            var buildSystem = WorkDistributorFeature <BuildRequest, BuildCompled>
                              .Create(context, Props.Create(() => new BuildingActor(manager)), "Compiler", TimeSpan.FromHours(1), "CompilerSupervisor");

            var processorProps = Feature.Props(AppCommandProcessor.New(configuration.GetInstance <AppData, string>(), fileSystem, repositoryApi, manager, trashBin, buildSystem, changeTracker))
                                 .WithRouter(router);
            var processor = context.ActorOf(processorProps, "ProcessorRouter");

            return(new DeploymentServerState(query, processor));
        }
Example #4
0
        public void ApiV1RepositoryStatisticsGetWithHttpInfo()
        {
            configuration = GetClientConfig();
            RepositoryApi repositoryApi = new RepositoryApi(configuration);
            ApiResponse <RepositoryStatistics> stats = repositoryApi.ApiV1RepositoryStatisticsGetWithHttpInfo();

            Assert.Equal(200, stats.StatusCode);
        }
Example #5
0
        public void AsyncApiV1RepositoryInfoGetWithHttpInfo()
        {
            configuration = GetClientConfig();
            RepositoryApi repositoryApi = new RepositoryApi(configuration);
            Task          task          = repositoryApi.ApiV1RepositoryInfoGetAsyncWithHttpInfo();

            Assert.NotNull(task.Id.ToString());
        }
Example #6
0
        public void ApiV1RepositoryStatisticsGet()
        {
            configuration = GetClientConfig();
            RepositoryApi        repositoryApi = new RepositoryApi(configuration);
            RepositoryStatistics stats         = repositoryApi.ApiV1RepositoryStatisticsGet();

            Assert.NotNull(stats);
        }
Example #7
0
        public void AsyncApiV1RepositoryStatisticsGet()
        {
            configuration = GetClientConfig();
            RepositoryApi repositoryApi = new RepositoryApi(configuration);
            Task          task          = repositoryApi.ApiV1RepositoryStatisticsGetAsync();

            Assert.NotNull(task.Id.ToString());
        }
Example #8
0
        public void ApiV1RepositoryInfoGetTest()
        {
            configuration = GetClientConfig();
            RepositoryApi  repositoryApi = new RepositoryApi(configuration);
            RepositoryInfo info          = repositoryApi.ApiV1RepositoryInfoGet();

            Assert.NotNull(info);
        }
Example #9
0
        public void ApiV1RepositoryInfoGetWithHttpInfo()
        {
            configuration = GetClientConfig();
            RepositoryApi repositoryApi       = new RepositoryApi(configuration);
            ApiResponse <RepositoryInfo> info = repositoryApi.ApiV1RepositoryInfoGetWithHttpInfo();

            Assert.NotNull(info);
            Assert.Equal(200, info.StatusCode);
        }
            public ServiceManager(AppConfig config)
            {
                _dataTransfer  = DataTransferManager.New(Context, "File_Coordination");
                _repositoryApi = RepositoryApi.CreateProxy(Context.System);

                Receive <Init>(_ =>
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(config.CurrentConfig))
                        {
                            return;
                        }

                        var hConfig          = ConfigurationFactory.ParseString(config.CurrentConfig);
                        var connectionString = hConfig.GetString("akka.persistence.journal.mongodb.connection-string");

                        if (string.IsNullOrWhiteSpace(connectionString))
                        {
                            return;
                        }

                        var stream = Context.System.EventStream;

                        Cluster.Get(Context.System).RegisterOnMemberUp(() =>
                        {
                            InitClient(connectionString);
                            stream.Publish(new DeploymentServicesChanged(_repository?.IsOk == true && _deploymentServer?.IsOk == true));
                        });
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e, "Error on Parsing Current Configuration");
                    }
                });
                Receive <NewConnectionString>(s =>
                {
                    try
                    {
                        var hConfig          = ConfigurationFactory.ParseString(s.Config);
                        var connectionString = hConfig.GetString("akka.persistence.journal.mongodb.connection-string");

                        InitClient(connectionString);
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, "Error on Init new Connection String");
                    }

                    Context.System.EventStream.Publish(new DeploymentServicesChanged(_repository?.IsOk == true && _deploymentServer?.IsOk == true));
                });
                Receive <GetCurrentState>(_ => Sender.Tell(new DeploymentServicesChanged(_repository?.IsOk == true && _deploymentServer?.IsOk == true)));
            }
Example #11
0
        static async Task Main(string[] args)
        {
            await Bootstrap.StartNode(args, KillRecpientType.Service)
            .ConfigurateAkkaSystem((context, system) =>
            {
                var repoManager = RepositoryApi.CreateProxy(system);
                var fileManager = DataTransferManager.New(system, "FileTransferManager");

                DeploymentManager.InitDeploymentManager(
                    system,
                    system.Settings.Config.GetString("akka.persistence.journal.mongodb.connection-string"), fileManager, repoManager);
            })
            .Build().Run();
        }
        public SetupBuilder(string hostName, string?seedHostName, AppConfig config, Action <string> log, ActorSystem actorSystem, DeploymentApi api, RepositoryApi repository, IActionInvoker actionInvoker)
        {
            _logger       = new LoggerConfiguration().WriteTo.Logger(Log.ForContext <SetupBuilder>()).WriteTo.Sink(new DelegateSink(s => _log !(s))).CreateLogger();
            _dataTransfer = DataTransferManager.New(actorSystem);

            _context = new RunContext
            {
                HostName     = hostName,
                SeedHostName = seedHostName
            };
            _config        = config;
            _log           = log;
            _api           = api;
            _repository    = repository;
            _actionInvoker = actionInvoker;
        }
        /// <summary>
        /// Get the list of children Items for the parentID for a specific agency
        /// and a specific type with a limit of objects returned.
        /// </summary>
        /// <param name="agency"></param>
        /// <param name="parentID"></param>
        /// <param name="itemType"></param>
        /// <param name="limitMax"></param>
        private static void GetListItems(string agency, string parentID, string itemType, long limitMax)
        {
            //Converting string parameter to GUID
            Guid itemTypeGuid = new Guid(itemType);

            //GET the RepositoryInfo
            var configuration = GetClientConfig();
            var instance      = new RepositoryApi(configuration);
            var response      = instance.ApiV1RepositoryInfoGet();

            //GET the Sets
            var  instanceSets = new SetApi(configuration);
            Guid guid         = new Guid(parentID);
            var  responseSets = instanceSets.ApiV1SetByAgencyByIdByVersionGet("int.example", guid, 1);

            //GET the list of GUID
            GetLatestItemsRequest request = new GetLatestItemsRequest
            {
                Identifiers = new List <IdentifierInRequest>()
            };

            foreach (IdentifierTriple itemTriple in responseSets)
            {
                request.Identifiers.Add(new IdentifierInRequest(itemTriple.Item3, itemTriple.Item1, itemTriple.Item2));
            }
            var  instanceList = new ItemApi(configuration);
            var  responseList = instanceList.ApiV1ItemGetListLatestPost(request);
            long countdown    = 0L;

            foreach (RepositoryItem item in responseList)
            {
                if (countdown <= limitMax)
                {
                    if (item != null && item.ItemType == itemTypeGuid && item.AgencyId == agency)
                    {
                        Console.WriteLine(item.ToJson());
                    }
                }
                countdown++;
            }
        }
        static void Main(string[] args)
        {
            ////Using test Guid : You have to replace them by yours.
            //GetListItems("52c5dd34-1b5f-460b-8904-6f0f2897f6a1");
            //Console.WriteLine("/////////////////////////////////////////////////////////");
            //Console.WriteLine("/////////////////////////////////////////////////////////");
            //Console.WriteLine("/////////////////////////////////////////////////////////" + "\n");
            //GetListItems("int.example", "52c5dd34-1b5f-460b-8904-6f0f2897f6a1", "a1bb19bd-a24a-4443-8728-a6ad80eb42b8");
            //Console.WriteLine("/////////////////////////////////////////////////////////");
            //Console.WriteLine("/////////////////////////////////////////////////////////");
            //Console.WriteLine("/////////////////////////////////////////////////////////" + "\n");
            //GetListItems("int.example", "52c5dd34-1b5f-460b-8904-6f0f2897f6a1", "a1bb19bd-a24a-4443-8728-a6ad80eb42b8", 2L);
            var client = new ApiClient("https://quill.colectica.org");
            var config = new Configuration(client);

            config.DefaultHeader.Add("api_key", "QUILLTEST");

            var            api      = new RepositoryApi(config);
            RepositoryInfo response = api.ApiV1RepositoryInfoGet();

            Console.WriteLine(response.ToJson());
            Console.ReadLine();
        }
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterSettingsManager(c => c.WithProvider <LocalConfigurationProvider>());
            builder.RegisterType <LocalConfiguration>().As <ILocalConfiguration>().SingleInstance();

            builder.Register(c => c.Resolve <IEventAggregator>().GetEvent <ConfigEventDispatcher, IConfigEvent>()).SingleInstance();

            builder.RegisterType <ClusterConnectionTracker>().As <IClusterConnectionTracker>().SingleInstance();
            builder.RegisterType <DatabaseConfig>().As <IDatabaseConfig>().SingleInstance();
            builder.RegisterType <ServerConfigurationApi>().As <IServerConfigurationApi>().SingleInstance();
            builder.RegisterType <PropertyChangedNotifer>().As <IPropertyChangedNotifer>().SingleInstance();

            builder.RegisterFeature <ClusterNodeManagerRef, IClusterNodeManager>(ClusterHostManagerActor.New());
            builder.RegisterFeature <ApiEventDispatcherRef, IApiEventDispatcher>(ApiEventDispatcherActor.New());
            builder.RegisterFeature <ProcessServiceHostRef, IProcessServiceHost>(ProcessServiceHostActor.New());

            builder.Register(c => DeploymentApi.CreateProxy(c.Resolve <ActorSystem>())).SingleInstance();
            builder.Register(c => RepositoryApi.CreateProxy(c.Resolve <ActorSystem>())).SingleInstance();
            builder.Register(c => ConfigurationApi.CreateProxy(c.Resolve <ActorSystem>())).SingleInstance();

            builder.RegisterStartUpAction <ActorStartUp>();

            base.Load(builder);
        }
 static IPreparedFeature _(IHostEnvironment hostEnvironment, IDatabaseConfig config, RepositoryApi repositoryApi, DeploymentApi deploymentApi, ConfigurationApi configurationApi)
 => Feature.Create(() => new ProcessServiceHostActor(), _ => new _(config, new Services(), hostEnvironment, repositoryApi, deploymentApi, configurationApi));
        public static async Task Main(string[] args)
        {
            await Bootstrap.StartNode(args, KillRecpientType.Service, IpcApplicationType.Client,
                                      ab =>
            {
                ab.OnMemberUp((context, system, cluster) =>
                {
                    string ApplyMongoUrl(string baseUrl, string repoKey, SharpRepositoryConfiguration configuration)
                    {
                        var builder = new MongoUrlBuilder(baseUrl)
                        {
                            DatabaseName = repoKey, ApplicationName = context.HostingEnvironment.ApplicationName
                        };
                        var mongoUrl = builder.ToString();

                        configuration.AddRepository(new MongoDbRepositoryConfiguration(repoKey, mongoUrl)
                        {
                            Factory = typeof(MongoDbConfigRepositoryFactory)
                        });

                        return(mongoUrl);
                    }

                    ServiceRegistry.Get(system)
                    .RegisterService(new RegisterService(
                                         context.HostingEnvironment.ApplicationName,
                                         cluster.SelfUniqueAddress,
                                         ServiceTypes.Infrastructure));

                    var connectionstring = system.Settings.Config.GetString("akka.persistence.snapshot-store.mongodb.connection-string");
                    if (string.IsNullOrWhiteSpace(connectionstring))
                    {
                        LogManager.GetCurrentClassLogger().Error("No Mongo DB Connection provided: Shutdown");
                        system.Terminate();
                        return;
                    }

                    var config            = new SharpRepositoryConfiguration();
                    var fileSystemBuilder = new VirtualFileFactory();

                    ApplyMongoUrl(connectionstring, CleanUpManager.RepositoryKey, config);

                    var url = ApplyMongoUrl(connectionstring, RepositoryManager.RepositoryKey, config);
                    RepositoryManager.InitRepositoryManager(system,
                                                            new RepositoryManagerConfiguration(config, fileSystemBuilder.CreateMongoDb(url),
                                                                                               DataTransferManager.New(system, "Repository-DataTransfer")));

                    url = ApplyMongoUrl(connectionstring, DeploymentManager.RepositoryKey, config);
                    DeploymentManager.InitDeploymentManager(system,
                                                            new DeploymentConfiguration(config,
                                                                                        fileSystemBuilder.CreateMongoDb(url),
                                                                                        DataTransferManager.New(system, "Deployment-DataTransfer"),
                                                                                        RepositoryApi.CreateProxy(system)));

                    ApplyMongoUrl(connectionstring, ServiceManagerDeamon.RepositoryKey, config);
                    ServiceManagerDeamon.Init(system, config);
                })
                .OnMemberRemoved((_, system, _) => system.Terminate());
            }, true)
Example #18
0
 public static DeploymentManager CreateInstance(IActorRefFactory factory, string connectionString, DataTransferManager manager, RepositoryApi api)
 => new DeploymentManager(factory.ActorOf(Props.Create(() => new DeploymentServerImpl(new MongoClient(connectionString), manager, api)), DeploymentApi.DeploymentPath));
Example #19
0
 public static DeploymentManager InitDeploymentManager(ActorSystem actorSystem, string connectionString, DataTransferManager manager, RepositoryApi api)
 => InitDeploymentManager(actorSystem, new MongoClient(connectionString), manager, api);
        public DeploymentServerImpl(IMongoClient client, DataTransferManager dataTransfer, RepositoryApi repositoryProxy)
        {
            var changeTracker = Context.ActorOf <ChangeTrackerActor>();

            var database = client.GetDatabase("Deployment");
            var trashBin = database.GetCollection <ToDeleteRevision>("TrashBin");
            var files    = new GridFSBucket(database, new GridFSBucketOptions {
                BucketName = "Apps"
            });

            var cleanUp = Context.ActorOf(() => new CleanUpManager(database, "CleanUp", trashBin, files), "CleanUp-Manager");

            cleanUp.Tell(CleanUpManager.Initialization);

            var router = new SmallestMailboxPool(Environment.ProcessorCount)
                         .WithSupervisorStrategy(Akka.Actor.SupervisorStrategy.DefaultStrategy);

            var queryProps = Props.Create(() => new AppQueryHandler(database.GetCollection <AppData>(AppsCollectionName, null), files, dataTransfer, changeTracker))
                             .WithRouter(router);
            var query = Context.ActorOf(queryProps, "QueryRouter");

            Receive <IDeploymentQuery>(q => query.Forward(q));

            var buildSystem = WorkDistributor <BuildRequest, BuildCompled> .Create(Context, Props.Create(() => new BuildingActor(dataTransfer)), "Compiler", TimeSpan.FromHours(1), "CompilerSupervisor");

            var processorProps = Props.Create(() => new AppCommandProcessor(database.GetCollection <AppData>(AppsCollectionName, null), files, repositoryProxy, dataTransfer, trashBin, buildSystem, changeTracker))
                                 .WithRouter(router);
            var processor = Context.ActorOf(processorProps, "ProcessorRouter");

            Receive <IDeploymentCommand>(a => processor.Forward(a));

            Receive <string>(s =>
            {
                try
                {
                    database.GetCollection <AppData>(AppsCollectionName).Indexes.CreateOne(new CreateIndexModel <AppData>(Builders <AppData> .IndexKeys.Ascending(ad => ad.Name)));
                }
                catch (Exception e)
                {
                    Log.Error(e, "Error on Creating Index for Data");
                }
            });
        }
Example #21
0
        public static DeploymentManager InitDeploymentManager(ActorSystem actorSystem, IMongoClient client, DataTransferManager manager, RepositoryApi api)
        {
            var repo = ClusterSingletonManager.Props(Props.Create(() => new DeploymentServerImpl(client, manager, api)),
                                                     ClusterSingletonManagerSettings.Create(actorSystem).WithRole("UpdateSystem"));

            return(new DeploymentManager(actorSystem.ActorOf(repo, DeploymentApi.DeploymentPath)));
        }
Example #22
0
        public void OnCreate()
        {
            RepositoryApi repositoryApi = new RepositoryApi("http://localhost:5000");

            Assert.NotNull(repositoryApi);
        }
 public sealed record _(IDatabaseConfig DatabaseConfig, Services Operator, IHostEnvironment HostEnvironment, RepositoryApi RepositoryApi, DeploymentApi DeploymentApi, ConfigurationApi ConfigurationApi);
Example #24
0
        private void XuTest1_Click(object sender, EventArgs e)
        {
            XuTest1.Enabled = false;

            var target = new DirectoryInfo(XuTargetFolder.Text);
            var source = new DirectoryInfo(XuSourceFolder.Text);
            //var right = new DirectoryInfo(@"C:\DevFrontend\M_Frontend-Master\bin");

            var api = new RepositoryApi { TargetDirectory = target, SourceDirectory = source };
            var filecount = api.Analyze();
            var x = api.FileList;

            var z1 = x;
            var z2 = filecount;

            //var x3 = x.Select(t => t.FileVersionTarget.CompareTo(t.FileVersionSource) != 0).ToList();

            UnequalFileList = new List<FileMetadata>();

            foreach (var current in x)
            {
                var compare = current.FileVersionTarget.CompareTo(current.FileVersionSource);

                if (compare < 0 && XuShowNewer.Checked)
                {
                    UnequalFileList.Add(current);
                }
                else if (compare > 0 && XuShowOlder.Checked)
                {
                    UnequalFileList.Add(current);
                }
                else if (compare == 0 && XuShowEqual.Checked)
                {
                    UnequalFileList.Add(current);
                }
            }

            //UnequalFileList = x.Where(metadata => metadata.FileVersionTarget.CompareTo(metadata.FileVersionSource) != 0).ToList();

            Table = new List<Row>();

            foreach (var fileMetadata in UnequalFileList)
            {
                var t1 = fileMetadata.FileVersionTarget.CompareTo(fileMetadata.FileVersionSource);

                var direction = Direction.Identical;

                if (t1 > 0)
                {
                    direction = Direction.Right;
                }
                else if (t1 < 0)
                {
                    direction = Direction.Left;
                }

                Table.Add(
                    new Row
                    {
                        Direction = MapDirection(direction),
                        Filename = fileMetadata.Filename,
                        TargetVersion = fileMetadata.FileVersionTarget.ToString(),
                        SourceVersion = fileMetadata.FileVersionSource.ToString()
                    }
                );

            }

            //XuTable.BeginEdit(true);
            XuTable.DataSource = Table;
            //XuTable.EndEdit();
            Update();

            XuTest1.Enabled = true;
        }
Example #25
0
 public static IPreparedFeature New(ISharpRepositoryConfiguration configuration, IDirectory fileSystem, DataTransferManager manager, RepositoryApi repositoryApi)
 => Feature.Create(() => new DeploymentServerImpl(), c => CreateState(c, configuration, fileSystem, manager, repositoryApi));
        //TODO Refactor for StateManager

        public SetupBuilderViewModel(ILifetimeScope lifetimeScope, Dispatcher dispatcher, AppConfig config, DeploymentServices deploymentServices, IActionInvoker actionInvoker)
            : base(lifetimeScope, dispatcher, actionInvoker)
        {
            _config        = config;
            _server        = new SetupServer(s => UICall(() => TerminalLines !.Add(s)), Context.System.Settings.Config);
            _deploymentApi = DeploymentApi.CreateProxy(Context.System, "SetupBuilder_DeploymentApi");
            _repositoryApi = RepositoryApi.CreateProxy(Context.System, "SetupBuilder_RepositoryApi");

            AddShortcut   = RegisterProperty <bool>(nameof(AddShortcut));
            CurrentError  = RegisterProperty <string>(nameof(CurrentError));
            AddSeed       = RegisterProperty <bool>(nameof(AddSeed));
            TerminalLines = this.RegisterUiCollection <string>(nameof(TerminalLines)).AndAsync();

            var hostEntrys = new HashSet <string>();

            _api = HostApi.CreateOrGet(Context.System);
            Receive <HostEntryChanged>(e =>
            {
                if (string.IsNullOrWhiteSpace(e.Name))
                {
                    return;
                }

                if (e.Removed)
                {
                    hostEntrys.Remove(e.Name);
                }
                else
                {
                    hostEntrys.Add(e.Name);
                }
            });

            Func <string, string?> HostNameValidator(Func <UIProperty <string> > counterPart)
            {
                return(s =>
                {
                    if (string.IsNullOrWhiteSpace(s))
                    {
                        return LocLocalizer.Inst.SetupBuilderView.ErrorEmptyHostName;
                    }
                    return hostEntrys.Contains(s) || s == counterPart().Value ? LocLocalizer.Inst.SetupBuilderView.ErrorDuplicateHostName : null;
                });
            }

            HostName = RegisterProperty <string>(nameof(HostName))
                       .WithValidator(SetError(HostNameValidator(() => SeedHostName !)))
                       .OnChange(s => SeedHostName += s + "_Seed");

            SeedHostName = RegisterProperty <string>(nameof(SeedHostName))
                           .WithValidator(SetError(HostNameValidator(() => HostName)));

            NewCommad
            .WithCanExecute(b =>
                            b.And(
                                deploymentServices.IsReadyQuery(b),
                                b.FromProperty(_buildRunning, i => i == 0),
                                b.FromProperty(HostName.IsValid),
                                b.Or(
                                    b.FromProperty(AddSeed, s => !s),
                                    b.FromProperty(SeedHostName.IsValid))))
            .WithExecute(ExecuteBuild)
            .ThenRegister("CreateSeupCommand");
        }
Example #27
0
        public AppCommandProcessor(IMongoCollection <AppData> apps, GridFSBucket files, RepositoryApi repository, DataTransferManager dataTransfer,
                                   IMongoCollection <ToDeleteRevision> toDelete, WorkDistributor <BuildRequest, BuildCompled> workDistributor, IActorRef changeTracker)
        {
            _apps = apps;

            CommandPhase1 <CreateAppCommand>("CreateApp", repository,
                                             (command, reporter) =>
            {
                reporter.Send(DeploymentMessages.RegisterRepository);
                return(new RegisterRepository(command.TargetRepo)
                {
                    IgnoreDuplicate = true
                });
            },
                                             (command, reporter, op) => new ContinueCreateApp(op, command, reporter));

            CommandPhase2 <ContinueCreateApp, CreateAppCommand, AppInfo>("CreateApp2", (command, result, reporter, data) =>
            {
                if (!result.Ok)
                {
                    if (reporter.IsCompled)
                    {
                        return(null);
                    }
                    reporter.Compled(OperationResult.Failure(result.Error ?? BuildErrorCodes.CommandErrorRegisterRepository));
                    return(null);
                }

                if (data != null)
                {
                    reporter.Compled(OperationResult.Failure(BuildErrorCodes.CommandDuplicateApp));
                    return(null);
                }

                var newData = new AppData(command.AppName, -1, DateTime.UtcNow, DateTime.MinValue, command.TargetRepo, command.ProjectName, ImmutableList <AppFileInfo> .Empty);

                apps.InsertOne(newData);
                var info = newData.ToInfo();

                changeTracker.Tell(info);
                return(info);
            });

            CommandPhase1 <PushVersionCommand>("PushVersion",
                                               (command, reporter) =>
            {
                var data = apps.AsQueryable().FirstOrDefault(ad => ad.Name == command.AppName);
                if (data == null)
                {
                    reporter.Compled(OperationResult.Failure(BuildErrorCodes.CommandAppNotFound));
                }
                else
                {
                    BuildRequest.SendWork(workDistributor, reporter, data, repository, BuildEnv.TempFiles.CreateFile())
                    .PipeTo(Self,
                            success: c => new ContinuePushNewVersion(OperationResult.Success(c), command, reporter),
                            failure: e => new ContinuePushNewVersion(OperationResult.Failure(e.Unwrap()?.Message ?? "Cancel"), command, reporter));
                }
            });

            CommandPhase2 <ContinuePushNewVersion, PushVersionCommand, AppBinary>("PushVersion2", (command, result, reporter, data) =>
            {
                if (data == null)
                {
                    if (!reporter.IsCompled)
                    {
                        reporter.Compled(OperationResult.Failure(BuildErrorCodes.CommandAppNotFound));
                    }
                    return(null);
                }

                if (!result.Ok)
                {
                    return(null);
                }

                using var transaction = apps.Database.Client.StartSession(new ClientSessionOptions { DefaultTransactionOptions = new TransactionOptions(writeConcern: WriteConcern.Acknowledged) });
                var dataFilter        = Builders <AppData> .Filter.Eq(ad => ad.Name, data.Name);

                var(commit, fileName) = ((string, ITempFile))result.Outcome !;

                using var targetStream = fileName;

                var newId = files.UploadFromStream(data.Name + ".zip", targetStream.Stream);

                var newBinary  = new AppFileInfo(newId, data.Last + 1, DateTime.UtcNow, false, commit);
                var newBinarys = data.Versions.Add(newBinary);

                var definition = Builders <AppData> .Update;
                var updates    = new List <UpdateDefinition <AppData> >
                {
                    definition.Set(ad => ad.Last, newBinary.Version),
                    definition.Set(ad => ad.Versions, newBinarys)
                };

                var deleteUpdates = new List <ToDeleteRevision>();

                if (data.Versions.Count(s => !s.Deleted) > 5)
                {
                    foreach (var info in newBinarys.OrderByDescending(i => i.CreationTime).Skip(5))
                    {
                        if (info.Deleted)
                        {
                            continue;
                        }
                        info.Deleted = true;
                        deleteUpdates.Add(new ToDeleteRevision(info.File.ToString()));
                    }
                }

                transaction.StartTransaction();

                if (deleteUpdates.Count != 0)
                {
                    toDelete.InsertMany(transaction, deleteUpdates);
                }
                if (!apps.UpdateOne(transaction, dataFilter, definition.Combine(updates)).IsAcknowledged)
                {
                    transaction.AbortTransaction();
                    reporter.Compled(OperationResult.Failure(BuildErrorCodes.DatabaseError));
                    return(null);
                }

                transaction.CommitTransaction();

                changeTracker.Tell(_apps.AsQueryable().FirstOrDefault(ad => ad.Name == command.AppName));
                return(new AppBinary(command.AppName, newBinary.Version, newBinary.CreationTime, false, newBinary.Commit, data.Repository));
            });
 public static IPreparedFeature New(IRepository <AppData, string> apps, IDirectory files, RepositoryApi repository,
                                    DataTransferManager dataTransfer, IRepository <ToDeleteRevision, string> toDelete, IWorkDistributor <BuildRequest> workDistributor,
                                    IActorRef changeTracker)
 => Feature.Create(() => new AppCommandProcessor(),
                   _ => new AppCommandProcessorState(apps, files, repository, dataTransfer, toDelete, workDistributor,
                                                     changeTracker));