private async Task <int> Run(string[] args) { Console.WriteLine(_programTitle); Console.WriteLine(); IOption option = new OptionBuilder() .SetArgs(args) .Build(); if (option.Help) { option.GetHelp() .Append(string.Empty) .ForEach(x => Console.WriteLine(x)); return(_ok); } option.DumpConfigurations(); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); using (ServiceProvider serviceProvider = CreateContainer(option)) { IServiceProvider container = serviceProvider; await InitializeRepository(option, container, cancellationTokenSource.Token); Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; cancellationTokenSource.Cancel(); Console.WriteLine("Canceling..."); }; var activities = new Func <Task>[] { () => option.Agent && option.Get ? container.GetRequiredService <AgentActivity>().Get(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Agent && option.List ? container.GetRequiredService <AgentActivity>().List(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Agent && option.Delete ? container.GetRequiredService <AgentActivity>().Delete(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Agent && option.Clear ? container.GetRequiredService <AgentActivity>().Clear(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Target && option.Get ? container.GetRequiredService <TargetActivity>().Get(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Target && option.List ? container.GetRequiredService <TargetActivity>().List(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Target && option.Delete ? container.GetRequiredService <TargetActivity>().Delete(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Target && option.Clear ? container.GetRequiredService <TargetActivity>().Clear(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Monitor ? container.GetRequiredService <MonitorActivity>().Run(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Template ? container.GetRequiredService <TemplateActivity>().Create() : Task.CompletedTask, () => option.Import ? container.GetRequiredService <ImportActivity>().Import(cancellationTokenSource.Token) : Task.CompletedTask, () => option.Balance ? container.GetRequiredService <BalanceActivity>().BalanceAgents(cancellationTokenSource.Token) : Task.CompletedTask, }; await activities .ForEachAsync(async x => await x()); } Console.WriteLine(); Console.WriteLine("Completed"); return(_ok); }
private async Task <int> Run(string[] args) { Console.WriteLine(_programTitle); Console.WriteLine(); Option option = new OptionBuilder() .SetArgs(args) .Build(); if (option.Help) { option.GetHelp() .Append(string.Empty) .ForEach(x => Console.WriteLine(x)); return(_ok); } CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); using (ServiceProvider container = BuildContainer(option)) { ILogger <Program> logger = container .GetRequiredService <ILoggerFactory>().CreateLogger <Program>(); option.DumpConfigurations(logger); Console.CancelKeyPress += (object?_, ConsoleCancelEventArgs e) => { e.Cancel = true; cancellationTokenSource.Cancel(); Console.WriteLine("Canceling..."); }; var executeQueue = new ActionBlock <Func <Task> >(async x => await x()); if (option.Build) { await executeQueue.SendAsync(() => container.GetRequiredService <ClearBuildFolderActivity>().Clear()); } if (option.Build) { await executeQueue.SendAsync(() => container.GetRequiredService <BuildArticlesActivity>().Build(cancellationTokenSource.Token)); } if (option.Build) { await executeQueue.SendAsync(() => container.GetRequiredService <BuildArticleIndexActivity>().Build(cancellationTokenSource.Token)); } if (option.Build) { await executeQueue.SendAsync(() => container.GetRequiredService <BuildWordIndexActivity>().Build(cancellationTokenSource.Token)); } if (option.Upload) { await executeQueue.SendAsync(() => container.GetRequiredService <UploadArticlesActivity>().Upload(cancellationTokenSource.Token)); } if (option.Upload) { await executeQueue.SendAsync(() => container.GetRequiredService <UploadDirectoryActivity>().Upload(cancellationTokenSource.Token)); } executeQueue.Complete(); await executeQueue.Completion; } Console.WriteLine(); Console.WriteLine("Completed"); return(_ok); }