-1

public interface IBasicEvent
{
    void OnSuccess();
    void OnError();
    void Complete();
}

|

    1

    0

    public class ReportRunner
    {
        public delegate Task RunCompleteHandler(ReportRunner runner, ReportStatusEventArgs eventArgs);
        public event RunCompleteHandler RunComplete;
    
        protected void OnRunComplete(ReportRunner runner, ReportStatusEventArgs eventArgs)
        {
            RunComplete?.Invoke(runner, eventArgs);
        }
    
        private async Task ExecuteReport(FilterSet filters, Guid guid)
        {
            try
            {
                // run the report
            }
            catch (Exception ex)
            {
                // log and set the report to error status
            }
            finally
            {
                var status = _reportLogic.GetReportRunStatus(guid);
                var eventArgs = new ReportStatusEventArgs(status);
    
                OnRunComplete(this, eventArgs);
            }
        }
    }
    
    public class ReportStatusEventArgs : EventArgs
    {
        public readonly ReportRunStatus ReportRunStatus;
        public ReportStatusEventArgs(ReportRunStatus reportRunStatus)
        {
            ReportRunStatus = reportRunStatus;
        }
    }
    

    public async Task<IActionResult> Run(FiltersRequest request)
    {
        // basic checks omitted
        var factory = new ReportFactory();
        var reportModel = factory.CreateReport(reportType);
        var runner = new ReportRunner(reportModel, _contexts);
        var eventHandler = new ReportRunnerSubscriber(_reportHub, HttpContext.User.Identity.Name);
        eventHandler.Subscribe(runner);
    
        var guid = await runner.Run(filterSet);
    
        return Json(guid);
    }
    

    public class ReportRunnerSubscriber
    {
        private readonly IHubContext<ReportHub> _reportHub;
        private readonly string _userId;
    
        public ReportRunnerSubscriber(IHubContext<ReportHub> reportHub, string userId)
        {
            _reportHub = reportHub;
            _userId = userId;
        }
    
        public void Subscribe(ReportRunner reportRunner)
        {
            reportRunner.RunComplete += NotifySubscribers;
        }
    
        public async Task NotifySubscribers(ReportRunner reportRunner, ReportStatusEventArgs eventArgs)
        {
            var status = eventArgs.ReportRunStatus;
            status.ReportRuns = null;
            await _reportHub.Clients.User(_userId).SendAsync("ReportComplete", status);
        }
    }
    
    |