Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Worker.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using Grpc.Core;
  2. using Grpc.Net.Client;
  3. using GrpcShared.DTO;
  4. using GrpcShared.Interfaces;
  5. using IdentityProvider.Services;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Server.Kestrel.Core;
  9. using ProtoBuf.Grpc.Client;
  10. using SpotifyService.Services;
  11. namespace SpotifyWorker
  12. {
  13. public class Worker : BackgroundService
  14. {
  15. private readonly ILogger<Worker> _logger;
  16. private IStatsService _serviceClient;
  17. private IIdentityService _identityService;
  18. private Timer _timer;
  19. public Worker(ILogger<Worker> logger)
  20. {
  21. _logger = logger;
  22. // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
  23. _serviceClient = GrpcChannel.ForAddress("https://localhost:5001/").CreateGrpcService<IStatsService>();
  24. _identityService = GrpcChannel.ForAddress("http://127.0.0.1:5002/").CreateGrpcService<IIdentityService>();
  25. }
  26. public override Task StartAsync(CancellationToken cancellationToken)
  27. {
  28. _logger.LogInformation("Service is starting.");
  29. _timer = new Timer(DoWork, null, TimeSpan.Zero,
  30. TimeSpan.FromSeconds(5));
  31. return base.StartAsync(cancellationToken);
  32. }
  33. private void DoWork(object state)
  34. {
  35. _logger.LogInformation("Service is running.");
  36. }
  37. public class GrpcServerStartup
  38. {
  39. public void ConfigureServices(IServiceCollection services)
  40. {
  41. services.AddGrpc();
  42. //services.AddTransient<IStatsService, StatsService>();
  43. }
  44. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  45. {
  46. app.UseRouting();
  47. app.UseEndpoints(endpoints =>
  48. {
  49. endpoints.MapGrpcService<StatsService>();
  50. endpoints.MapGrpcService<IdentityService>();
  51. });
  52. }
  53. }
  54. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  55. {
  56. while (!stoppingToken.IsCancellationRequested)
  57. {
  58. // await Host.CreateDefaultBuilder()
  59. //.ConfigureWebHostDefaults(builder =>
  60. //{
  61. // builder
  62. // //.ConfigureKestrel(options =>
  63. // //{
  64. // // options.ListenAnyIP(5002, listenOptions =>
  65. // // {
  66. // // listenOptions.Protocols = HttpProtocols.Http2;
  67. // // });
  68. // //})
  69. // .UseStartup<GrpcServerStartup>()
  70. // .UseUrls("http://localhost:5005");
  71. //})
  72. //.Build()
  73. //.StartAsync(stoppingToken);
  74. //await _statsService.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  75. //var result = await _httpClient.GetAsync("url");
  76. //_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
  77. await Task.Delay(5000, stoppingToken);
  78. //try
  79. //{
  80. // var res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "630ddbad89698131d98dc0fd" });
  81. // Console.WriteLine(res.Item!.Name);
  82. //}
  83. //catch (RpcException e)
  84. //{
  85. // if (e.StatusCode == StatusCode.Cancelled)
  86. // {
  87. // return;
  88. // }
  89. // Console.WriteLine(e.Message);
  90. // throw;
  91. //}
  92. try
  93. {
  94. var res = await _identityService.ListUsersAsync(new VoidMessage());
  95. if (res == null) return;
  96. for (int i = 0; i < res.Count; i++)
  97. {
  98. try
  99. {
  100. var track = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage
  101. {
  102. UserId = res[i].Id
  103. });
  104. Console.WriteLine(track.IsSaved);
  105. if (track != null)
  106. {
  107. await _identityService.SaveTrackAsync(new GrpcShared.DTO.Db.SaveTrackRequest
  108. {
  109. Album = track.Item.Album.Name,
  110. Artist = track.Item.Artists[0].Name,
  111. Title = track.Item.Name,
  112. TrackId = track.Item.Id,
  113. UserId = res[i].Id
  114. });
  115. }
  116. }
  117. catch (RpcException ex)
  118. {
  119. if (ex.StatusCode == StatusCode.Cancelled) continue;
  120. throw;
  121. }
  122. }
  123. }
  124. catch (RpcException e)
  125. {
  126. if (e.StatusCode == StatusCode.Cancelled)
  127. {
  128. return;
  129. }
  130. //Console.WriteLine(e.Message);
  131. throw;
  132. }
  133. //res = await _serviceClient.GetCurrentlyPlayingTrack(new GrpcShared.DTO.SessionMessage { UserId = "6308a8bfc731f7b44d76ac4e" });
  134. }
  135. }
  136. }
  137. }