public async ValueTask Run(HttpRequestMessage request, ThreadResult result, CancellationToken cancellationToken) { await Task.Factory.StartNew (() => { Task.Delay(SleepDurationMilliseconds).Wait(); result.Add(request.RequestUri.ToString(), DateTime.UtcNow, DateTime.UtcNow.AddSeconds(1), true, 200, 0, 0, 0, 0, null, null); }, cancellationToken); }
public void AddTest() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var options = new DbContextOptionsBuilder <EfDbContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new EfDbContext(options)) { var environment = new Mock <IEnvironment>(); environment.Setup(x => x.SystemDateTimeUtc).Returns(new DateTime(2019, 8, 1)); environment.Setup(x => x.UserName).Returns("TestUser"); context.Database.EnsureCreated(); var service = new EfResultStore(context, environment.Object); var projectName = "TestProject"; var attributes = new Dictionary <string, string>(); attributes.Add(nameof(projectName), projectName); var start = new DateTime(2019, 8, 1); var result = new ThreadResult(); result.Add("/", start, start.AddSeconds(1), true, 200, 100, 200, 300, 400, "Headers", new Exception("Test")); service.Add(projectName, -1, attributes, result); var projects = context.Projects.ToArray(); var runAttributes = context.RunAttributes.ToArray(); var results = context.Results.ToArray(); var sb = new StringBuilder(); sb.Append(JsonConvert.SerializeObject(projects)); sb.AppendLine(); sb.Append(JsonConvert.SerializeObject(runAttributes)); sb.AppendLine(); sb.Append(JsonConvert.SerializeObject(results)); Approvals.Verify(sb.ToString()); } } finally { connection.Close(); } }
private void LongRuningTask(object data) { // simulate long running task – your main logic should go here ThreadResult.Progress = 0; ThreadResult.UpdateString = "Starting..."; for (int i = 0; i < 100; i++) { ThreadResult.Progress++; double d1 = ThreadResult.Progress; double d2 = 100.0; ThreadResult.UpdateString = "Progress at " + (100 * d1 / d2).ToString() + "%"; Thread.Sleep(4000); } // Add ThreadResult -- when this // line executes it means task has been // completed ThreadResult.Add(requestId.ToString(), "Item Processed Successfully."); // you can add your result in second parameter. }
public async ValueTask Run(HttpRequestMessage request, ThreadResult result, CancellationToken cancellationToken) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (result == null) { throw new ArgumentNullException(nameof(result)); } var requestUri = request.RequestUri.ToString(); var startDateTimeUtc = DateTime.UtcNow; var isSuccessStatusCode = false; var statusCode = 500; var headerLength = 0L; var responseLength = 0L; var requestSentTicks = 0L; var responseTicks = 0L; Exception exception = null; string responseHeaders = null; try { var sw = Stopwatch.StartNew(); using (var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)) { requestSentTicks = sw.ElapsedTicks; statusCode = (int)response.StatusCode; isSuccessStatusCode = response.IsSuccessStatusCode; // 200-299 responseHeaders = response.ToString(); headerLength = responseHeaders.Length; sw.Restart(); // Read content to get response length. using (var responseStream = await response.Content.ReadAsStreamAsync()) { int read; int offset = 0; var responseBuffer = new byte[500]; do { read = await responseStream.ReadAsync(responseBuffer, 0, responseBuffer.Length); offset += read; } while (read != 0); responseLength = offset; } ////TODO: Saving result does make is much slover. //using (var sourceStream = await response.Content.ReadAsStreamAsync()) //{ // string fileToWriteTo = Path.GetTempFileName(); // using (var destinationStream = new FileStream(fileToWriteTo, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose)) // { // await sourceStream.CopyToAsync(destinationStream); // responseLength = destinationStream.Length; // } //} sw.Stop(); responseTicks = sw.ElapsedTicks; } } catch (Exception ex) { exception = ex; } finally { result.Add(requestUri, startDateTimeUtc, DateTime.UtcNow, isSuccessStatusCode, statusCode, headerLength, responseLength, requestSentTicks, responseTicks, responseHeaders, exception); } }