using Grpc.Core; using Grpc.Net.Client; using GrpcShared.DTO; using GrpcShared.Interfaces; using IdentityProvider.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Core; using ProtoBuf.Grpc.Client; using SpotifyService.Services; namespace SpotifyWorker { public class Worker : BackgroundService { private readonly ILogger _logger; private IStatsService _serviceClient; private IIdentityService _identityService; public Worker(ILogger logger) { _logger = logger; // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); _serviceClient = GrpcChannel.ForAddress("https://localhost:5001/").CreateGrpcService(); _identityService = GrpcChannel.ForAddress("http://127.0.0.1:5002/").CreateGrpcService(); } public override Task StartAsync(CancellationToken cancellationToken) { return base.StartAsync(cancellationToken); } public class GrpcServerStartup { public void ConfigureServices(IServiceCollection services) { services.AddGrpc(); //services.AddTransient(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGrpcService(); endpoints.MapGrpcService(); }); } } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { // await Host.CreateDefaultBuilder() //.ConfigureWebHostDefaults(builder => //{ // builder // //.ConfigureKestrel(options => // //{ // // options.ListenAnyIP(5002, listenOptions => // // { // // listenOptions.Protocols = HttpProtocols.Http2; // // }); // //}) // .UseStartup() // .UseUrls("http://localhost:5005"); //}) //.Build() //.StartAsync(stoppingToken); //await _statsService.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" }); //var result = await _httpClient.GetAsync("url"); //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(5000, stoppingToken); //try //{ // var res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "630ddbad89698131d98dc0fd" }); // Console.WriteLine(res.Item!.Name); //} //catch (RpcException e) //{ // if (e.StatusCode == StatusCode.Cancelled) // { // return; // } // Console.WriteLine(e.Message); // throw; //} try { var res = await _identityService.ListUsersAsync(new VoidMessage()); if (res == null) return; for (int i = 0; i < res.Count; i++) { try { var track = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = res[i].Id }); Console.WriteLine(track.IsSaved); if (track != null) { await _identityService.SaveTrackAsync(new GrpcShared.DTO.Db.SaveTrackRequest { Album = track.Item.Album.Name, Artist = track.Item.Artists[0].Name, Title = track.Item.Name, TrackId = track.Item.Id, UserId = res[i].Id }); } } catch (RpcException ex) { if (ex.StatusCode == StatusCode.Cancelled) continue; throw; } } } catch (RpcException e) { if (e.StatusCode == StatusCode.Cancelled) { return; } //Console.WriteLine(e.Message); throw; } //res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" }); } } } }