Beispiel #1
0
        public async Task <IActionResult> GetAvailableCars([FromBody] RequestCar requestCar)
        {
            TaskCompletionSource <int> tcs = new TaskCompletionSource <int>();
            CancellationTokenSource    cts = new CancellationTokenSource(10000);

            cts.Token.Register(() => tcs.TrySetResult(0));
            ResponseCar response = new ResponseCar();

            await _pubAndSub.SubscribeAsync(ChannelConfig.ResponseCar, (channel, message) =>
            {
                response = message.ToString().FromJsonTo <ResponseCar>();
                tcs.TrySetResult(1);
            });

            requestCar.Id = Guid.NewGuid();
            await _pubAndSub.PublishAsync(ChannelConfig.RequestCar, requestCar.ToJson());

            var result = await tcs.Task;

            if (result <= 0)
            {
                _log.Information("Timeout when trying to request a car");
                return(NotFound("Not possible to connect with the service."));
            }
            return(Ok(response.ToJson()));
        }
Beispiel #2
0
        public static async Task Run()
        {
            var dataFactory = GetService <DataFactory>();
            var redis       = dataFactory.GetRedisClient().GetDatabase();
            var pubAndSub   = redis.Multiplexer.GetSubscriber();

            _log.Information("Running");

            await pubAndSub.SubscribeAsync(ChannelConfig.RequestCar, async (channel, message) =>
            {
                var request = message.ToString().FromJsonTo <RequestCar>();
                //Here you can have any proccess you want before checking on redis.

                string keyToCheck = keyToSayImChecking + request.Id;
                if (redis.KeyExists(keyToCheck))
                {
                    _log.Information("It's already beeing checked | request {0}", request.Id);
                }
                else
                {
                    redis.StringSet(keyToCheck, 1, TimeSpan.FromSeconds(30));
                    _log.Information("Checking request: {0}", request.Id);
                    List <Car> cars = new List <Car>();

                    for (int i = request.StartYear; i <= request.EndYear; i++)
                    {
                        string key = request.Automaker + request.Name + i;
                        if (redis.KeyExists(key))
                        {
                            var car = (await redis.StringGetAsync(key)).ToString().FromJsonTo <Car>();
                            cars.Add(car);
                        }
                    }

                    var response = new ResponseCar()
                    {
                        Cars = cars
                    };

                    await pubAndSub.PublishAsync(ChannelConfig.ResponseCar, response.ToJson());
                }
            });


            /// Redis Lock Example.
            var system = GetService <ActorSystem>();
            var props  = new RoundRobinPool(5).Props(Props.Create <Master>());

            system.ActorOf(props, "master");
            Console.ReadLine();
        }