Making cartservice more reliable

1. Making sure we re-create redis connection upon disconnect
2. Fixed local cart store implementation to handle updates (useful for testing w/o redis)
3. Fixed windows scripts to work against redis correctly
This commit is contained in:
Simon Zeltser 2018-07-02 13:26:37 -07:00
parent 11c208a9f4
commit d457f7ec28
6 changed files with 179 additions and 103 deletions

View file

@ -33,26 +33,36 @@ namespace cartservice
{
// Run the server in a separate thread and make the main thread busy waiting.
// The busy wait is because when we run in a container, we can't use techniques such as waiting on user input (Console.Readline())
Task.Run(async () =>
Task serverTask = Task.Run(async () =>
{
Console.WriteLine($"Trying to start a grpc server at {host}:{port}");
Server server = new Server
try
{
Services = { Hipstershop.CartService.BindService(new CartServiceImpl(cartStore)) },
Ports = { new ServerPort(host, port, ServerCredentials.Insecure) }
};
Console.WriteLine($"Trying to start a grpc server at {host}:{port}");
Server server = new Server
{
Services = { Hipstershop.CartService.BindService(new CartServiceImpl(cartStore)) },
Ports = { new ServerPort(host, port, ServerCredentials.Insecure) }
};
Console.WriteLine($"Cart server is listening at {host}:{port}");
server.Start();
Console.WriteLine($"Cart server is listening at {host}:{port}");
server.Start();
await cartStore.InitializeAsync();
await cartStore.InitializeAsync();
Console.WriteLine("Initialization completed");
// Keep the server up and running
while(true)
{
Thread.Sleep(TimeSpan.FromMinutes(10));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
});
// Busy wait to keep the process alive
while(true)
{
Thread.Sleep(TimeSpan.FromMinutes(10));
}
return Task.WaitAny(new[] { serverTask });
}
static void Main(string[] args)
@ -69,6 +79,8 @@ namespace cartservice
Parser.Default.ParseArguments<ServerOptions>(args).MapResult(
(ServerOptions options) =>
{
Console.WriteLine($"Started as process with id {System.Diagnostics.Process.GetCurrentProcess().Id}");
// Set hostname/ip address
string hostname = options.Host;
if (string.IsNullOrEmpty(hostname))
@ -106,7 +118,10 @@ namespace cartservice
// Redis was specified via command line or environment variable
if (!string.IsNullOrEmpty(redis))
{
// If you want to start cart store using local cache in process, you can replace the following line with this:
// cartStore = new LocalCartStore();
cartStore = new RedisCartStore(redis);
return StartServer(hostname, port, cartStore);
}
else