コード例 #1
0
        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);
            }
        }
コード例 #2
0
 private void ArchiveWorker(Worker worker)
 {
     worker.Thread = null;
     worker.BlobJob.StorageAccountKey = null;
     ExecuteOnUI(() =>
     {
         Workers.Remove(worker);
         WorkersHistory.Add(worker);
     });
 }
コード例 #3
0
        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();
        }
コード例 #4
0
 public void CancelWorker(Worker worker)
 {
     try
     {
         worker.Thread.Abort();
     }
     catch { }
     ExecuteOnUI(() =>
     {
         Workers.Remove(worker);
     });
 }