private void CreateWorker(BlobJob job) { var worker = new Worker() { BlobJob = job, SizeInBytes = job.SizeInBytes, }; ExecuteOnUI(() => { Workers.Add(worker); }); try { worker.Thread = new Thread(new ThreadStart(() => { worker.Start = DateTime.Now; try { var storageAccount = CloudStorageAccount.Parse(string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", job.StorageAccount, job.StorageAccountKey)); var blobClient = storageAccount.CreateCloudBlobClient(); //var serviceProperties = blobClient.GetServiceProperties(); //if (!"2012-02-12".Equals(serviceProperties.DefaultServiceVersion)) //{ // serviceProperties.DefaultServiceVersion = "2012-02-12"; // blobClient.SetServiceProperties(serviceProperties); //} var containerReference = blobClient.GetContainerReference(job.Container); containerReference.CreateIfNotExists(); var blobReference = containerReference.GetBlockBlobReference(job.BlobName); try { if (System.IO.File.Exists(job.File.FullFilePath)) { blobReference.FetchAttributes(); var fileInfo = new FileInfo(job.File.FullFilePath); if (blobReference.Properties.Length == fileInfo.Length) { worker.Message = "Same file already exists"; worker.Finish = DateTime.Now; ArchiveWorker(worker); return; } } } catch { } if (job.SizeInBytes > 1 * 1024 * 1024) { var blobTransferHelper = new BlobTransferHelper(); blobTransferHelper.TransferCompleted += (sender, e) => { if (!string.IsNullOrEmpty(job.ContentType) && (job.JobType == BlobJobType.Upload || job.JobType == BlobJobType.SetMetadata)) { blobReference.Properties.ContentType = job.ContentType; blobReference.SetProperties(); } worker.Message = "Success"; worker.Finish = DateTime.Now; ArchiveWorker(worker); }; blobTransferHelper.TransferProgressChanged += (sender, e) => { worker.TransferedInBytes = e.BytesSent; worker.SpeedInBytes = e.Speed; worker.TimeRemaining = TimeSpan.FromSeconds(Convert.ToInt32(e.TimeRemaining.TotalSeconds)); worker.SizeInBytes = e.TotalBytesToSend; }; switch (job.JobType) { case BlobJobType.Upload: blobTransferHelper.UploadBlobAsync(blobReference, job.File.FullFilePath); break; case BlobJobType.Download: Directory.CreateDirectory(Path.GetDirectoryName(job.File.FullFilePath)); blobTransferHelper.DownloadBlobAsync(blobReference, job.File.FullFilePath); break; } } else { switch (job.JobType) { case BlobJobType.Upload: blobReference.UploadFromFileAsync(job.File.FullFilePath, FileMode.OpenOrCreate); break; case BlobJobType.Download: Directory.CreateDirectory(Path.GetDirectoryName(job.File.FullFilePath)); blobReference.DownloadToFileAsync(job.File.FullFilePath, FileMode.OpenOrCreate); break; } if (!string.IsNullOrEmpty(job.ContentType) && (job.JobType == BlobJobType.Upload || job.JobType == BlobJobType.SetMetadata)) { blobReference.Properties.ContentType = job.ContentType; blobReference.SetProperties(); } worker.Message = "Success"; worker.TransferedInBytes = worker.SizeInBytes; worker.Finish = DateTime.Now; worker.SpeedInBytes = worker.SizeInBytes / (worker.Finish - worker.Start).TotalSeconds; ArchiveWorker(worker); } } catch (ThreadAbortException) { worker.Message = "Cancelled"; worker.Finish = DateTime.Now; ArchiveWorker(worker); } catch (Exception e) { worker.Message = "Error"; worker.ErrorMessage = string.Format("{0}", e.ToString()); worker.Finish = DateTime.Now; ArchiveWorker(worker); OnJobError(job, e); } })); worker.Thread.Start(); } catch (Exception e) { worker.Message = "Error"; worker.ErrorMessage = string.Format("{0}", e.ToString()); ArchiveWorker(worker); OnJobError(job, e); } }
private void OnJobError(BlobJob job, Exception e) { var stringBuilder = new StringBuilder(); stringBuilder.AppendFormat("{1:yyyy-MM-dd HH:mm:ss} Error!{0}", Environment.NewLine, DateTime.Now); stringBuilder.AppendFormat(" Job Type: {1}{0}", Environment.NewLine, job.JobType); stringBuilder.AppendFormat(" Account: {1}{0}", Environment.NewLine, job.StorageAccount); stringBuilder.AppendFormat(" Container: {1}{0}", Environment.NewLine, job.Container); stringBuilder.AppendFormat(" Blob: {1}{0}", Environment.NewLine, job.BlobName); stringBuilder.AppendFormat(" File: {1}{0}", Environment.NewLine, job.File.FullFilePath); stringBuilder.AppendFormat(" Exception: {1}{0}{0}", Environment.NewLine, e.ToString().Replace(Environment.NewLine, Environment.NewLine + " ")); OnOnError(stringBuilder.ToString()); }
private void CreateFakeWorker(BlobJob BlobJob) { var worker = new Worker() { BlobJob = BlobJob, SizeInBytes = BlobJob.SizeInBytes, }; ExecuteOnUI(() => { Workers.Add(worker); }); var random = new Random(); worker.SizeInBytes = (Convert.ToInt64(1000000 * random.NextDouble()) + 1000000); var backgroundWorker = new Thread(new ThreadStart(() => { while (worker.TransferedInBytes < worker.SizeInBytes) { ExecuteOnUI(() => { worker.SpeedInBytes = random.Next(100000); worker.TransferedInBytes += worker.SpeedInBytes; worker.TimeRemaining = TimeSpan.FromSeconds((worker.SizeInBytes - worker.TransferedInBytes) / worker.SpeedInBytes); }); Thread.Sleep(1000); } worker.Message = "Success"; ArchiveWorker(worker); })); backgroundWorker.Start(); }