public static int CreateNewTask( string tenantId, string userObjectId, string subscriptionId, string subscriptionName, string targetRegion) { using (var db = new AssessmentDbContext(ConfigHelper.DatabaseConnectionString)) { var task = new AssessmentTask { DeploymentId = DeploymentId, TenantId = tenantId, UserObjectId = userObjectId, SubscriptionId = subscriptionId, SubscriptionName = subscriptionName, TargetRegion = targetRegion, Status = AssessmentTask.TaskStatus.NotStarted, CreatedTime = DateTime.UtcNow }; db.AssessmentTasks.Add(task); db.SaveChanges(); return(task.Id); } }
private async Task ProcessTaskAsync( AssessmentTask taskDefinition, TelemetryContext telemetryContext, string accessToken) { var telemetryManager = TelemetryHelper.CreateTelemetryManager(telemetryContext); var context = AssessmentHelper.GetEnvironmentContext( telemetryManager, accessToken, ConfigHelper.ResourceManagerEndpoint, taskDefinition.SubscriptionId); var assessmentService = new AssessmentService(); var assessmentReport = await assessmentService.GenerateReportAsync( context, taskDefinition.TargetRegion); var reportId = DataAccess.SaveReport( taskDefinition.TenantId, taskDefinition.UserObjectId, assessmentReport.ReportFilePath); File.Delete(assessmentReport.ReportFilePath); TelemetryHelper.LogInformation( FormattableString.Invariant($"Saved report {reportId} to database and storage account."), telemetryContext); DataAccess.UpdateTaskStatusCompleted( taskDefinition.Id, reportId); TokenStore.Instance.RemoveTokenWrapperByTaskId(taskDefinition.Id); TelemetryHelper.LogInformation( @"Updated task status to completed.", telemetryContext); TelemetryHelper.WriteEvent( TelemetryEventNames.TaskEnd, telemetryContext); TelemetryHelper.LogVerbose(@"Telemetry flush.", telemetryContext); TelemetryHelper.Flush(); var flushWaitingTime = TimeSpan.FromSeconds(60); TelemetryHelper.LogVerbose( FormattableString.Invariant($"Waiting flush for {flushWaitingTime}."), telemetryContext); Thread.Sleep(flushWaitingTime); }
public void Start(AssessmentTask taskDefinition) { var telemetryContext = GetTelementryContext(taskDefinition); TelemetryHelper.WriteEvent( TelemetryEventNames.TaskStart, telemetryContext); TaskFactory.StartNew(() => this.StartAsync(taskDefinition, telemetryContext) .GetAwaiter().GetResult()); }
private async Task StartAsync( AssessmentTask taskDefinition, TelemetryContext telemetryContext) { TelemetryHelper.LogVerbose(@"Task starting.", telemetryContext); var accessToken = TokenStore.Instance.GetTokenByTaskId(taskDefinition.Id); if (accessToken == null) { return; } DataAccess.UpdateTaskStatusProcessing(taskDefinition.Id); TelemetryHelper.LogInformation(@"Updated task status to processing.", telemetryContext); try { await this.ProcessTaskAsync(taskDefinition, telemetryContext, accessToken); } catch (Exception ex) { string failedReason; if (ex is AssessmentException assessmentException) { failedReason = $"Generate assessment report failed: {ex.Message}"; } else if (ex is ResourceException resourceException) { failedReason = ex.Message; } else { failedReason = "Generate assessment report failed"; } TelemetryHelper.LogError(failedReason, ex, telemetryContext); DataAccess.UpdateTaskStatusFailed( taskDefinition.Id, failedReason, ex); TokenStore.Instance.RemoveTokenWrapperByTaskId(taskDefinition.Id); TelemetryHelper.LogInformation(@"Updated task status to failed.", telemetryContext); } }
private static TelemetryContext GetTelementryContext( AssessmentTask taskDefinition) { return(new TelemetryContext { TenantId = taskDefinition.TenantId, UserObjectId = taskDefinition.UserObjectId, Properties = new Dictionary <string, string> { { "TaskId", taskDefinition.Id.ToString(CultureInfo.InvariantCulture) }, { "SubscriptionId", taskDefinition.SubscriptionId }, { "TargetRegion", taskDefinition.TargetRegion }, } }); }