internal static Task<WebResponse> GetResponseAsyncWithTimeout(this WebRequest req, CloudQueueClient service, TimeSpan? timeout)
        {
            Task<WebResponse> serverTask = req.GetResponseAsync(service);

            Task<WebResponse> wrappedTask = TimeoutHelper.GetTimeoutWrappedTask(timeout, serverTask);
            return wrappedTask;
        }
 private static Task<WebResponse> GetResponseAsync(this WebRequest req, CloudQueueClient service)
 {
     return new APMTask<WebResponse>(
         req.BeginGetResponse,
         (asyncresult) => service.EndGetResponse(asyncresult, req),
         req.Abort);
 }
예제 #3
0
 public TableStorageQueueHelper()
     : base()
 {
     _queueClient = base.StorageAccount.CreateCloudQueueClient();
 }
예제 #4
0
        /// <summary>
        /// Initializes all required resources by the AutoSclaer
        /// </summary>
        public void Initialize(IDictionary <string, string> resourceDetails, IAutoScalerPolicy policy)
        {
            if (resourceDetails == null)
            {
                throw new ArgumentException("Argument 'resourceDetails' cannot be null, missing properties for initialization in AutoScaler!");
            }

            //
            // Read configuration settings
            //
            _defaultBatchId = resourceDetails[Constants.QUEUE_HANDLER_INIT_PROP_DEFAULT_BATCH_ID];
            _storageConn    = resourceDetails[Constants.QUEUE_HANDLER_INIT_PROP_STORAGECONNECTION];
            _serviceBusConn = resourceDetails[GlobalConstants.SERVICEBUS_INTERNAL_CONNECTIONSTRING_CONFIGNAME];
            _checkForJobProcessorCommandsIntervalInSeconds = int.Parse(resourceDetails[GlobalConstants.GERES_CONFIG_AUTOSCALER_CHECKFORJOBHOSTUPDATES_INTERVAL]);

            if (string.IsNullOrEmpty(_defaultBatchId) || string.IsNullOrEmpty(_storageConn))
            {
                throw new ArgumentException("Missing properties in resourceDetails parameter!");
            }

            //
            // Check the loaded policy
            //
            if (policy == null)
            {
                throw new ArgumentException("Missing property for autoscaler policy!");
            }
            _policy = policy;

            //
            // Create all required repositories
            //
            _jobHostRepository = RepositoryFactory.CreateJobHostRepository(_storageConn, RoleEnvironment.DeploymentId);
            _batchRepository   = RepositoryFactory.CreateBatchRepository(_storageConn);
            _roleOpsRepository = RepositoryFactory.CreateRoleOperationStatusRepository(_storageConn);

            //
            // Connect to storage for the jobs-queue
            //
            var cloudAccount = CloudStorageAccount.Parse(_storageConn);

            _cloudQueueClient = cloudAccount.CreateCloudQueueClient();
            _defaultQueue     = _cloudQueueClient.GetQueueReference(_defaultBatchId);
            _defaultQueue.CreateIfNotExists();

            //
            // Connect to the service bus for autoscaler commands
            //
            _jobHostServiceBus = new JobHostServiceBus(_serviceBusConn, GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORAUTOSCALER,
                                                       GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORJOBHOST);
            var subscribeToJobHostCommandsTask = Task.Run(() =>
            {
                SubscriptionClient client = null;

                while (true)
                {
                    try
                    {
                        if (client == null || client.IsClosed)
                        {
                            client = _jobHostServiceBus.CreateSubscription(GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORAUTOSCALER,
                                                                           GlobalConstants.SERVICEBUS_INTERNAL_SUBSCRIPTION_JOBHOSTUPDATES);
                        }

                        var msg = client.Receive(TimeSpan.FromSeconds(_checkForJobProcessorCommandsIntervalInSeconds));
                        if (msg != null)
                        {
                            if (ProcessCommandFromJobHost(msg))
                            {
                                msg.Complete();
                            }
                            else
                            {
                                msg.Abandon();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        client = null;
                        Geres.Diagnostics.GeresEventSource.Log.AutoScalerWorkerAutoScalerListenForJobHostUpdatesFailed
                        (
                            RoleEnvironment.CurrentRoleInstance.Id, RoleEnvironment.DeploymentId,
                            string.Format("Topic: {0}, Subscription: {1}", GlobalConstants.SERVICEBUS_INTERNAL_SUBSCRIPTION_JOBHOSTUPDATES,
                                          GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_COMMANDSFORAUTOSCALER),
                            ex.Message,
                            ex.StackTrace
                        );
                    }
                }
            });
        }
예제 #5
0
        static async Task <int> Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            dbConnectionString      = configuration.GetConnectionString("PlaceboDatabase");
            storageConnectionString = configuration["storageConnectionString"];
            trainingQueueName       = configuration["TrainingQueueName"];
            var shortDBConnectionString      = dbConnectionString.Substring(0, 50);
            var shortStorageConnectionString = storageConnectionString.Substring(0, 50);

            Console.WriteLine($"Database connection string = {shortDBConnectionString}");
            Console.WriteLine($"Storage connection string = {shortStorageConnectionString}");

            Console.WriteLine($"Training Queue Name = {trainingQueueName}");

            var rootCommand = new RootCommand
            {
                new Option <string>(
                    "--documentFormat",
                    description: "The document format for which the training assets should be uploaded"),

                new Option <string>(
                    "--localPath",
                    description: "The local folder containing the training assets"),
                new Option <string>(
                    "--blobContainer",
                    description: "The name of the blob container where the assets should be uploaded"),
                new Option <string>(
                    "--blobContainerFolder",
                    getDefaultValue: () => null,
                    description: "The anme of a folder within the blob container where the assets should be uploaded"),
            };

            rootCommand.Description = "This command uploads a set of model training assets for a document format (e.g. phoenix) from a local directory to Azure blob storage.  " +
                                      "This triggers a model training run in azure.  A new model is created based on the assets and a record of the new model is kept in the ModelTraining table in the " +
                                      "database.  This new model becomes the latest model for that document format and is then used by the rcognizer component while processing future documents";
            try
            {
                rootCommand.Handler = CommandHandler.Create <string, string, string, string>(async(documentFormat, localPath, blobContainer, blobContainerFolder) =>
                {
                    try
                    {
                        Console.WriteLine($"The value for --documentFormat is: {documentFormat}");
                        if (string.IsNullOrEmpty(documentFormat))
                        {
                            throw new Exception($"--documentFormat {documentFormat} must be provided");
                        }

                        Console.WriteLine($"The value for --localPath is: {localPath}");
                        if (string.IsNullOrEmpty(localPath))
                        {
                            throw new Exception($"--localPath {localPath} must be provided");
                        }

                        Console.WriteLine($"The value for --blobContainer is: {blobContainer}");
                        if (string.IsNullOrEmpty(blobContainer))
                        {
                            throw new Exception($"--blobContainer {blobContainer} must be provided");
                        }

                        Console.WriteLine($"The value for --blobContainerFolder is: {blobContainerFolder}");
                        if (string.IsNullOrEmpty(blobContainerFolder))
                        {
                            throw new Exception($"--blobContainerFolder {blobContainerFolder} must be provided");
                        }

                        if (!Directory.Exists(localPath))
                        {
                            throw new Exception($"--localPath {localPath} does not exist or is not a directory");
                        }

                        // Get hold of the storage account
                        CloudStorageAccount storageAccount = null;
                        try
                        {
                            storageAccount = CloudStorageAccount.Parse(storageConnectionString);

                            var targetBlobClient = storageAccount.CreateCloudBlobClient();
                            var targetContainer  = targetBlobClient.GetContainerReference(blobContainer);
                            await targetContainer.CreateIfNotExistsAsync();
                            var directory = targetContainer.GetDirectoryReference(blobContainerFolder);

                            BlobResultSegment resultSegment         = null;
                            BlobContinuationToken continuationToken = null;

                            do
                            {
                                resultSegment = await directory.ListBlobsSegmentedAsync(true, BlobListingDetails.All, 50, continuationToken, null, null);
                                if (resultSegment.Results.Count() > 0)
                                {
                                    Console.WriteLine($"Container already contains {resultSegment.Results.Count()} blobs - they will be deleted");
                                }
                                foreach (var blob in resultSegment.Results)
                                {
                                    try
                                    {
                                        var blobToDelete = directory.GetBlockBlobReference(blob.Uri.ToString());
                                        await blobToDelete.DeleteIfExistsAsync();
                                        Console.WriteLine($"Deleted blob: {blobToDelete.Name}");
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("Unable to delete blob {blob.Uri.ToString()}");
                                    }
                                }

                                // Get the continuation token. If not null, get the next segment.
                                continuationToken = resultSegment.ContinuationToken;
                            } while (continuationToken != null);

                            string[] fileEntries  = Directory.GetFiles(localPath);
                            Stopwatch innnerTimer = new Stopwatch();
                            Stopwatch outerTimer  = new Stopwatch();
                            outerTimer.Start();
                            int i = 0;
                            foreach (string fileName in fileEntries)
                            {
                                FileInfo f = new FileInfo(fileName);
                                innnerTimer.Reset();
                                innnerTimer.Start();
                                using FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                                var blobToUpload    = directory.GetBlockBlobReference(f.Name);
                                await blobToUpload.UploadFromStreamAsync(fs);
                                i++;
                                innnerTimer.Stop();

                                Console.WriteLine($"Uploaded file {f.Name} to container {targetContainer.Name} in {innnerTimer.ElapsedMilliseconds} ms");
                            }
                            outerTimer.Stop();
                            Console.WriteLine($"Uploaded {i} files to container {targetContainer.Name} in {outerTimer.ElapsedMilliseconds} ms");
                            var policy = new SharedAccessBlobPolicy
                            {
                                Permissions            = SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Read,
                                SharedAccessStartTime  = DateTime.UtcNow.AddMinutes(-15),
                                SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(120)
                            };

                            var targetContainerToken = targetContainer.GetSharedAccessSignature(policy);
                            var targetContainerSAS   = string.Format("{0}{1}", targetContainer.Uri, targetContainerToken);
                            Console.WriteLine($"targetContainerSAS={targetContainerSAS}");

                            TrainingRequestMessage trainingRequestMessage = new TrainingRequestMessage
                            {
                                BlobFolderName    = blobContainerFolder,
                                BlobSasUrl        = targetContainerSAS,
                                DocumentFormat    = documentFormat,
                                IncludeSubFolders = "false",
                                UseLabelFile      = "true"
                            };

                            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

                            // Retrieve a reference to a container.
                            CloudQueue queue = queueClient.GetQueueReference(trainingQueueName);

                            // Create the queue if it doesn't already exist
                            await queue.CreateIfNotExistsAsync();

                            CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(trainingRequestMessage));
                            await queue.AddMessageAsync(message);
                        }
                        catch (Exception e)
                        {
                            throw;
                        }


                        Console.WriteLine("done.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                                                                                             );
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(-1);
            }
            return(rootCommand.InvokeAsync(args).Result);
        }
예제 #6
0
 public HubClient(CloudQueueClient queueClient)
 {
     this.queueClient = queueClient;
 }
예제 #7
0
        private static CloudQueue CreateSdkQueue(CloudStorageAccount sdkAccount, string queueName)
        {
            CloudQueueClient sdkClient = sdkAccount.CreateCloudQueueClient();

            return(sdkClient.GetQueueReference(queueName));
        }
예제 #8
0
        /// <summary>
        /// Save Recent Activites emails to azure table
        /// </summary>
        public void GetRecentActivitiesEmailData()
        {
            Trace.TraceInformation("Worker Role RecentActivity start GetRecentActivitiesEmailData() {0}", DateTime.Now);

            try
            {
                List <int> objallcompany = objLeadNotifcationBusiness.GetAllActiveCompanies();
                foreach (int companyId in objallcompany)
                {
                    try
                    {
                        accountSetting = userBusiness.GetSendNotificationsActiveUsersId(companyId);
                    }
                    catch (Exception accountSettingExp)
                    {
                        hasError = true;
                        logdata.Append("\n");
                        logdata.Append(companyID + "," + " " + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + accountSettingExp.Message + ". Error Occured When Feching  User Account Setting Data");
                        continue;
                    }
                    try
                    {
                        foreach (var user in accountSetting)
                        {
                            WorkerRoleRecentActivity.CultureName = user.User.CultureInformation == null ? "en-US" : user.User.CultureInformation.CultureName;
                            WorkerRoleCommonFunctions.SetCurrentUserCulture();
                            WorkerRoleRecentActivity.UserId = user.UserId;
                            companyID = companyId;
                            WorkerRoleRecentActivity.TimeZoneOffSet = user.User.TimeZone == null ? "0" : user.User.TimeZone.offset.ToString();
                            int TotalRecords = 0;
                            List <SSP_GetRecentActivitesForEmailNotification_Result> RecentActivities = new List <SSP_GetRecentActivitesForEmailNotification_Result>();
                            try
                            {
                                RecentActivities = objLeadNotifcationBusiness.GetRecentActivitiesForNotification(ErucaCRM.Utility.ReadConfiguration.PageSize, companyId, user.UserId, ref TotalRecords);
                                AutoMapper.Mapper.Map(RecentActivities, RecentActivitiesModel);
                            }
                            catch (Exception exRecentActivities)
                            {
                                hasError = true;
                                logdata.Append("\n");
                                logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + exRecentActivities.Message + ". Error Occured When Feching Recent Activites Data");
                                continue;
                            }

                            objmailhelper = new ErucaCRM.Utility.MailHelper();
                            if (RecentActivities.Count > 0)
                            {
                                maxLeadAuditId = RecentActivities.Max(x => x.LeadAuditId);
                            }
                            foreach (var homemodel in RecentActivitiesModel)
                            {
                                try
                                {
                                    MailBodyTemplat    = WorkerRoleCommonFunctions.GetGlobalizedLabel("EmailTemplates", "RecentActivityEmailBody", WorkerRoleRecentActivity.CultureName);
                                    mailSubjectTemplat = WorkerRoleCommonFunctions.GetGlobalizedLabel("EmailTemplates", "RecentActivityEmailSubject", WorkerRoleRecentActivity.CultureName);
                                    objmailhelper.Body = objmailhelper.Body + string.Format(MailBodyTemplat, ErucaCRM.Utility.ReadConfiguration.OwnerDetail + Convert.ToInt32(homemodel.CreatedBy).Encrypt(), ErucaCRM.Utility.ReadConfiguration.NoImage, homemodel.LeadAuditId, homemodel.ActivityText, homemodel.ActivityCreatedTime);
                                    if (objmailhelper.Subject == null)
                                    {
                                        objmailhelper.Subject = string.Format(mailSubjectTemplat, TotalRecords);
                                    }
                                    leadAuditIds.Add(homemodel.LeadAuditId);
                                }
                                catch (Exception expEmailTemplate)
                                {
                                    hasError = true;
                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + expEmailTemplate.Message + ". Error Occured When Reading Config File (OwnerDetail|NoImage|) || Reading Culture File.(EmailTemplates(RecentActivityEmailBody|RecentActivityEmailSubject|))");
                                }
                            }
                            if (RecentActivitiesModel.Count > 0)
                            {
                                try
                                {
                                    objmailhelper.Body = "<div style='width: 450px'> <div style='width:100%;text-align:center'><img src='" + ErucaCRM.Utility.ReadConfiguration.SiteUrl + ErucaCRM.Utility.ReadConfiguration.WebsiteLogoPath + "'></div>" + objmailhelper.Body;

                                    if (TotalRecords > ErucaCRM.Utility.ReadConfiguration.PageSize)
                                    {
                                        objmailhelper.Body = objmailhelper.Body + " <div style='text-align: right; margin: 15px 0 0 0;'><a href='" + ErucaCRM.Utility.ReadConfiguration.PopUrl + "' style='text-decoration:none;background: none repeat scroll 0 0 #0798bc; width:300px;color: #fff; font-size: 9px; border: 1px solid #8db6e4; border-radius: 3px; cursor: pointer; margin: 14px 5px 13px; padding: 1px;'>" + WorkerRoleCommonFunctions.GetGlobalizedLabel("DashBoard", "ViewAll", WorkerRoleRecentActivity.CultureName) + "</a></div></div>";
                                    }
                                    //send email
                                    objmailhelper.ToAddress     = user.User.EmailId;
                                    objmailhelper.RecipientName = user.User.FirstName + " " + user.User.LastName;
                                    //objmailhelper.SendMailMessage(objmailhelper.ToAddress, objmailhelper.Subject, objmailhelper.Body);

                                    // Retrieve the storage account from the connection string.
                                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                                        CloudConfigurationManager.GetSetting("StorageConnectionString"));
                                    // Create the table client.
                                    CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
                                    // Create the table if it doesn't exist.
                                    CloudTable table = tableClient.GetTableReference("Message");
                                    table.CreateIfNotExists();
                                    // Create a new customer entity.

                                    Message message = new Message();
                                    message.ToAddress      = objmailhelper.ToAddress;
                                    message.Subject        = objmailhelper.Subject;
                                    message.Body           = objmailhelper.Body;
                                    message.ReciepientName = objmailhelper.RecipientName;
                                    var sendEmailRow = new SendEmail
                                    {
                                        PartitionKey     = message.ReciepientName,
                                        RowKey           = message.ToAddress,
                                        EmailAddress     = message.ToAddress,
                                        EmailSent        = false,
                                        MessageBody      = message.Body,
                                        ScheduledDate    = DateTime.Now,
                                        FromEmailAddress = ErucaCRM.Utility.ReadConfiguration.EmailForScheduler,
                                        SubjectLine      = message.Subject,
                                    };

                                    try
                                    {
                                        TableOperation insertOperation = TableOperation.InsertOrReplace(sendEmailRow);
                                        table.Execute(insertOperation);
                                        Trace.TraceInformation("Worker Role RecentActivity saved data in message table {0}", DateTime.Now);
                                    }
                                    catch (Exception ex)
                                    {
                                        string err = "Error creating SendEmail row:  " + ex.Message;
                                        if (ex.InnerException != null)
                                        {
                                            err += " Inner Exception: " + ex.InnerException;
                                        }
                                        Trace.TraceError(err);
                                    }

                                    string queueMessageString =
                                        sendEmailRow.PartitionKey + "," +
                                        sendEmailRow.RowKey + ",";
                                    // Create the queue client.
                                    CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                                    // Retrieve a reference to a queue.
                                    CloudQueue queue = queueClient.GetQueueReference("azuremailqueue");
                                    // Create the queue if it doesn't already exist.
                                    queue.CreateIfNotExists();
                                    var queueMessage = new CloudQueueMessage(queueMessageString);
                                    // Create a message and add it to the queue.
                                    CloudQueueMessage cloudMessage = new CloudQueueMessage("azuremailqueue");
                                    queue.AddMessage(queueMessage);
                                    Trace.TraceInformation("Worker Role RecentActivity saved data in queue table {0}", DateTime.Now);
                                    userBusiness.UpdateUserNotification(WorkerRoleRecentActivity.UserId, maxLeadAuditId);

                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Success.ToString() + "," + objmailhelper.Subject);
                                }
                                catch (System.Net.Mail.SmtpException expEmailSend)
                                {
                                    hasError = true;
                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + expEmailSend.Message + ". Error Occured When Sending Email || Reading Config File File(PopUrl|SiteUrl|WebsiteLogoPath|PageSize).");
                                }
                                catch (Exception expEmailSend)
                                {
                                    hasError = true;
                                    logdata.Append("\n");
                                    logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + expEmailSend.Message + ". Error Occured When Sending Email || Reading Config File File(PopUrl|SiteUrl|WebsiteLogoPath|PageSize).");
                                }
                            }
                        }
                    }
                    catch (Exception exeption)
                    {
                        hasError = true;
                        logdata.Append("\n");
                        logdata.Append(companyID + "," + WorkerRoleRecentActivity.UserId + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + exeption.Message);
                    }
                }
                SaveLogData();
            }
            catch (Exception exep)
            {
                logdata.Append("\n");
                logdata.Append("" + "," + "" + "," + ErucaCRM.Utility.Enums.ResponseResult.Failure.ToString() + "," + exep.Message + ". Error Occur When Feching All Company Data.Please Check Network Releated Information.");
                hasError = true;
                SaveLogData();
            }
        }
예제 #9
0
        public static async Task Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .AddEnvironmentVariables()
                                .Build();

            var connectionString = configuration["ConnectionString"];
            var qName            = configuration["MailQueueName"];

            if (connectionString == null ||
                qName == null)
            {
                throw new Exception("Please define ConnectionString and qName environment variables");
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
            CloudQueue          queue          = queueClient.GetQueueReference(qName);

            await queue.CreateIfNotExistsAsync();

            Console.WriteLine("FROM:");
            string toAddress = Console.ReadLine();

            Console.WriteLine("TO:");
            string fromAddress = Console.ReadLine();

            Console.WriteLine("Subject:");
            string subject = Console.ReadLine();

            MailMessage msg = new MailMessage();

            msg.To.Add(new MailAddress(toAddress));
            msg.From    = new MailAddress(fromAddress);
            msg.Subject = subject;

            Console.WriteLine("Message body (CTRL+Z to finish):");
            string        line;
            List <string> msgBody = new List <string>();

            do
            {
                line = Console.ReadLine();
                if (line != null)
                {
                    msgBody.Add(line);
                }
            } while (line != null);

            msg.Body = string.Join(Environment.NewLine, msgBody);

            SerializableMailMessage sermsg = new SerializableMailMessage();

            sermsg.Email = msg;

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.NewLineOnAttributes = true;

            var sw = new StringWriter();
            var xw = XmlWriter.Create(sw);

            sermsg.WriteXml(xw);
            xw.Flush();

            var xmlEmail = sw.ToString();

            await queue.AddMessageAsync(new CloudQueueMessage(xmlEmail));

            Console.WriteLine("Email queued for delivery.");
        }
예제 #10
0
 public QueueServices(IOptions <ConnectionStringsOptions> connectionStringsOptions)
 {
     _storageAccount = CloudStorageAccount.Parse(connectionStringsOptions.Value.BlobStorage);
     _queueClient    = _storageAccount.CreateCloudQueueClient();
 }
예제 #11
0
        public QueueManager()
        {
            var cloudStorageAccount = CloudStorageAccount.Parse(ConnectionString);

            _cloudQueueClient = cloudStorageAccount.CreateCloudQueueClient();
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Got GitHub Webhook.");

            string eventType = string.Empty;

            if (req.Headers.ContainsKey(EventType))
            {
                eventType = req.Headers[EventType];
                log.LogInformation($"Rec. event of type: {eventType}");
            }

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Request body: {requestBody}");

            // Establishing connectivity to queue
            var azureStorageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            CloudStorageAccount csAccount    = CloudStorageAccount.Parse(azureStorageConnectionString);
            CloudQueueClient    cQueueClient = csAccount.CreateCloudQueueClient();
            CloudQueue          cQueue       = cQueueClient.GetQueueReference("scheduledprsqueue");

            // this env var should have an xml body containing an RSA key
            var xmlGHPrivateKey = Environment.GetEnvironmentVariable("GitHubPrivateKey");
            var handler         = new GitHubEventHandlers(log, xmlGHPrivateKey, AppId);

            // deserialize the payload
            var payload = JsonConvert.DeserializeObject <WebhookPayload>(requestBody);

            bool isHuman = payload?.Sender?.Type == "User"; // don't respond in an infinite loop

            if (payload != null && isHuman)
            {
                log.LogDebug("Deserialized the payload.");

                try
                {
                    switch (eventType)
                    {
                    case PullRequestEvent:
                        var prresult = CheckPRHasCommand(payload);
                        if (prresult != null)
                        {
                            log.LogInformation($"Got PR with command: {prresult.BranchName} {prresult.MergeTime}");
                            log.LogInformation($"Message insert result: " + InsertMessageToQueue(cQueue, prresult, TimeSpan.FromMinutes(5)));
                        }
                        break;

                    case IssueCommentEvent:     // same event as a PR comment, need to check that this comment is made on a PR and not an issue
                        // do a thing, but differently
                        var result = CheckCommentHasCommand(payload);
                        if (result != null)
                        {
                            // ack to the comment
                            await handler.AckAddToQueueAsync(result);

                            await handler.BlockMergeAsync(result);

                            log.LogInformation($"Got comment with command: {result.BranchName} {result.MergeTime}");
                            log.LogInformation($"Message insert result: " + InsertMessageToQueue(cQueue, result, TimeSpan.FromMinutes(5)));
                        }

                        // debug, this should be done in QueueExecutor
                        // MergePRAsync(log, xmlGHPrivateKey, result).GetAwaiter().GetResult();
                        break;
                    }
                }
                catch (Exception e)
                {
                    log.LogInformation(e, "Caught exception when processing payload.");
                }
            }
            return(new OkResult());
        }
예제 #13
0
        //When pressing the button the program performs takes the users inputs for the flight, hotel and payment info and puts out the totalCost of the tickets and hotel
        protected void BtnGet_Click(object sender, EventArgs e)
        {
            //check to see so destinations are different places
            if (To.SelectedItem.Value == From.SelectedItem.Value)
            {
                Response.Write("<script>alert('Destinations needs to be different'); </script>");
            }
            else
            {
                try
                {
                    //connection to storage account
                    StorageCredentials  credentials    = new StorageCredentials(accountName, accountKey);
                    CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, useHttps: true);

                    CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();

                    //queues where information gets stored and read from
                    CloudQueue webQ     = cloudQueueClient.GetQueueReference("webqueue");
                    CloudQueue flightQ  = cloudQueueClient.GetQueueReference("flightqueue");
                    CloudQueue hotelQ   = cloudQueueClient.GetQueueReference("hotelqueue");
                    CloudQueue carQ     = cloudQueueClient.GetQueueReference("carqueue");
                    CloudQueue paymentQ = cloudQueueClient.GetQueueReference("paymentqueue");

                    double flightCost = 0;
                    double hotelCost  = 0;
                    double carCost    = 0;

                    //Create the queues if they do not exist already
                    webQ.CreateIfNotExists();
                    flightQ.CreateIfNotExists();
                    hotelQ.CreateIfNotExists();
                    carQ.CreateIfNotExists();
                    paymentQ.CreateIfNotExists();

                    //json objects for storing the data for the different services
                    JObject flightJson  = new JObject();
                    JObject hotelJson   = new JObject();
                    JObject carJson     = new JObject();
                    JObject paymentJson = new JObject();

                    //flight
                    //Store the data from the labels into the flight json
                    flightJson["from"] = From.SelectedValue;
                    flightJson["to"]   = To.SelectedValue;

                    //If labels are empty we just store a 0
                    int flightTemp;
                    if (infants.Text == "")
                    {
                        flightJson["infants"] = 0;
                    }
                    //else save the value into the flight json
                    else
                    {
                        if (int.TryParse(infants.Text, out flightTemp))
                        {
                            flightJson["infants"] = flightTemp;
                        }
                    }

                    if (children.Text == "")
                    {
                        flightJson["children"] = 0;
                    }
                    else
                    {
                        if (int.TryParse(children.Text, out flightTemp))
                        {
                            flightJson["children"] = flightTemp;
                        }
                    }

                    if (adults.Text == "")
                    {
                        flightJson["adults"] = 0;
                    }
                    else
                    {
                        if (int.TryParse(adults.Text, out flightTemp))
                        {
                            flightJson["adults"] = flightTemp;
                        }
                    }

                    if (seniorNbr.Text == "")
                    {
                        flightJson["oldies"] = 0;
                    }
                    else
                    {
                        if (int.TryParse(seniorNbr.Text, out flightTemp))
                        {
                            flightJson["oldies"] = flightTemp;
                        }
                    }

                    //convert the json to a String and save it into a CloudQueueMessage
                    CloudQueueMessage message = new CloudQueueMessage(flightJson.ToString());
                    //add that message to the flightQ
                    flightQ.AddMessage(message);

                    //get the message from webQ
                    message = webQ.GetMessage();
                    while (message == null)
                    {
                        message = webQ.GetMessage();
                    }

                    JObject jObject = (JObject)JsonConvert.DeserializeObject(message.AsString);
                    flightCost = (double)jObject["flightCost"];
                    //delete the message in webQ
                    webQ.DeleteMessage(message);


                    //hotelJson     Not needed for this application in lab3 so it is commented
                    //Store the data from the labels into the hotel json
                    //If labels are empty we just store a 0
                    if (String.IsNullOrEmpty(nights.Text))
                    {
                        paymentJson["hotelCost"] = 0;
                    }
                    else // else save the value into the hotel json
                    {
                        hotelJson["roomType"] = hotelRoom.SelectedValue;

                        int hotelTemp;
                        if (travellers.Text == "")
                        {
                            hotelJson["travellers"] = 0;
                        }
                        else
                        {
                            if (int.TryParse(travellers.Text, out hotelTemp))
                            {
                                hotelJson["travellers"] = hotelTemp;
                            }
                        }

                        if (nights.Text == "")
                        {
                            hotelJson["nights"] = 0;
                        }
                        else
                        {
                            if (int.TryParse(nights.Text, out hotelTemp))
                            {
                                hotelJson["nights"] = hotelTemp;
                            }
                        }

                        if (seniors.Text == "")
                        {
                            hotelJson["seniors"] = 0;
                        }
                        else
                        {
                            if (int.TryParse(seniors.Text, out hotelTemp))
                            {
                                hotelJson["seniors"] = hotelTemp;
                            }
                        }

                        if (primGuest.Text == "")
                        {
                            hotelJson["primGuest"] = "";
                        }
                        else
                        {
                            hotelJson["primGuest"] = primGuest.Text;
                        }

                        //convert the json to a String and save it into a CloudQueueMessage
                        message = new CloudQueueMessage(hotelJson.ToString());
                        //add that message to the hotelQ
                        hotelQ.AddMessage(message);

                        //get the message from webQ
                        message = webQ.GetMessage();
                        while (message == null)
                        {
                            message = webQ.GetMessage();
                        }

                        jObject   = (JObject)JsonConvert.DeserializeObject(message.AsString);
                        hotelCost = (double)jObject["hotelCost"];
                        //delete the message in webQ
                        webQ.DeleteMessage(message);
                    }

                    //Carcost
                    // modelYear, seats, age, fuelType

                    carJson["fuelType"] = fuelType.SelectedValue;

                    int carTemp;
                    if (seats.Text == "")
                    {
                        carJson["seats"] = 0;
                    }

                    else
                    {
                        if (int.TryParse(seats.Text, out carTemp))
                        {
                            carJson["seats"] = carTemp;
                        }
                    }

                    if (modelYear.Text == "")
                    {
                        carJson["modelYear"] = 0;
                    }

                    else
                    {
                        if (int.TryParse(modelYear.Text, out carTemp))
                        {
                            carJson["modelYear"] = carTemp;
                        }
                    }

                    if (driverAge.Text == "")
                    {
                        carJson["age"] = 0;
                    }

                    else
                    {
                        if (int.TryParse(driverAge.Text, out carTemp))
                        {
                            carJson["age"] = carTemp;
                        }
                    }

                    //convert the json to a String and save it into a CloudQueueMessage
                    message = new CloudQueueMessage(carJson.ToString());
                    //add that message to the hotelQ
                    carQ.AddMessage(message);

                    //get the message from webQ
                    message = webQ.GetMessage();
                    while (message == null)
                    {
                        message = webQ.GetMessage();
                    }

                    jObject = (JObject)JsonConvert.DeserializeObject(message.AsString);
                    carCost = (double)jObject["carCost"];
                    //delete the message in webQ
                    webQ.DeleteMessage(message);

                    //paymentJson
                    //Store the data from the labels into the hotel json
                    //if labels are empty store 0 or just an empty String to avoid null
                    int paymentTemp;
                    if (cardNbr.Text == "")
                    {
                        paymentJson["cardNbr"] = 0;
                    }
                    else
                    {
                        if (int.TryParse(cardNbr.Text, out paymentTemp))
                        {
                            paymentJson["cardNbr"] = paymentTemp;
                        }
                    }

                    if (cardHolder.Text == "")
                    {
                        paymentJson["cardHolder"] = "";
                    }
                    else
                    {
                        paymentJson["cardHolder"] = cardHolder.Text;
                    }

                    paymentJson["flightCost"] = flightCost;
                    paymentJson["hotelCost"]  = hotelCost;
                    paymentJson["carCost"]    = carCost;

                    //convert the json to a String and save it into a CloudQueueMessage
                    message = new CloudQueueMessage(paymentJson.ToString());
                    //add that message to the paymentQ
                    paymentQ.AddMessage(message);

                    //get the message from webQ
                    message = webQ.GetMessage();
                    while (message == null)
                    {
                        message = webQ.GetMessage();
                    }

                    jObject = (JObject)JsonConvert.DeserializeObject(message.AsString);
                    //delete the message in webQ
                    webQ.DeleteMessage(message);

                    //print out the totalCost for flight and hotel into the totalCost label
                    totalCost.Text = String.Format("{0:0.##}", jObject["totalCost"]);

                    //adds information to the database for flights
                    try
                    {
                        conn.Open();
                        //statement for the flights table
                        SqlCommand cmd = new SqlCommand(@"INSERT INTO dbo.flights (passportNbr, name, flightNbr, departureDate, price)
                                  VALUES (@passportNbr, @name, @flightNbr, @departureDate, @price)", conn);
                        if (passNumber.Text == "")
                        {
                            ;
                        }
                        else
                        {
                            cmd.Parameters.Add(new SqlParameter("passportNbr", Int32.Parse(passNumber.Text)));
                            cmd.Parameters.Add(new SqlParameter("name", cardHolder.Text));
                            cmd.Parameters.Add(new SqlParameter("flightNbr", Int32.Parse(flightNumber.Text)));
                            cmd.Parameters.Add(new SqlParameter("departureDate", departDate.Text));
                            cmd.Parameters.Add(new SqlParameter("price", float.Parse(totalCost.Text)));
                            cmd.ExecuteNonQuery();

                            conn.Close();
                        }
                    }
                    catch (SqlException ex)
                    {
                        Debug.WriteLine(ex.ToString());
                    }

                    //adds information to the database for customer
                    try
                    {
                        conn1.Open();
                        //statement for the customer table
                        SqlCommand cmd1 = new SqlCommand(@"INSERT INTO dbo.customer (cardNbr, name, expireDate, balance)
                                  VALUES (@cardNbr, @name, @expireDate, @balance)", conn);
                        if (cardHolder.Text == "")
                        {
                            ;
                        }
                        else
                        {
                            cmd1.Parameters.Add(new SqlParameter("cardNbr", cardNbr.Text));
                            cmd1.Parameters.Add(new SqlParameter("name", cardHolder.Text));
                            cmd1.Parameters.Add(new SqlParameter("expireDate", expireDate.Text));
                            cmd1.Parameters.Add(new SqlParameter("balance", float.Parse(balance.Text)));
                            cmd1.ExecuteNonQuery();

                            conn1.Close();

                            //Here the data for transaction and customers gets saved to the NoSQL MongoDB database
                            Mongo.Instance.SaveTransaction(new Transaction(DateTime.Now.ToString("M/d/yyyy"), Convert.ToInt32(cardNbr.Text), Convert.ToDouble(totalCost.Text)));
                            Mongo.Instance.SaveCustomer(new Customer(Convert.ToInt32(cardNbr.Text), cardHolder.Text, expireDate.Text, Convert.ToDouble(balance.Text)));
                        }
                        //listAirports.DataBind();
                        //GridView1.DataBind();
                    }
                    catch (SqlException ex)
                    {
                        Debug.WriteLine(ex.ToString());
                    }



                    //adds information to the database for bookings
                    try
                    {
                        conn2.Open();
                        //statement for the bookings table
                        SqlCommand cmd2 = new SqlCommand(@"INSERT INTO dbo.bookings (hotelCode, name, passportNbr, arrivalDate, nights, roomRate)
                                  VALUES (@hotelCode, @name, @passportNbr, @arrivalDate, @nights, @roomRate)", conn);
                        if (nights.Text == "")
                        {
                            ;
                        }
                        else
                        {
                            cmd2.Parameters.Add(new SqlParameter("hotelCode", hotelList.SelectedItem.Value));
                            cmd2.Parameters.Add(new SqlParameter("name", primGuest.Text));
                            cmd2.Parameters.Add(new SqlParameter("passportNbr", Int32.Parse(hotelPassNumber.Text)));
                            cmd2.Parameters.Add(new SqlParameter("arrivalDate", hotelDate.Text));
                            cmd2.Parameters.Add(new SqlParameter("nights", Int32.Parse(nights.Text)));
                            cmd2.Parameters.Add(new SqlParameter("roomRate", (float)hotelCost));
                            cmd2.ExecuteNonQuery();

                            conn2.Close();
                        }
                    }
                    catch (SqlException ex)
                    {
                        Debug.WriteLine(ex.ToString());
                    }
                }
                catch (Exception ee) { Debug.WriteLine(ee); }
            }
        }
예제 #14
0
 public void createClients()
 {
     cbClient = csAccount.CreateCloudBlobClient();
     cqClient = csAccount.CreateCloudQueueClient();
 }
예제 #15
0
 public DeploymentQueue(CloudQueueClient queueClient)
 {
     _queueClient = queueClient;
 }
예제 #16
0
 public AzureMessageQueueReceiver(IMessageEnvelopeUnwrapper unwrapper, CloudQueueClient client, QueueAddressGenerator addressGenerator)
 {
     this.unwrapper        = unwrapper;
     this.client           = client;
     this.addressGenerator = addressGenerator;
 }
예제 #17
0
        /// <inheritdoc />
        public IStorageQueueClient CreateQueueClient()
        {
            CloudQueueClient sdkClient = _sdkAccount.CreateCloudQueueClient();

            return(new StorageQueueClient(sdkClient));
        }
예제 #18
0
        public ActionResult Post()
        {
            // Prepare connection to storage account
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AppSettings.ConnectionString);

            try{
                // Unpack the Stripe Event
                var json        = new StreamReader(HttpContext.Request.Body).ReadToEndAsync().Result;
                var stripeEvent = Stripe.EventUtility.ParseEvent(json);

                // Connect to Message Queue
                CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                CloudQueue       queue       = queueClient.GetQueueReference(AppSettings.QueueName);
                queue.CreateIfNotExistsAsync();

                // Connect to LogsTable
                CloudTableClient logsTableClient = storageAccount.CreateCloudTableClient();
                CloudTable       logsTable       = logsTableClient.GetTableReference(AppSettings.StripeEventLogTableName);
                logsTable.CreateIfNotExistsAsync();

                // Ensure idempotency (Has this already been logged?)
                // ToDo: Check LogsTable, Ignore events that have already been processed...

                // We only focus on the events we care about, the remainder are logged for future reference:
                switch (stripeEvent.Type)
                {
                // Handle Stripe events based on type:

                case Stripe.Events.CustomerCreated:
                    // Process CustomerCreated Event...
                    break;

                case Stripe.Events.ChargeSucceeded:
                    // Process ChargeSucceeded Event...
                    break;

                case Stripe.Events.ChargeFailed:
                    // Process ChargeFailed Event...
                    break;

                default:
                    // Process Default Event...
                    break;
                }

                // Send into message queue:
                // Create a message and add it to the queue.
                var queueMessage = new QueueMessage {
                    Id   = stripeEvent.Id,
                    Type = stripeEvent.Type
                };

                var messageAsJson         = JsonConvert.SerializeObject(queueMessage);
                CloudQueueMessage message = new CloudQueueMessage(messageAsJson);
                queue.AddMessageAsync(message);

                // Log processing of this event:
                var eventLogEntity = new EventLogEntity(stripeEvent.Id);
                eventLogEntity.Type = stripeEvent.Type;
                TableOperation insertOperation = TableOperation.Insert(eventLogEntity);
                logsTable.ExecuteAsync(insertOperation);

                // Return status code 200
                return(Ok());
            }
            catch (Exception e)
            {
                // Connect to ExceptionsTable
                CloudTableClient exceptionsTableClient = storageAccount.CreateCloudTableClient();
                CloudTable       exceptionsTable       = exceptionsTableClient.GetTableReference(AppSettings.ExceptionLogTableName);
                exceptionsTable.CreateIfNotExistsAsync();

                // Log the exception:
                var exceptionLogEntity = new ExceptionLogEntity()
                {
                    Message = e.Message
                };
                TableOperation insertOperation = TableOperation.Insert(exceptionLogEntity);
                exceptionsTable.ExecuteAsync(insertOperation);

                // Return status code 400:
                return(BadRequest());
            }
        }
        /// <summary>
        /// Send back to watcher a "Posion messsage" and delete from in queue
        /// </summary>
        /// <param name="poisonMessage">the poison message</param>
        /// <returns>Sueccess or not</returns>
        private bool SendPoisonMessage(CloudQueueMessage poisonMessage)
        {
            bool sw = false;

            try
            {
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_Configuration.ProcessConfigConn);
                CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                CloudQueue          poisonQueue    = queueClient.GetQueueReference(_Configuration.poisonQueue);
                poisonQueue.CreateIfNotExists();

                MediaButler.Common.ButlerRequest  myButlerRequest  = Newtonsoft.Json.JsonConvert.DeserializeObject <ButlerRequest>(poisonMessage.AsString);
                MediaButler.Common.ButlerResponse myButlerResponse = new Common.ButlerResponse();

                myButlerResponse.MezzanineFiles = myButlerRequest.MezzanineFiles;
                ////Add to Mezzamine Files the control File URL if it exist
                //Becouse it is needed to move/delete the control file from processing to succes or fail
                if (!string.IsNullOrEmpty(myButlerRequest.ControlFileUri))
                {
                    myButlerResponse.MezzanineFiles.Add(myButlerRequest.ControlFileUri);
                }
                myButlerResponse.TimeStampProcessingCompleted = DateTime.Now.ToString();
                myButlerResponse.TimeStampProcessingStarted   = DateTime.Now.ToString();
                myButlerResponse.WorkflowName = myButlerRequest.WorkflowName;
                myButlerResponse.MessageId    = myButlerRequest.MessageId;
                myButlerResponse.TimeStampRequestSubmitted = myButlerRequest.TimeStampUTC;
                myButlerResponse.StorageConnectionString   = myButlerRequest.StorageConnectionString;
                myButlerResponse.Log = "Poison Message";

                //Lookin for Errors in Table Status
                CloudTableClient tableClient       = storageAccount.CreateCloudTableClient();
                CloudTable       table             = tableClient.GetTableReference(MediaButler.Common.Configuration.ButlerWorkflowStatus);
                TableOperation   retrieveOperation =
                    TableOperation.Retrieve <MediaButler.Common.workflow.ProcessSnapShot>(myButlerRequest.WorkflowName, myButlerRequest.MessageId.ToString());
                TableResult retrievedResult = table.Execute(retrieveOperation);
                if (retrievedResult.Result != null)
                {
                    //we have process info
                    var status = (MediaButler.Common.workflow.ProcessSnapShot)retrievedResult.Result;
                    try
                    {
                        var request = Newtonsoft.Json.JsonConvert.DeserializeObject <MediaButler.Common.workflow.ChainRequest>(status.jsonContext);
                        foreach (var error in request.Exceptions)
                        {
                            myButlerResponse.Log += "\r\n" + error.Message;
                        }
                    }
                    catch (Exception X)
                    {
                        Trace.TraceWarning("Unable to load Error LOG in response.log on poison message");
                        myButlerResponse.Log += "\r\n" + X.Message;
                        myButlerResponse.Log += "\r\n" + status.jsonContext;
                    }


                    //Delete register from Status table
                    TableOperation insertOperation = TableOperation.Delete(status);
                    table.Execute(insertOperation);
                }
                //Send Poison Mesagge
                CloudQueueMessage poison = new CloudQueueMessage(Newtonsoft.Json.JsonConvert.SerializeObject(myButlerResponse));
                poisonQueue.AddMessage(poison);
                sw = true;
            }
            catch (Exception X)
            {
                string txt = string.Format("[{0}] at {1} has an error: {2}", this.GetType().FullName, "GetNewMessage", X.Message);
                Trace.TraceError(txt);
            }
            return(sw);
        }
예제 #20
0
 public QueueService(string storageConnection)
 {
     _conn            = storageConnection;
     _storageAccount  = CloudStorageAccount.Parse(_conn);
     CloudQueueClient = _storageAccount.CreateCloudQueueClient();
 }
 /// <summary>
 /// Queue management constructor
 /// </summary>
 /// <param name="client">Cloud queue client</param>
 public StorageQueueManagement(AzureStorageContext context)
 {
     internalStorageContext = context;
     queueClient            = internalStorageContext.StorageAccount.CreateCloudQueueClient();
 }
            public TestFixture()
            {
                RandomNameResolver   nameResolver      = new TestNameResolver();
                JobHostConfiguration hostConfiguration = new JobHostConfiguration()
                {
                    NameResolver = nameResolver,
                    TypeLocator  = new FakeTypeLocator(typeof(MultipleStorageAccountsEndToEndTests)),
                };

                Config = hostConfiguration;

                Account1 = CloudStorageAccount.Parse(hostConfiguration.StorageConnectionString);
                string secondaryConnectionString = AmbientConnectionStringProvider.Instance.GetConnectionString(Secondary);

                Account2 = CloudStorageAccount.Parse(secondaryConnectionString);

                CleanContainers();

                CloudBlobClient    blobClient1     = Account1.CreateCloudBlobClient();
                string             inputName       = nameResolver.ResolveInString(Input);
                CloudBlobContainer inputContainer1 = blobClient1.GetContainerReference(inputName);

                inputContainer1.Create();
                string outputName = nameResolver.ResolveWholeString(Output);

                OutputContainer1 = blobClient1.GetContainerReference(outputName);
                OutputContainer1.CreateIfNotExists();

                CloudBlobClient    blobClient2     = Account2.CreateCloudBlobClient();
                CloudBlobContainer inputContainer2 = blobClient2.GetContainerReference(inputName);

                inputContainer2.Create();
                OutputContainer2 = blobClient2.GetContainerReference(outputName);
                OutputContainer2.CreateIfNotExists();

                CloudQueueClient queueClient1 = Account1.CreateCloudQueueClient();
                CloudQueue       inputQueue1  = queueClient1.GetQueueReference(inputName);

                inputQueue1.CreateIfNotExists();
                OutputQueue1 = queueClient1.GetQueueReference(outputName);
                OutputQueue1.CreateIfNotExists();

                CloudQueueClient queueClient2 = Account2.CreateCloudQueueClient();
                CloudQueue       inputQueue2  = queueClient2.GetQueueReference(inputName);

                inputQueue2.CreateIfNotExists();
                OutputQueue2 = queueClient2.GetQueueReference(outputName);
                OutputQueue2.CreateIfNotExists();

                CloudTableClient tableClient1    = Account1.CreateCloudTableClient();
                string           outputTableName = nameResolver.ResolveWholeString(OutputTableName);

                OutputTable1 = tableClient1.GetTableReference(outputTableName);
                OutputTable2 = Account2.CreateCloudTableClient().GetTableReference(outputTableName);

                // upload some test blobs to the input containers of both storage accounts
                CloudBlockBlob blob = inputContainer1.GetBlockBlobReference("blob1");

                blob.UploadText(TestData);
                blob = inputContainer2.GetBlockBlobReference("blob2");
                blob.UploadText(TestData);

                // upload some test queue messages to the input queues of both storage accounts
                inputQueue1.AddMessage(new CloudQueueMessage(TestData));
                inputQueue2.AddMessage(new CloudQueueMessage(TestData));

                Host = new JobHost(hostConfiguration);
                Host.Start();
            }
예제 #23
0
        static EnqueuerFunction()
        {
            var storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));

            QueueClient = storageAccount.CreateCloudQueueClient();
        }
예제 #24
0
 /// <summary>
 /// Create the queue if it does not already exist
 /// </summary>
 /// <param name="client">
 /// Ref to Queue Client
 /// </param>
 /// <param name="queue">
 /// Queue Name
 /// </param>
 /// <param name="options">
 /// Options if needed
 /// </param>
 /// <param name="context">
 /// Operational Context
 /// </param>
 /// <returns>
 /// True or False
 /// </returns>
 private static bool CreateQueueIfNotExist(CloudQueueClient client, string queue, QueueRequestOptions options = null, OperationContext context = null)
 {
     return(client.GetQueueReference(queue).CreateIfNotExists(options, context));
 }
예제 #25
0
        public void ProfileStorageAccess()
        {
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;

            #region Blob

            //Blobs
            CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();

            CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference("blobcontainer");
            cloudBlobContainer.CreateIfNotExists();

            ICloudBlob cloudBlob = cloudBlobContainer.GetBlockBlobReference("testblob");

            byte[] data = { 1, 2, 3 };

            using (TimeMeasure.Measure("blob write"))
            {
                cloudBlob.UploadFromByteArray(data, 0, 3);
            }

            #endregion

            #region Queue

            CloudQueueClient queueClient = cloudStorageAccount.CreateCloudQueueClient();

            //Queues
            CloudQueue cloudQueue = queueClient.GetQueueReference("test-queue");
            cloudQueue.CreateIfNotExists();

            using (TimeMeasure.Measure("queue write"))
            {
                cloudQueue.AddMessage(new CloudQueueMessage("testing123"));
            }

            CloudTableClient tableClient = cloudStorageAccount.CreateCloudTableClient();

            #endregion

            #region Table

            //Tables
            CloudTable cloudTable = tableClient.GetTableReference("testtable");
            cloudTable.CreateIfNotExists();

            ITableEntity tableEntity = new DynamicTableEntity
            {
                PartitionKey = "pkey",
                Properties   = new Dictionary <string, EntityProperty> {
                    { "name", new EntityProperty("chris") }
                },
                RowKey = "row1",
            };

            TableOperation operation = TableOperation.InsertOrMerge(tableEntity);

            using (TimeMeasure.Measure("table entity write"))
            {
                cloudTable.Execute(operation);
            };

            #endregion
        }
예제 #26
0
 public CloudQueueProvider(CloudQueueClient client)
 {
     this.client = client;
 }
예제 #27
0
        static void Main(string[] args)
        {
            Console.WriteLine("Queue encryption sample");

            // Retrieve storage account information from connection string
            // How to create a storage connection string - https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/
            CloudStorageAccount storageAccount = EncryptionShared.Utility.CreateStorageAccountFromConnectionString();
            CloudQueueClient    client         = storageAccount.CreateCloudQueueClient();
            CloudQueue          queue          = client.GetQueueReference(DemoQueue + Guid.NewGuid().ToString("N"));

            try
            {
                queue.Create();

                // Create the IKey used for encryption.
                RsaKey key = new RsaKey("private:key1");

                // Create the encryption policy to be used for insert and update.
                QueueEncryptionPolicy insertPolicy = new QueueEncryptionPolicy(key, null);

                // Set the encryption policy on the request options.
                QueueRequestOptions insertOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = insertPolicy
                };

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                // Add message
                Console.WriteLine("Inserting the encrypted message.");
                queue.AddMessage(message, null, null, insertOptions, null);

                // For retrieves, a resolver can be set up that will help pick the key based on the key id.
                LocalResolver resolver = new LocalResolver();
                resolver.Add(key);

                QueueEncryptionPolicy retrPolicy      = new QueueEncryptionPolicy(null, resolver);
                QueueRequestOptions   retrieveOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = retrPolicy
                };

                // Retrieve message
                Console.WriteLine("Retrieving the encrypted message.");
                CloudQueueMessage retrMessage = queue.GetMessage(null, retrieveOptions, null);

                // Update message
                Console.WriteLine("Updating the encrypted message.");
                string updatedMessage = Guid.NewGuid().ToString("N");
                retrMessage.SetMessageContent(updatedMessage);
                queue.UpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility, insertOptions, null);

                // Retrieve updated message
                Console.WriteLine("Retrieving the updated encrypted message.");
                retrMessage = queue.GetMessage(null, retrieveOptions, null);

                Console.WriteLine("Press enter key to exit");
                Console.ReadLine();
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
예제 #28
0
 protected BaseTest()
 {
     queues = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudQueueClient();
 }
예제 #29
0
        public override void Run()
        {
            var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"));

            CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobStorage.GetContainerReference("photogallery");

            CloudQueueClient queueStorage = storageAccount.CreateCloudQueueClient();
            CloudQueue queue = queueStorage.GetQueueReference("thumbnailmaker");

            Trace.TraceInformation("Creating container and queue...");

            // If the Start() method throws an exception, the role recycles.
            // If this sample is run locally and the development storage tool has not been started, this 
            // can cause a number of exceptions to be thrown because roles are restarted repeatedly.
            // Lets try to create the queue and the container and check whether the storage services are running
            // at all.
            bool containerAndQueueCreated = false;
            while (!containerAndQueueCreated)
            {
                try
                {
                    container.CreateIfNotExist();

                    var permissions = container.GetPermissions();

                    permissions.PublicAccess = BlobContainerPublicAccessType.Container;

                    container.SetPermissions(permissions);

                    permissions = container.GetPermissions();

                    queue.CreateIfNotExist();

                    containerAndQueueCreated = true;
                }
                catch (StorageClientException e)
                {
                    if (e.ErrorCode == StorageErrorCode.TransportError)
                    {
                        Trace.TraceError(string.Format("Connect failure! The most likely reason is that the local " +
                            "Development Storage tool is not running or your storage account configuration is incorrect. " +
                            "Message: '{0}'", e.Message));
                        System.Threading.Thread.Sleep(5000);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            Trace.TraceInformation("Listening for queue messages...");

            // Now that the queue and the container have been created in the above initialization process, get messages
            // from the queue and process them individually.
            while (true)
            {
                try
                {
                    CloudQueueMessage msg = queue.GetMessage();
                    if (msg != null)
                    {
                        string path = msg.AsString;
                        string thumbnailName = System.IO.Path.GetFileNameWithoutExtension(path) + ".jpg";
                        Trace.TraceInformation(string.Format("Dequeued '{0}'", path));
                        CloudBlockBlob content = container.GetBlockBlobReference(path);
                        CloudBlockBlob thumbnail = container.GetBlockBlobReference("thumbnails/" + thumbnailName);
                        MemoryStream image = new MemoryStream();

                        content.DownloadToStream(image);

                        image.Seek(0, SeekOrigin.Begin);
                      
                        thumbnail.Properties.ContentType = "image/jpeg";
                        thumbnail.UploadFromStream(CreateThumbnail(image));

                        Trace.TraceInformation(string.Format("Done with '{0}'", path));

                        queue.DeleteMessage(msg);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                catch (Exception e)
                {
                    // Explicitly catch all exceptions of type StorageException here because we should be able to 
                    // recover from these exceptions next time the queue message, which caused this exception,
                    // becomes visible again.

                    System.Threading.Thread.Sleep(5000);
                    Trace.TraceError(string.Format("Exception when processing queue item. Message: '{0}'", e.Message));
                }
            }
        }
예제 #30
0
        public StupidEnqueueuer(string cloudStorageAccountConnStr)
        {
            var storageAccount = CloudStorageAccount.Parse(cloudStorageAccountConnStr);

            _client = storageAccount.CreateCloudQueueClient();
        }
 public void Connect(string connectionString)
 {
     _client = CloudStorageAccount.Parse(connectionString).CreateCloudQueueClient();
 }
 private async Task CreateQueue(CloudQueueClient client, string queueName)
 {
     var queue = client.GetQueueReference(queueName);
     await queue.CreateIfNotExistsAsync();
 }