Exemple #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                //0 Get Token from IS4
                //1 Create SC if not exist
                //2 Retrieve products from product grpc with server stream
                //3 Add sc items into SC with client stream

                //0 Get Token from IS4
                var token = await GetTokenFromIS4();

                //1 Create SC if not exist
                using var scChannel = GrpcChannel.ForAddress(_config.GetValue <string>("WorkerService:ShoppingCartServerUrl"));
                var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel);

                var scModel = await GetOrCreateShoppingCartAsync(scClient, token);

                // open sc client stream
                using var scClientStream = scClient.AddItemIntoShoppingCart();

                //2 Retrieve products from product grpc with server stream
                using var productChannel = GrpcChannel.ForAddress(_config.GetValue <string>("WorkerService:ProductServerUrl"));
                var productClient = new ProductProtoService.ProductProtoServiceClient(productChannel);

                _logger.LogInformation("GetAllProducts started..");
                using var clientData = productClient.GetAllProducts(new GetAllProductsRequest());
                await foreach (var responseData in clientData.ResponseStream.ReadAllAsync())
                {
                    _logger.LogInformation("GetAllProducts Stream Response: {responseData}", responseData);

                    //3 Add sc items into SC with client stream
                    var addNewScItem = new AddItemIntoShoppingCartRequest
                    {
                        Username     = _config.GetValue <string>("WorkerService:UserName"),
                        DiscountCode = "CODE_100",
                        NewCartItem  = new ShoppingCartItemModel
                        {
                            ProductId   = responseData.ProductId,
                            Productname = responseData.Name,
                            Price       = responseData.Price,
                            Color       = "Black",
                            Quantity    = 1
                        }
                    };

                    await scClientStream.RequestStream.WriteAsync(addNewScItem);

                    _logger.LogInformation("ShoppingCart Client Stream Added New Item : {addNewScItem}", addNewScItem);
                }
                await scClientStream.RequestStream.CompleteAsync();

                var addItemIntoShoppingCartResponse = await scClientStream;
                _logger.LogInformation("AddItemIntoShoppingCart Client Stream Response: {addItemIntoShoppingCartResponse}", addItemIntoShoppingCartResponse);

                await Task.Delay(_config.GetValue <int>("WorkerService:TaskInterval"), stoppingToken);
            }
        }
Exemple #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Console.WriteLine("Waiting for server is running...");
            Thread.Sleep(2000);

            while (!stoppingToken.IsCancellationRequested)
            {
                var interval = _configuration.GetValue <int>("WorkerService:TaskInterval");

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                var scUrl = _configuration.GetValue <string>("WorkerService:ShoppingCartServerUrl");
                using var scChannel = GrpcChannel.ForAddress(scUrl);
                var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel);

                var token = await GetTokenFromIS4();

                var scModel = await GetOrCreateShoppingCartAsync(scClient, token);

                var pcUrl = _configuration.GetValue <string>("WorkerService:ProductServerUrl");
                using var scClientStream = scClient.AddItemIntoShoppingCart();
                var pChannel      = GrpcChannel.ForAddress(pcUrl);
                var productClient = new ProductProtoService.ProductProtoServiceClient(pChannel);

                _logger.LogInformation("GetAllProducts started...");
                using var clietData = productClient.GetAllProducts(new GetAllProductsRequest());
                await foreach (var responseData in clietData.ResponseStream.ReadAllAsync())
                {
                    _logger.LogInformation($"GetAllProducts Stream Response: {responseData}", responseData);

                    var userName     = _configuration.GetValue <string>("WorkerService:UserName");
                    var addNewScItem = new AddItemIntoShoppingCartRequest
                    {
                        Username     = userName,
                        DiscountCode = "CODE_100",
                        NewCartItem  = new ShoppingCartItemModel
                        {
                            ProductId   = responseData.Id,
                            ProductName = responseData.Name,
                            Price       = responseData.Price,
                            Color       = "Black",
                            Quantity    = 1
                        }
                    };

                    await scClientStream.RequestStream.WriteAsync(addNewScItem);

                    _logger.LogInformation($"ShoppingCart Client Stream Added new Item : {addNewScItem}", addNewScItem);
                }

                await scClientStream.RequestStream.CompleteAsync();

                var addItemIntoShoppingCartResponse = await scClientStream;
                _logger.LogInformation($"AddItemIntoShoppingCart Client Stream Response : {addItemIntoShoppingCartResponse}", addItemIntoShoppingCartResponse);

                await Task.Delay(interval, stoppingToken);
            }
        }
Exemple #3
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                int delay = _config.GetValue <int>("WorkerService:TaskInterval");
                await Task.Delay(delay, stoppingToken);

                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                // Get Token

                var token = await GetTokenFromIS();

                //create Sopping cart
                //Retrieve products from server
                //add products to a sc
                using var scChannel = GrpcChannel
                                      .ForAddress(_config.GetValue <string>("WorkerService:ShoppingCartServerUrl"));

                //get SC
                var scClient = new ShoppingCartProtoService.ShoppingCartProtoServiceClient(scChannel);
                var scModel  = await GetOrCreateShoppingCartAsync(scClient, token);

                //open shopping cart stream
                using var scClientStream = scClient.AddItemIntoShoppingCart();

                //retrive products
                using var productChannel = GrpcChannel
                                           .ForAddress(_config.GetValue <string>("WorkerService:ProductServerUrl"));
                var productClient = new ProductsProtoService.ProductsProtoServiceClient(productChannel);

                _logger.LogInformation("GetAllProducts --->");
                using var _clientData = productClient.GetAllProducts(new GetAllProductRequest());
                await foreach (var responseData in _clientData.ResponseStream.ReadAllAsync())
                {
                    Console.WriteLine("currentProduct - " + responseData.ToString());
                    var addNewScItem = new AddItemIntoShoppingCartRequest
                    {
                        Username     = _config.GetValue <string>("WorkerService:UserName"),
                        DiscountCode = "CODE_90",
                        NewCartItem  = new ShoppingCartItemModel
                        {
                            Color       = "red",
                            Price       = responseData.Price,
                            ProductId   = responseData.ProductId,
                            ProductName = responseData.Name,
                            Quantity    = 1
                        }
                    };

                    await scClientStream.RequestStream.WriteAsync(addNewScItem);
                }
                await scClientStream.RequestStream.CompleteAsync();

                var addItemIntoSCResponce = await scClientStream;
                _logger.LogInformation($"Added  {addItemIntoSCResponce.InsertCount} products to a shopping cart");
            }
        }