Skip to content

Commit

Permalink
Merge pull request planetarium#74 from longfin/feature/async-main
Browse files Browse the repository at this point in the history
async Main() and flushing
  • Loading branch information
longfin committed Oct 23, 2019
2 parents c0748bb + 6b41ed6 commit d9259ba
Showing 1 changed file with 66 additions and 61 deletions.
127 changes: 66 additions & 61 deletions Libplanet.Explorer.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Libplanet.Explorer.Executable
/// </summary>
public class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
Options options = Options.Parse(args, Console.Error);

Expand All @@ -38,7 +38,11 @@ public static void Main(string[] args)
.WriteTo.Console();
Log.Logger = loggerConfig.CreateLogger();

IStore store = new LiteDBStore(options.StorePath, readOnly: options.Seed is null);
IStore store = new LiteDBStore(
path: options.StorePath,
flush: false,
readOnly: options.Seed is null
);
IBlockPolicy<AppAgnosticAction> policy = new BlockPolicy<AppAgnosticAction>(
null,
blockIntervalMilliseconds: options.BlockIntervalMilliseconds,
Expand All @@ -47,6 +51,12 @@ public static void Main(string[] args)
var blockChain = new BlockChain<AppAgnosticAction>(policy, store);
Startup.BlockChainSingleton = blockChain;

IWebHost webHost = WebHost.CreateDefaultBuilder()
.UseStartup<ExplorerStartup<AppAgnosticAction, Startup>>()
.UseSerilog()
.UseUrls($"http://{options.Host}:{options.Port}/")
.Build();

Swarm<AppAgnosticAction> swarm = null;
if (options.Seed is BoundPeer)
{
Expand All @@ -71,76 +81,71 @@ public static void Main(string[] args)
);
}

IWebHost webHost = WebHost.CreateDefaultBuilder()
.UseStartup<ExplorerStartup<AppAgnosticAction, Startup>>()
.UseSerilog()
.UseUrls($"http://{options.Host}:{options.Port}/")
.Build();

var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, eventArgs) =>
using (var cts = new CancellationTokenSource())
using (swarm)
{
eventArgs.Cancel = true;
cts.Cancel();
};
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
cts.Cancel();
};

Task swarmTask = Task.Run(
async () =>
try
{
if (swarm is null)
{
return;
}
var peers = new HashSet<Peer>();
if (options.Seed is Peer peer)
{
peers.Add(peer);
}
try
{
await swarm.BootstrapAsync(
peers,
5000,
5000,
cancellationToken: cts.Token
);
}
catch (TimeoutException)
{
Console.Error.WriteLine("No any neighbors.");
}
// Since explorer does not require states, turn off trustedPeer option.
/*ImmutableHashSet<Address> trustedPeers =
peers.Select(p => p.Address).ToImmutableHashSet();*/
var trustedPeers = ImmutableHashSet<Address>.Empty;
Console.Error.WriteLine("Starts preloading.");
await swarm.PreloadAsync(
dialTimeout: TimeSpan.FromSeconds(15),
trustedStateValidators: trustedPeers,
cancellationToken: cts.Token
await Task.WhenAll(
webHost.RunAsync(cts.Token),
StartSwarmAsync(swarm, options.Seed, cts.Token)
);
Console.Error.WriteLine("Finished preloading.");
}
catch (OperationCanceledException)
{
await swarm?.StopAsync(waitFor: TimeSpan.FromSeconds(1))
.ContinueWith(_ => NetMQConfig.Cleanup(false));
}
}
}

await swarm.StartAsync(cancellationToken: cts.Token);
},
cts.Token
);
private static async Task StartSwarmAsync(
Swarm<AppAgnosticAction> swarm,
Peer seed,
CancellationToken cancellationToken)
{
if (swarm is null)
{
return;
}

var peers = new HashSet<Peer>();
if (!(seed is null))
{
peers.Add(seed);
}

try
{
Task.WaitAll(webHost.RunAsync(cts.Token), swarmTask);
await swarm.BootstrapAsync(
peers,
5000,
5000,
cancellationToken: cancellationToken
);
}
catch (OperationCanceledException)
catch (TimeoutException)
{
if (swarm is Swarm<AppAgnosticAction>)
{
Task.WaitAll(swarm.StopAsync(waitFor: TimeSpan.FromSeconds(1)));
NetMQConfig.Cleanup(false);
}
Console.Error.WriteLine("No any neighbors.");
}

// Since explorer does not require states, turn off trustedPeer option.
var trustedPeers = ImmutableHashSet<Address>.Empty;
Console.Error.WriteLine("Starts preloading.");
await swarm.PreloadAsync(
dialTimeout: TimeSpan.FromSeconds(15),
trustedStateValidators: trustedPeers,
cancellationToken: cancellationToken
);
Console.Error.WriteLine("Finished preloading.");

await swarm.StartAsync(cancellationToken: cancellationToken);
}

internal class AppAgnosticAction : IAction
Expand Down

0 comments on commit d9259ba

Please sign in to comment.