Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Neo.CLI/CLI/CommandLineOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

/// <summary>
/// Check if CommandLineOptions was configured
Expand Down
8 changes: 6 additions & 2 deletions src/Neo.CLI/CLI/MainService.CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public partial class MainService
{
public int OnStartWithCommandLine(string[] args)
{
RootCommand rootCommand = new(Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>()!.Title)
var rootCommand = new RootCommand(Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>()!.Title)
{
new Option<string>(["-c", "--config","/config"], "Specifies the config file."),
new Option<string>(["-w", "--wallet","/wallet"], "The path of the neo3 wallet [*.json]."),
new Option<string>(["-p", "--password" ,"/password"], "Password to decrypt the wallet, either from the command line or config file."),
new Option<bool>(["--background","/background"], "Run the service in background."),
new Option<string>(["--db-engine","/db-engine"], "Specify the db engine."),
new Option<string>(["--db-path","/db-path"], "Specify the db path."),
new Option<string>(["--noverify","/noverify"], "Indicates whether the blocks need to be verified when importing."),
Expand All @@ -39,6 +40,7 @@ public int OnStartWithCommandLine(string[] args)

private void Handle(RootCommand command, CommandLineOptions options, InvocationContext context)
{
IsBackground = options.Background;
Start(options);
}

Expand Down Expand Up @@ -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,
Expand Down
32 changes: 28 additions & 4 deletions src/Neo.ConsoleService/ConsoleServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}\"";
Expand Down Expand Up @@ -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();
}
}
Expand Down
Loading