public void AddImport(ImportLogModel model) { if (model.Quantity == 0) { throw new ArgumentException("Số lượng phải lớn hơn 0!"); } }
private async Task ImportNodeSets(Client apiClient, List <ImportOPCModel> importRequest) { ImportLogModel status = null; if (importRequest.Any()) { var orderedImportRequest = importRequest?.Count == 1 ? importRequest : OrderImportsByDependencies(importRequest); do { var nextBatch = orderedImportRequest.Take(1).ToList(); ResultMessageWithDataModel result = null; try { result = await apiClient.ImportAsync(nextBatch); Assert.True(result.IsSuccess, $"Failed to import nodesets: {result.Message}"); } catch (Exception ex) { Assert.True(false, $"Failed to import nodesets: {ex.Message}"); } var statusModel = new MyNamespace.IdIntModel { Id = Convert.ToInt32(result.Data), }; var sw = Stopwatch.StartNew(); do { System.Threading.Thread.Sleep(5000); status = await apiClient.GetByIDAsync(statusModel); } while (sw.Elapsed < TimeSpan.FromMinutes(15) && ((int)status.Status == (int)CESMII.ProfileDesigner.Common.Enums.TaskStatusEnum.InProgress || (int)status.Status == (int)CESMII.ProfileDesigner.Common.Enums.TaskStatusEnum.NotStarted)); // Ignore import failure: will be reported on export validation if ((int?)(status?.Status) != (int)CESMII.ProfileDesigner.Common.Enums.TaskStatusEnum.Completed) { var errorText = $"Error importing nodeset {nextBatch.FirstOrDefault().FileName}: {status.Messages.FirstOrDefault().Message}"; output.WriteLine(errorText); Assert.True(false, errorText); } //Assert.True((int?)(status?.Status) == (int)CESMII.ProfileDesigner.Common.Enums.TaskStatusEnum.Completed); //Assert.True(!status?.Messages?.Any()); orderedImportRequest.RemoveRange(0, nextBatch.Count); } while (orderedImportRequest.Any()); } }
public async Task <int> CallSlowMethod(List <ImportOPCModel> nodeSetXmlList, UserToken userToken) { //create a start log item var fileNames = string.Join(", ", nodeSetXmlList.Select(f => f.FileName).ToArray <string>()); //the rest of the fields are set in the dal ImportLogModel logItem = new ImportLogModel() { FileList = nodeSetXmlList.Select(f => f.FileName).ToArray <string>(), Messages = new List <ImportLogMessageModel>() { new ImportLogMessageModel() { Message = $"The import is processing for files {fileNames}..." } } }; var logId = await _dalImportLog.Add(logItem, userToken); //slow task - kick off in background //_backgroundWorkerQueue.QueueBackgroundWorkItem(async token => _ = Task.Run(async() => { //wrap in scope so that we don't lose the scope of the dependency injected objects once the //web api request completes and disposes of the import service object (and its module vars) using (var scope = _serviceScopeFactory.CreateScope()) { var dalImportLog = scope.ServiceProvider.GetService <IDal <ImportLog, ImportLogModel> >(); await Task.Delay(20000); //await Task.Delay(5000); //completed message var logItem = dalImportLog.GetById(logId.Value, userToken); logItem.Status = TaskStatusEnum.Completed; logItem.Completed = DateTime.UtcNow; logItem.Messages.Add(new ImportLogMessageModel() { Message = "Completed" }); await dalImportLog.Update(logItem, userToken); } }); //return result async return(logId.Value); }
public async Task <int> ImportOpcUaNodeSet(List <ImportOPCModel> nodeSetXmlList, UserToken userToken) { //create a start log item var fileNames = string.Join(", ", nodeSetXmlList.Select(f => f.FileName).ToArray <string>()); //the rest of the fields are set in the dal ImportLogModel logItem = new ImportLogModel() { FileList = nodeSetXmlList.Select(f => f.FileName).ToArray <string>(), Messages = new List <ImportLogMessageModel>() { new ImportLogMessageModel() { Message = $"Starting..." } } }; var logId = await _dalImportLog.Add(logItem, userToken); Task backgroundTask = null; //slow task - kick off in background //_backgroundWorkerQueue.QueueBackgroundWorkItem(async token => _ = Task.Run(async() => { //kick off the importer //wrap in scope in the internal method so that we don't lose the scope of the dependency injected objects once the //web api request completes and disposes of the import service object (and its module vars) try { backgroundTask = ImportOpcUaNodeSetInternal(nodeSetXmlList, logId.Value, userToken); await backgroundTask; } catch (Exception ex) { _logger.LogError(new EventId(), ex, "Unhandled exception in background importer."); } }); //return result async return(logId.Value); }