Esempio n. 1
0
        protected static CloudQueueMessage CreateJobQueueMessage(TestMsg message)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(message.GetType());
            MemoryStream memoryStream             = new MemoryStream();

            serializer.WriteObject(memoryStream, message);

            return(new CloudQueueMessage(System.Text.Encoding.Default.GetString(memoryStream.GetBuffer())));
        }
Esempio n. 2
0
        public async Task Start(string[] args)
        {
            // Parse the arguments.
            if (!ParseArguments(args, out uint levelOfConcurrency))
            {
                // If invalid arguments were provided, exit the role.
                return;
            }

            // Wait for a message to appear on the job queue.
            while (true)
            {
                Console.WriteLine("Awaiting a job to perform.");

                CloudQueueMessage rawMessage = await jobQueue_.GetMessageAsync();

                while (rawMessage == null)
                {
                    await Task.Delay(TimeSpan.FromSeconds(1));

                    rawMessage = await jobQueue_.GetMessageAsync();
                }
                TestMsg message = null;
                try
                {
                    message = ConvertQueueMessage(rawMessage);
                }
                catch (Exception ex)
                {
                    // Upon failure to deserialize, log and throw the invalid message away.
                    Console.WriteLine($"[ERROR] Failed to deserialize message.  Details: {ex.Message}");
                    continue;
                }
                finally
                {
                    await jobQueue_.DeleteMessageAsync(rawMessage);
                }
                Console.WriteLine("Message received.");

                CloudBlobContainer container = blobClient_.GetContainerReference(message.Operation.ContainerName);
                CloudBlockBlob     blob      = container.GetBlockBlobReference(message.Operation.BlobName);

                TestOperation operation = message.Operation;

                if (operation is PutBlobOperation || operation is GetBlobOperation)
                {
                    // Define the task to be performed.
                    Func <Task <TimeSpan> > executeOperation = null;

                    if (operation is PutBlobOperation)
                    {
                        var detailedOperation = operation as PutBlobOperation;

                        executeOperation = () => { return(UploadBlocksAsync(blobClient_, container, detailedOperation.BlobName, detailedOperation.BlockSizeBytes, detailedOperation.NumBlocks, detailedOperation.StartingBlockId, levelOfConcurrency)); };
                    }
                    else if (operation is GetBlobOperation)
                    {
                        var detailedOperation = operation as GetBlobOperation;

                        executeOperation = () => { return(GetBlocksAsync(blobClient_, container, detailedOperation.BlobName, detailedOperation.StartIndex, detailedOperation.LengthBytes, detailedOperation.ChunkSizeBytes, levelOfConcurrency)); };
                    }


                    // Perform the task and report corresponding metrics.
                    DateTimeOffset startTime = DateTimeOffset.UtcNow;

                    StatusMsg statusMessage = null;
                    try
                    {
                        TimeSpan duration = await executeOperation();

                        statusMessage = new StatusMsg(operation.ID, true, "NA", startTime, duration);
                    }
                    catch (Exception ex)
                    {
                        statusMessage = new StatusMsg(operation.ID, false, ex.Message, startTime, DateTimeOffset.UtcNow - startTime);
                    }
                    finally
                    {
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(StatusMsg));
                        MemoryStream memoryStream             = new MemoryStream();
                        serializer.WriteObject(memoryStream, statusMessage);
                        string messageStr = System.Text.Encoding.Default.GetString(memoryStream.GetBuffer());
                        messageStr = messageStr.Substring(0, (int)memoryStream.Length);
                        await statusQueue_.AddMessageAsync(new CloudQueueMessage(messageStr));
                    }
                }
                else
                {
                    Console.WriteLine($"Operation for {message.GetType().FullName} currently unsupported.");
                }
            }
        }