Ejemplo n.º 1
0
        /// <summary>
        /// Validate/Upload then delete the new dataSet. Email if there is an error
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void DatasetUploaded(object sender, FileSystemEventArgs e)
        {
            const int maximumRetryPeriod = 5;     //max retry for 5 minutes
            const int retryDelay         = 10000; //try every 10 seconds

            var fileReceived = DateTime.Now;

            if (e.FullPath.Contains(".csv"))
            {
                try
                {
                    while (true) //have to wait until the file is fully uploaded
                    {
                        if (FileUploadCompleted(e.FullPath))
                        {
                            //get the title
                            var fileDetail   = e.FullPath.Split('\\');
                            var schemaId     = Convert.ToInt32(fileDetail[fileDetail.Length - 2]);
                            var schemaDetail = _dataSetSchemaService.Get(schemaId);
                            var fileName     = fileDetail[fileDetail.Length - 1];

                            //Validate and upload the data
                            var data = new MediaAssetUploadModel
                            {
                                SchemaId = schemaId,
                                Title    = String.Format("{0} at {1} {2}", schemaDetail.Title, DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString())
                            };

                            var result = _uploaderService.SaveCsv(schemaId, data.Title, e.FullPath);

                            if (result.Errors.Count > 0)
                            {
                                var email = BuildFailedUploadEmail(result, fileName, schemaDetail.Category.Title, schemaDetail.Title);
                                AddDebugInfo(new DebugInfo(String.Format("Category title:{0}, Schema title:{1}. Automatic csv upload failed as the csv had invalid data entered. File: {2} . Errors: {3}", schemaDetail.Category.Title, schemaDetail.Title, fileName, email.Length > 3500 ? email.Substring(0, 3500) : email), DebugInfoTypeEnum.FolderWatchTriggered));
                                SendEmail(schemaDetail.OwnerEmail, email, false, String.Format("Category title:{0}, Schema title:{1}. Automatic csv upload failed as the csv had invalid data entered", schemaDetail.Category.Title, schemaDetail.Title));
                            }
                            else
                            {
                                AddDebugInfo(new DebugInfo(String.Format(@"Category title:{0}, Schema title:{1}. Dataset was successfully added to datashare. Title: {2}", schemaDetail.Category.Title, schemaDetail.Title, data.Title), DebugInfoTypeEnum.FolderWatchTriggered));
                            }
                            break;
                        }
                        // Calculate the elapsed time and stop if the maximum retry period has been reached.
                        var timeElapsed = DateTime.Now - fileReceived;
                        if (timeElapsed.TotalMinutes > maximumRetryPeriod)
                        {
                            break;
                        }
                        Thread.Sleep(retryDelay);
                    }
                }
                catch (Exception ex)
                {
                    AddDebugInfo(new DebugInfo(String.Format(@"Error uploading dataset: {0}", ex.Message), DebugInfoTypeEnum.Error), ex);
                    RestartService("DataShare.Service", 10000);
                }
            }
        }
Ejemplo n.º 2
0
      public ActionResult SaveCsv(int schemaId, string title, string fileName)
      {
          var filePath = Path.Combine(_systemConfigurationService.AppSettings("MediaAssetFolder"), fileName);
          var result   = _uploaderService.SaveCsv(schemaId, title, filePath);

          result.Errors = result.Errors.Take(100).ToList();

          return(Json(new { result.Errors }));
      }