diff --git a/src/Neo.CLI/CLI/CommandLineOption.cs b/src/Neo.CLI/CLI/CommandLineOption.cs index e775e8bf5f..d3d5b1ba7d 100644 --- a/src/Neo.CLI/CLI/CommandLineOption.cs +++ b/src/Neo.CLI/CLI/CommandLineOption.cs @@ -21,6 +21,7 @@ public class CommandLineOptions public string? DBPath { get; init; } public LogLevel Verbose { get; init; } = LogLevel.Info; public bool? NoVerify { get; init; } + public bool Background { get; init; } /// /// Check if CommandLineOptions was configured diff --git a/src/Neo.CLI/CLI/MainService.CommandLine.cs b/src/Neo.CLI/CLI/MainService.CommandLine.cs index 04cb4ecfbc..7e37986929 100644 --- a/src/Neo.CLI/CLI/MainService.CommandLine.cs +++ b/src/Neo.CLI/CLI/MainService.CommandLine.cs @@ -21,11 +21,12 @@ public partial class MainService { public int OnStartWithCommandLine(string[] args) { - RootCommand rootCommand = new(Assembly.GetExecutingAssembly().GetCustomAttribute()!.Title) + var rootCommand = new RootCommand(Assembly.GetExecutingAssembly().GetCustomAttribute()!.Title) { new Option(["-c", "--config","/config"], "Specifies the config file."), new Option(["-w", "--wallet","/wallet"], "The path of the neo3 wallet [*.json]."), new Option(["-p", "--password" ,"/password"], "Password to decrypt the wallet, either from the command line or config file."), + new Option(["--background","/background"], "Run the service in background."), new Option(["--db-engine","/db-engine"], "Specify the db engine."), new Option(["--db-path","/db-path"], "Specify the db path."), new Option(["--noverify","/noverify"], "Indicates whether the blocks need to be verified when importing."), @@ -39,6 +40,7 @@ public int OnStartWithCommandLine(string[] args) private void Handle(RootCommand command, CommandLineOptions options, InvocationContext context) { + IsBackground = options.Background; Start(options); } @@ -72,7 +74,9 @@ private static void CustomProtocolSettings(CommandLineOptions options, ProtocolS private static void CustomApplicationSettings(CommandLineOptions options, Settings settings) { - var tempSetting = string.IsNullOrEmpty(options.Config) ? settings : new Settings(new ConfigurationBuilder().AddJsonFile(options.Config, optional: true).Build().GetSection("ApplicationConfiguration")); + var tempSetting = string.IsNullOrEmpty(options.Config) + ? settings + : new Settings(new ConfigurationBuilder().AddJsonFile(options.Config, optional: true).Build().GetSection("ApplicationConfiguration")); var customSetting = new Settings { Logger = tempSetting.Logger, diff --git a/src/Neo.ConsoleService/ConsoleServiceBase.cs b/src/Neo.ConsoleService/ConsoleServiceBase.cs index 7e5d9eee96..15a574053f 100644 --- a/src/Neo.ConsoleService/ConsoleServiceBase.cs +++ b/src/Neo.ConsoleService/ConsoleServiceBase.cs @@ -35,6 +35,8 @@ public abstract class ConsoleServiceBase protected bool ShowPrompt { get; set; } = true; + protected bool IsBackground { get; set; } = false; + private bool _running; private readonly CancellationTokenSource _shutdownTokenSource = new(); private readonly CountdownEvent _shutdownAcknowledged = new(1); @@ -522,7 +524,7 @@ private void OnScCommand(string action) } string arguments; - if (action == "/install") + if (action == "install") { var fileName = Process.GetCurrentProcess().MainModule!.FileName; arguments = $"create {ServiceName} start= auto binPath= \"{fileName}\""; @@ -551,17 +553,39 @@ private void OnScCommand(string action) } } + private void WaitForShutdown() + { + _running = true; + try + { + _shutdownTokenSource.Token.WaitHandle.WaitOne(); + } + catch (OperationCanceledException) + { + // Expected when shutdown is triggered + } + _running = false; + } + public void Run(string[] args) { if (Environment.UserInteractive) { - if (args.Length == 1 && (args[0] == "/install" || args[0] == "/uninstall")) + if (args.Length == 1 && (args[0] == "--install" || args[0] == "/install")) { - OnScCommand(args[0]); + OnScCommand("install"); + } + else if (args.Length == 1 && (args[0] == "--uninstall" || args[0] == "/uninstall")) + { + OnScCommand("uninstall"); } else { - if (OnStart(args)) RunConsole(); + if (OnStart(args)) + { + if (IsBackground) WaitForShutdown(); + else RunConsole(); + } OnStop(); } }