public override void Run() { Trace.TraceInformation("Listening for queue messages..."); while (true) { try { // retrieve a new message from the queue CloudQueueMessage msg = this.queue.GetMessage(); if (msg != null) { // parse message retrieved from queue var messageParts = msg.AsString.Split(new char[] { ',' }); var imageBlobUri = messageParts[0]; var partitionKey = messageParts[1]; var rowkey = messageParts[2]; Trace.TraceInformation("Processing image in blob '{0}'.", imageBlobUri); string thumbnailBlobUri = System.Text.RegularExpressions.Regex.Replace(imageBlobUri, "([^\\.]+)(\\.[^\\.]+)?$", "$1-thumb$2"); CloudBlob inputBlob = this.container.GetBlobReference(imageBlobUri); CloudBlob outputBlob = this.container.GetBlobReference(thumbnailBlobUri); using (BlobStream input = inputBlob.OpenRead()) using (BlobStream output = outputBlob.OpenWrite()) { this.ProcessImage(input, output); // commit the blob and set its properties output.Commit(); outputBlob.Properties.ContentType = "image/jpeg"; outputBlob.SetProperties(); // update the entry in table storage to point to the thumbnail GuestBookDataSource ds = new GuestBookDataSource(); ds.UpdateImageThumbnail(partitionKey, rowkey, thumbnailBlobUri); // remove message from queue this.queue.DeleteMessage(msg); Trace.TraceInformation("Generated thumbnail in blob '{0}'.", thumbnailBlobUri); } } else { System.Threading.Thread.Sleep(1000); } } catch (StorageClientException e) { Trace.TraceError("Exception when processing queue item. Message: '{0}'", e.Message); System.Threading.Thread.Sleep(5000); } } }
public override void Run() { // This is a sample worker implementation. Replace with your logic. Trace.TraceInformation("GuestBook_WorkerRole entry point is called", "Information"); while (true) { try { // TODO: Retrieve a new message from the queue CloudQueueMessage newMessage = _queueStorage.GetNewMessage(); if (newMessage != null) { var newMessageParts = newMessage.AsString.Split(new [] {','}); var imageBlobName = newMessageParts[0]; var partitionKey = newMessageParts[1]; var rowKey = newMessageParts[2]; Trace.TraceInformation("Processing image in blob '{0}'", imageBlobName); string thumbnailName = System.Text.RegularExpressions.Regex.Replace(imageBlobName, "([^\\.]+)(\\.[^\\.]+)?$", "$1-thumb$2"); Trace.TraceInformation("Generated Thumbnail name = '{0}'", thumbnailName); CloudBlockBlob inputBlob = _blobStorage.GetBlockBlobReference(imageBlobName); CloudBlockBlob outputBlob = _blobStorage.GetBlockBlobReference(thumbnailName); using(Stream inputStream = inputBlob.OpenRead()) using (Stream outputStream = outputBlob.OpenWrite()) { processImage(inputStream, outputStream); outputBlob.Properties.ContentType = inputBlob.Properties.ContentType; string outputBlobUri = outputBlob.Uri.ToString(); // Update related Guestbook entry in the table storage to point to the thumbnail's Uri GuestBookDataSource guestBookDataSource = new GuestBookDataSource(); guestBookDataSource.UpdateImageThumbnail(partitionKey, rowKey, outputBlobUri); // Remove the message from queue _queueStorage.DeleteMessage(newMessage); Trace.TraceInformation("Generated thumbnail in blob '{0}'.", outputBlobUri); } } } catch (Exception exception) { Trace.TraceError("Exception when processing queue item. Message: '{0}'", exception.Message); Thread.Sleep(5000); } } }