123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 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<Worker> _logger;
-
- private IStatsService _serviceClient;
- private IIdentityService _identityService;
- private Timer _timer;
-
- public Worker(ILogger<Worker> logger)
- {
- _logger = logger;
- // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
- _serviceClient = GrpcChannel.ForAddress("https://localhost:5001/").CreateGrpcService<IStatsService>();
- _identityService = GrpcChannel.ForAddress("http://127.0.0.1:5002/").CreateGrpcService<IIdentityService>();
- }
- public override Task StartAsync(CancellationToken cancellationToken)
- {
- _logger.LogInformation("Service is starting.");
-
- _timer = new Timer(DoWork, null, TimeSpan.Zero,
- TimeSpan.FromSeconds(5));
-
- return base.StartAsync(cancellationToken);
- }
- private void DoWork(object state)
- {
- _logger.LogInformation("Service is running.");
- }
- public class GrpcServerStartup
- {
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddGrpc();
- //services.AddTransient<IStatsService, StatsService>();
- }
-
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- app.UseRouting();
-
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapGrpcService<StatsService>();
- endpoints.MapGrpcService<IdentityService>();
- });
- }
- }
- 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<GrpcServerStartup>()
- // .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" });
-
-
- }
- }
- }
- }
|