fix report progress notification mech

This commit is contained in:
Фролов 2021-05-20 12:38:25 +05:00
parent 1c6990487f
commit d57f495ff0
6 changed files with 14 additions and 15 deletions

View File

@ -4,8 +4,8 @@ namespace AsbCloudApp.Services
{ {
public interface IBackgroundQueue public interface IBackgroundQueue
{ {
int EnqueueTask(Action action); int EnqueueTask(Action<int> action);
bool TryDequeue(out (Action action, int id) item); bool TryDequeue(out (Action<int> action, int id) item);
} }
} }

View File

@ -7,7 +7,7 @@ namespace AsbCloudApp.Services
{ {
string RootPath { get; } string RootPath { get; }
int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, DateTime end, int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, DateTime end,
EventHandler<(float progress, string operation)> handleReportProgress); Action<float, string, int > handleReportProgress);
int GetReportPagesCount(int wellId, DateTime begin, DateTime end, int stepSeconds, int format); int GetReportPagesCount(int wellId, DateTime begin, DateTime end, int stepSeconds, int format);
DatesRangeDto GetReportsDatesRange(int wellId); DatesRangeDto GetReportsDatesRange(int wellId);
} }

View File

@ -6,12 +6,12 @@ namespace AsbCloudInfrastructure.Services
{ {
class BackgroundQueue : IBackgroundQueue class BackgroundQueue : IBackgroundQueue
{ {
private readonly ConcurrentQueue<(Action action, int id)> tasks = private readonly ConcurrentQueue<(Action<int> action, int id)> tasks =
new ConcurrentQueue<(Action action, int id)>(); new ConcurrentQueue<(Action<int> action, int id)>();
private int id = 0; private int id = 0;
public int EnqueueTask(Action action) public int EnqueueTask(Action<int> action)
{ {
if (action == null) if (action == null)
throw new ArgumentNullException(nameof(action)); throw new ArgumentNullException(nameof(action));
@ -20,7 +20,7 @@ namespace AsbCloudInfrastructure.Services
return id; return id;
} }
public bool TryDequeue(out (Action action, int id) item) public bool TryDequeue(out (Action<int> action, int id) item)
=> tasks.TryDequeue(out item); => tasks.TryDequeue(out item);
} }
} }

View File

@ -21,7 +21,7 @@ namespace AsbCloudInfrastructure.Services
while (!token.IsCancellationRequested) while (!token.IsCancellationRequested)
{ {
if (tasksQueue.TryDequeue(out var item)) if (tasksQueue.TryDequeue(out var item))
await Task.Run(item.action, token); await Task.Run(()=>item.action(item.id), token);
else else
await Task.Delay(100, token); await Task.Delay(100, token);
} }

View File

@ -28,9 +28,9 @@ namespace AsbCloudInfrastructure.Services
public string RootPath { get ; private set; } public string RootPath { get ; private set; }
public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin, public int CreateReport(int wellId, int stepSeconds, int format, DateTime begin,
DateTime end, EventHandler<(float progress, string operation)> progressHandler) DateTime end, Action<float , string , int> progressHandler)
{ {
var newReportId = queue.EnqueueTask(() => var newReportId = queue.EnqueueTask((id) =>
{ {
var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>(); var optionsBuilder = new DbContextOptionsBuilder<AsbCloudDbContext>();
optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection")); optionsBuilder.UseNpgsql(configuration.GetConnectionString("DefaultConnection"));
@ -38,7 +38,7 @@ namespace AsbCloudInfrastructure.Services
using (var context = new AsbCloudDbContext(optionsBuilder.Options)) using (var context = new AsbCloudDbContext(optionsBuilder.Options))
{ {
var generator = GetReportGenerator(wellId, begin, end, stepSeconds, format, context); var generator = GetReportGenerator(wellId, begin, end, stepSeconds, format, context);
generator.OnProgress += progressHandler; generator.OnProgress += (s,e)=>progressHandler.Invoke(e.progress, e.operation, id);
generator.Make(); generator.Make();
} }
}); });

View File

@ -28,10 +28,9 @@ namespace AsbCloudWebApi.Controllers
private string signalRConnectionId = "0"; private string signalRConnectionId = "0";
private void HandleReportProgress(object sender, (float progress, string operation) e) private void HandleReportProgress (float progress, string operation, int id) =>
{ reportsHubContext.Clients.Group($"Report{id}").SendAsync("GetReportProgress", progress);
reportsHubContext.Clients.Client(signalRConnectionId).SendAsync("GetReportProgress", e.progress);
}
/// <summary> /// <summary>
/// Создает отчет по скважине с указанными параметрами /// Создает отчет по скважине с указанными параметрами