Esempio n. 1
0
        public StrategyRunnerBackgroundService(IServerMonitor serverMonitor, IStrategyRunnerActionBlock strategyRunnerActionBlock, ILoggerFactory loggerFactory)
        {
            this.serverMonitor             = serverMonitor;
            this.strategyRunnerActionBlock = strategyRunnerActionBlock;

            logger = loggerFactory.CreateLogger <StrategyRunnerBackgroundService>();
        }
Esempio n. 2
0
        public async Task Invoke(HttpContext context, IStrategyRunner strategyRunner, IStrategyRunnerActionBlock strategyRunnerActionBlock)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (strategyRunner == null)
            {
                throw new ArgumentNullException(nameof(strategyRunner));
            }

            if (strategyRunnerActionBlock == null)
            {
                throw new ArgumentNullException(nameof(strategyRunnerActionBlock));
            }

            try
            {
                var json = context.Request.Form["strategy"];

                var strategy = JsonConvert.DeserializeObject <Strategy>(json);

                var downloadsPath = Path.Combine(Directory.GetCurrentDirectory(), "downloads", Guid.NewGuid().ToString());

                if (!Directory.Exists(downloadsPath))
                {
                    Directory.CreateDirectory(downloadsPath);
                }

                if (context.Request.HasFormContentType)
                {
                    var form = context.Request.Form;

                    var downloads = from f
                                    in form.Files
                                    select Download(f, downloadsPath);

                    await Task.WhenAll(downloads.ToArray()).ConfigureAwait(false);
                }

                var strategyRunnerActionBlockInput = new StrategyRunnerActionBlockInput
                {
                    StrategyRunner = strategyRunner,
                    Strategy       = strategy,
                    DownloadsPath  = downloadsPath
                };

                await strategyRunnerActionBlock.RunStrategyAsync(strategyRunnerActionBlockInput).ConfigureAwait(false);

                await context.Response.WriteAsync(json).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                var response = context.Response;
                response.ContentType = "application/json";
                response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                await response.WriteAsync(JsonConvert.SerializeObject(ex)).ConfigureAwait(false);
            }
        }