Ejemplo n.º 1
0
 private async static Task EnqueueAsync(CloudQueue queue, string text)
 {
     // Create a message and add it to the queue.
     CloudQueueMessage message = new CloudQueueMessage(text);
     await queue.AddMessageAsync(message);
 }
Ejemplo n.º 2
0
        public void Enqueue(WorkflowMessage message)
        {
            var queueMessage = new CloudQueueMessage(message.GetContent());

            _queue.AddMessageAsync(queueMessage).Wait();
        }
Ejemplo n.º 3
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.");
                }
            }
        }
 public async Task Insert(NotificationQueueItem item)
 {
     CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(item));
     await queue.AddMessageAsync(message);
 }
Ejemplo n.º 5
0
        public Task PutMessageAsync(T itm)
        {
            var msg = Newtonsoft.Json.JsonConvert.SerializeObject(itm);

            return(_queue.AddMessageAsync(new CloudQueueMessage(msg)));
        }
Ejemplo n.º 6
0
 public Task AddMessageAsync(CloudQueueMessage message, CancellationToken cancellationToken)
 {
     return(_queue.AddMessageAsync(message, cancellationToken));
 }
        [HttpPost] //<-- /webhook/topic1
        public ActionResult Post()
        {
            // Connect to the
            // storage message queue:
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AppSettings.ConnectionString);

            CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
            CloudQueue       queue       = queueClient.GetQueueReference(AppSettings.QueueName);

            queue.CreateIfNotExistsAsync();

            using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                var jsonContent = reader.ReadToEnd();

                var eventType = "";

                // Check the event type.
                // Return the validation code if it's a "SubscriptionValidation" request.
                // Act on the Notification otherwise
                if (HttpContext.Request.Headers["aeg-event-type"].FirstOrDefault() == "SubscriptionValidation")
                {
                    return(HandleValidation(jsonContent));
                }
                else if (HttpContext.Request.Headers["aeg-event-type"].FirstOrDefault() == "Notification")
                {
                    var _topic  = String.Empty;
                    var _source = String.Empty;
                    var _count  = 0;

                    // Check to see if this is passed in using the CloudEvents schema
                    if (IsCloudEvent(jsonContent))
                    {
                        eventType = "Cloud";
                        var cloudEvent = UnpackCloudEvent(jsonContent);

                        var topicData = JsonConvert.DeserializeObject <CustomTopicData>(cloudEvent.Data.ToString());

                        _topic  = topicData.Topic;
                        _source = topicData.Source;
                        _count  = 1; //<-- Will always be 1 if CloudEvent
                    }
                    else
                    {
                        eventType = "Grid";
                        var gridEvents = UnpackGridEvents(jsonContent);

                        var topicData = JsonConvert.DeserializeObject <CustomTopicData>(gridEvents[0].Data.ToString());

                        _topic  = topicData.Topic;
                        _source = topicData.Source;
                        _count  = gridEvents.Count;
                    }

                    // Create a message and add it to the queue.
                    var queueMessage = new QueueMessage {
                        Topic      = _topic,
                        Source     = _source,
                        EventType  = eventType,
                        EventCount = _count
                    };

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

                    return(Ok());
                }

                return(BadRequest());
            }
        }
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequestMessage req,
            [Table("UserProfile")] CloudTable userProfileTable,
            [Queue("emaillogqueue")]  CloudQueue queue,
            [Blob("emailogblobcontainer/{rand-guid}.log")] TextWriter logEmailBlob,
            [SendGrid()] ICollector <Mail> message,
            TraceWriter log)
        {
            try
            {
                log.Info("LogEmailInBlob function processed a request.");

                var user = JsonConvert.DeserializeObject <UserProfile>(await req.Content.ReadAsStringAsync());

                if (user == null)
                {
                    throw new System.Exception("Please provide values for First name,Last Name and Profile Picture");
                }

                var userProfile = new UserProfile(user.FirstName, user.LastName, user.ProfilePicture, user.Email);

                var tblInsertOperation = TableOperation.Insert(userProfile);

                userProfileTable.Execute(tblInsertOperation);

                var mail = new Mail
                {
                    Subject = $"Thanks {userProfile.FirstName} {userProfile.LastName} for your Sign Up!"
                };

                var personalization = new Personalization();
                personalization.AddTo(new Email(userProfile.Email));

                Content content = new Content
                {
                    Type  = "text/plain",
                    Value = $"{userProfile.FirstName}, here's the link for your profile picture {userProfile.ProfilePicture}!"
                };

                mail.AddContent(content);
                mail.AddPersonalization(personalization);
                message.Add(mail);

                await queue.AddMessageAsync(new CloudQueueMessage(content.Value.ToString()));

                var queueMessage = await queue.GetMessageAsync();

                var data = queueMessage.AsString;

                await queue.DeleteMessageAsync(queueMessage);

                await logEmailBlob.WriteAsync(data);
            }
            catch (System.Exception ex)
            {
                log.Info($"LogEmailInBlob function : Exception:{ ex.Message}");
                throw;
            }
            finally
            {
                log.Info("LogEmailInBlob function has finished processing a request.");
            }
        }
Ejemplo n.º 9
0
 private static async Task AddMessageAsync(CloudQueue queue, object objectToSend)
 {
     string            facesJson = JsonConvert.SerializeObject(objectToSend);
     CloudQueueMessage facesMsg  = new CloudQueueMessage(facesJson);
     await queue.AddMessageAsync(facesMsg);
 }
        private static async Task sendDeviceId(int deviceId)
        {
            await Task.Delay(_random.Next(1, 500));

            await _queue.AddMessageAsync(new CloudQueueMessage(deviceId.ToString()));
        }
Ejemplo n.º 11
0
 public async static Task InsertMessage(CloudQueue cloudQueue)
 {
     CloudQueueMessage cloudQueueMessage = new CloudQueueMessage("This is an urgent message from Manish. - Second");
     await cloudQueue.AddMessageAsync(cloudQueueMessage);
 }
Ejemplo n.º 12
0
 public async Task AddMessageAsync(CloudQueue queue, String str)
 {
     await queue.AddMessageAsync(new CloudQueueMessage(str));
 }
Ejemplo n.º 13
0
        public async Task Table_IfBoundToICollectorPoco_AddInsertsUsingNativeTableTypes()
        {
            // Arrange
            PocoWithAllTypes expected = new PocoWithAllTypes
            {
                PartitionKey                   = PartitionKey,
                RowKey                         = RowKey,
                BooleanProperty                = true,
                NullableBooleanProperty        = null,
                ByteArrayProperty              = new byte[] { 0x12, 0x34 },
                DateTimeProperty               = DateTime.Now,
                NullableDateTimeProperty       = null,
                DateTimeOffsetProperty         = DateTimeOffset.MaxValue,
                NullableDateTimeOffsetProperty = null,
                DoubleProperty                 = 3.14,
                NullableDoubleProperty         = null,
                GuidProperty                   = Guid.NewGuid(),
                NullableGuidProperty           = null,
                Int32Property                  = 123,
                NullableInt32Property          = null,
                Int64Property                  = 456,
                NullableInt64Property          = null,
                StringProperty                 = "abc",
                PocoProperty                   = new Poco
                {
                    PartitionKey = "def",
                    RowKey       = "ghi",
                    Property     = "jkl"
                }
            };
            StorageAccount account      = CreateFakeStorageAccount();
            CloudQueue     triggerQueue = await account.CreateQueueAsync(TriggerQueueName);

            await triggerQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(expected)));

            // Act
            RunTrigger(account, typeof(BindToICollectorPocoWithAllTypesProgram));

            // Assert
            CloudTableClient client = account.CreateCloudTableClient();
            CloudTable       table  = client.GetTableReference(TableName);

            Assert.True(await table.ExistsAsync());
            DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey);

            Assert.Equal(expected.PartitionKey, entity.PartitionKey);
            Assert.Equal(expected.RowKey, entity.RowKey);
            IDictionary <string, EntityProperty> properties = entity.Properties;

            AssertNullablePropertyEqual(expected.BooleanProperty, EdmType.Boolean, properties, "BooleanProperty",
                                        (p) => p.BooleanValue);
            AssertPropertyNull(EdmType.Boolean, properties, "NullableBooleanProperty", (p) => p.BooleanValue);
            AssertPropertyEqual(expected.ByteArrayProperty, EdmType.Binary, properties, "ByteArrayProperty",
                                (p) => p.BinaryValue);
            AssertNullablePropertyEqual(expected.DateTimeProperty, EdmType.DateTime, properties, "DateTimeProperty",
                                        (p) => p.DateTime);
            AssertPropertyNull(EdmType.DateTime, properties, "NullableDateTimeProperty", (p) => p.DateTime);
            AssertNullablePropertyEqual(expected.DateTimeOffsetProperty, EdmType.DateTime, properties,
                                        "DateTimeOffsetProperty", (p) => p.DateTime);
            AssertPropertyNull(EdmType.DateTime, properties, "NullableDateTimeOffsetProperty",
                               (p) => p.DateTimeOffsetValue);
            AssertNullablePropertyEqual(expected.DoubleProperty, EdmType.Double, properties, "DoubleProperty",
                                        (p) => p.DoubleValue);
            AssertPropertyNull(EdmType.Double, properties, "NullableDoubleProperty", (p) => p.DoubleValue);
            AssertNullablePropertyEqual(expected.GuidProperty, EdmType.Guid, properties, "GuidProperty",
                                        (p) => p.GuidValue);
            AssertPropertyNull(EdmType.Guid, properties, "NullableGuidProperty", (p) => p.GuidValue);
            AssertNullablePropertyEqual(expected.Int32Property, EdmType.Int32, properties, "Int32Property",
                                        (p) => p.Int32Value);
            AssertPropertyNull(EdmType.Int32, properties, "NullableInt32Property", (p) => p.Int32Value);
            AssertNullablePropertyEqual(expected.Int64Property, EdmType.Int64, properties, "Int64Property",
                                        (p) => p.Int64Value);
            AssertPropertyNull(EdmType.Int64, properties, "NullableInt64Property", (p) => p.Int64Value);
            AssertPropertyEqual(expected.StringProperty, EdmType.String, properties, "StringProperty",
                                (p) => p.StringValue);
            AssertPropertyEqual(JsonConvert.SerializeObject(expected.PocoProperty, Formatting.Indented), EdmType.String,
                                properties, "PocoProperty", (p) => p.StringValue);
        }
Ejemplo n.º 14
0
        public void crawlerUrls(HashSet <String> duplicateList, List <String> noRobots)
        {
            HashSet <String> urlList = duplicateList;
            List <String>    lastten = new List <String>();
            getReference     g       = new getReference();
            CloudQueue       queue   = g.getQueue();
            CloudTable       table   = g.getTable();
            CloudQueue       cmd     = g.commandQueue();

            queue.FetchAttributes();
            var limitCount = queue.ApproximateMessageCount.Value;
            int count      = 0;

            while (0 < limitCount)
            {
                CloudQueueMessage retrievedMessage = queue.GetMessage();
                try
                {
                    if (retrievedMessage != null)
                    {
                        HtmlWeb      web      = new HtmlWeb();
                        HtmlDocument document = web.Load(retrievedMessage.AsString);
                        String       title    = "";
                        HtmlNode     node     = document.DocumentNode.SelectSingleNode("//title");
                        if (node != null)
                        {
                            HtmlAttribute desc;
                            desc  = node.Attributes["content"];
                            title = node.InnerHtml;
                        }

                        HtmlNode dateNode = document.DocumentNode.SelectSingleNode("//meta[(@itemprop='dateCreated')]");
                        String   date     = "";
                        if (dateNode != null)
                        {
                            date = dateNode.GetAttributeValue("content", "");
                        }

                        String tenUrls = "";
                        lastten.Add(retrievedMessage.AsString);
                        if (lastten.Count == 11)
                        {
                            lastten.RemoveAt(0);
                            tenUrls = String.Join(",", lastten);
                        }


                        queue.DeleteMessage(retrievedMessage);

                        String encodeUrl = EncodeUrlInKey(retrievedMessage.AsString);

                        crawledTable   ct        = new crawledTable("index", retrievedMessage.AsString, null, date, null, null, encodeUrl, 0, null, null, null);
                        crawledTable   dashboard = new crawledTable("dash", retrievedMessage.AsString, title, date, "DASHBOARD", tenUrls, "rowkey", 0, null, null, null);
                        TableOperation insertOrReplaceOperation  = TableOperation.InsertOrReplace(ct);
                        TableOperation insertOrReplaceOperation1 = TableOperation.InsertOrReplace(dashboard);
                        table.Execute(insertOrReplaceOperation);
                        table.Execute(insertOrReplaceOperation1);


                        String root = "";

                        if (retrievedMessage.AsString.Contains("bleacher"))
                        {
                            root = "bleacherreport.com";
                        }
                        else if (retrievedMessage.AsString.Contains("cnn"))
                        {
                            root = "cnn.com";
                        }

                        var rows = document.DocumentNode.SelectNodes("//a[@href]");
                        if (rows != null && rows.Count > 0)
                        {
                            foreach (var link in rows)
                            {
                                String url = link.Attributes["href"].Value;
                                if (url.StartsWith("//"))
                                {
                                    url = "http:" + url;
                                }
                                else if (url.StartsWith("/"))
                                {
                                    url = "http://" + root + url;
                                }
                                if (!urlList.Contains(url) && !noRobots.Contains(url) && (url.Contains(root + "/")))
                                {
                                    urlList.Add(url);
                                    CloudQueueMessage message = new CloudQueueMessage(url);
                                    queue.AddMessageAsync(message);
                                }
                            }
                        }
                        queue.FetchAttributes();
                        limitCount = queue.ApproximateMessageCount.Value;
                    }
                }
                catch (WebException e)
                {
                    queue.DeleteMessage(retrievedMessage);
                    crawledTable   ct = new crawledTable("error", retrievedMessage.AsString, "No Title", "No date", e.Status.ToString(), null, "error url", 0, null, null, null);
                    TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(ct);
                    table.Execute(insertOrReplaceOperation);
                }
            }
        }
 public static async Task AddMessageToJsonAsync <T>(this CloudQueue cloudQueue, T objectToAdd)
 {
     var msgAsJson     = JsonSerializer.Serialize(objectToAdd);
     var cloudQueueMsg = new CloudQueueMessage(msgAsJson);
     await cloudQueue.AddMessageAsync(cloudQueueMsg);
 }
Ejemplo n.º 16
0
        public async Task SendMessageAsync(string message)
        {
            await _cloudQueue.CreateIfNotExistsAsync();

            await _cloudQueue.AddMessageAsync(new CloudQueueMessage(message));
        }
Ejemplo n.º 17
0
 public async Task SendArticleAsync(string newsMessage)
 {
     CloudQueueMessage articleMassage = new CloudQueueMessage(newsMessage);
     await _queue.AddMessageAsync(articleMassage);
 }
Ejemplo n.º 18
0
        public async Task <bool> AddMessageAsync(CloudQueueMessage message)
        {
            await _queue.AddMessageAsync(message);

            return(true);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Subscriptions the operation.
        /// </summary>
        /// <param name="subscriptionId">The subscription identifier.</param>
        /// <param name="planId">The plan identifier.</param>
        /// <param name="operation">The operation.</param>
        /// <param name="numberofProviders">The numberof providers.</param>
        /// <returns> The <see cref="IActionResult" />.</returns>
        public IActionResult SubscriptionOperation(Guid subscriptionId, string planId, string operation, int numberofProviders)
        {
            this.logger.LogInformation("Home Controller / SubscriptionOperation subscriptionId:{0} :: planId : {1} :: operation:{2} :: NumberofProviders : {3}", JsonSerializer.Serialize(subscriptionId), JsonSerializer.Serialize(planId), JsonSerializer.Serialize(operation), JsonSerializer.Serialize(numberofProviders));
            try
            {
                var userDetails = this.userRepository.GetPartnerDetailFromEmail(this.CurrentUserEmailAddress);
                var oldValue    = this.subscriptionService.GetSubscriptionsBySubscriptionId(subscriptionId);
                SubscriptionProcessQueueModel queueObject = new SubscriptionProcessQueueModel();
                if (operation == "Activate")
                {
                    if (oldValue.SubscriptionStatus.ToString() != SubscriptionStatusEnumExtension.PendingActivation.ToString())
                    {
                        this.subscriptionRepository.UpdateStatusForSubscription(subscriptionId, SubscriptionStatusEnumExtension.PendingActivation.ToString(), true);

                        SubscriptionAuditLogs auditLog = new SubscriptionAuditLogs()
                        {
                            Attribute      = Convert.ToString(SubscriptionLogAttributes.Status),
                            SubscriptionId = oldValue.SubscribeId,
                            NewValue       = SubscriptionStatusEnumExtension.PendingActivation.ToString(),
                            OldValue       = oldValue.SubscriptionStatus.ToString(),
                            CreateBy       = userDetails.UserId,
                            CreateDate     = DateTime.Now,
                        };
                        this.subscriptionLogRepository.Save(auditLog);
                    }

                    queueObject.SubscriptionID = subscriptionId;
                    queueObject.TriggerEvent   = "Activate";
                    queueObject.UserId         = userDetails.UserId;
                    queueObject.PortalName     = "Admin";
                }

                if (operation == "Deactivate")
                {
                    this.subscriptionRepository.UpdateStatusForSubscription(subscriptionId, SubscriptionStatusEnumExtension.PendingUnsubscribe.ToString(), true);
                    SubscriptionAuditLogs auditLog = new SubscriptionAuditLogs()
                    {
                        Attribute      = Convert.ToString(SubscriptionLogAttributes.Status),
                        SubscriptionId = oldValue.SubscribeId,
                        NewValue       = SubscriptionStatusEnumExtension.PendingUnsubscribe.ToString(),
                        OldValue       = oldValue.SubscriptionStatus.ToString(),
                        CreateBy       = userDetails.UserId,
                        CreateDate     = DateTime.Now,
                    };
                    this.subscriptionLogRepository.Save(auditLog);

                    queueObject.SubscriptionID = subscriptionId;
                    queueObject.TriggerEvent   = "Unsubscribe";
                    queueObject.UserId         = userDetails.UserId;
                    queueObject.PortalName     = "Admin";
                }

                string queueMessage                = JsonSerializer.Serialize(queueObject);
                string storageConnectionString     = this.cloudConfigs.AzureWebJobsStorage ?? this.azureWebJobsStorage;
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

                //// Create the queue client.
                CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                CloudQueue       queue       = queueClient.GetQueueReference("saas-provisioning-queue");

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

                //// Create a message and add it to the queue.
                CloudQueueMessage message = new CloudQueueMessage(queueMessage);
                queue.AddMessageAsync(message);

                return(this.RedirectToAction(nameof(this.ActivatedMessage)));
            }
            catch (Exception ex)
            {
                this.logger.LogInformation("Message:{0} :: {1}", ex.Message, ex.InnerException);
                return(this.View("Error"));
            }
        }
Ejemplo n.º 20
0
 public Task Send(T message) => _queue.AddMessageAsync(new CloudQueueMessage(message.Content));
Ejemplo n.º 21
0
 public async Task Add(string content)
 {
     // ToDo: Probably it would be good to strongly type the object is added to the queue
     var message = new CloudQueueMessage(content);
     await queue.AddMessageAsync(message);
 }
Ejemplo n.º 22
0
 public async Task EnqueueAsync(ProviderQueueItem item, CancellationToken cancellationToken)
 {
     await _queue.AddMessageAsync(new CloudQueueMessage(JsonSerializer.Serialize(item)), cancellationToken);
 }
Ejemplo n.º 23
0
 private async Task AddQueueMsgAsync(string bonusJson)
 {
     CloudQueueMessage qmsg = new CloudQueueMessage(bonusJson);
     await queue.AddMessageAsync(qmsg);
 }
Ejemplo n.º 24
0
 public async Task Send(T message)
 {
     await queue.AddMessageAsync(new CloudQueueMessage(Convert.ToString((object)message)));
 }
Ejemplo n.º 25
0
 private async Task SendTypedMessageAsync <T>(T payload, string messageId)
 {
     var label = payload.GetType().Name;
     var msg   = WrapWithMessageId(payload, messageId + label, label);
     await _cloudQueue.AddMessageAsync(msg);
 }
Ejemplo n.º 26
0
        public static async Task RunAsync(
            IInstallationTokenProvider installationTokenProvider,
            CloudTable installationTable,
            CloudQueue openPrQueue,
            CloudQueue openPrPoisonQueue,
            ILogger logger,
            ExecutionContext context)
        {
            for (var i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(1000);

                var topQueueMessage = await openPrPoisonQueue.GetMessageAsync();

                if (topQueueMessage == null)
                {
                    continue;
                }

                // pre-emptively delete the message from the queue we are pulling from
                await openPrPoisonQueue.DeleteMessageAsync(topQueueMessage);

                var topMessage = JsonConvert.DeserializeObject <OpenPrMessage>(topQueueMessage.AsString);

                try
                {
                    var installation = (Installation)(await installationTable.ExecuteAsync(
                                                          TableOperation.Retrieve <Installation>(topMessage.InstallationId.ToString(), topMessage.RepoName)))
                                       .Result;

                    if (installation == null)
                    {
                        logger.LogInformation("Not listed in installation table");
                        continue;
                    }

                    logger.LogInformation($"https://github.com/{installation.Owner}/{installation.RepoName}");

                    var installationTokenParameters = new InstallationTokenParameters
                    {
                        AccessTokensUrl = string.Format(KnownGitHubs.AccessTokensUrlFormat, topMessage.InstallationId),
                        AppId           = KnownGitHubs.AppId,
                    };

                    var installationToken = await installationTokenProvider.GenerateAsync(
                        installationTokenParameters,
                        File.OpenText(Path.Combine(context.FunctionDirectory, $"../{KnownGitHubs.AppPrivateKey}")));

                    var appClient = new Octokit.GitHubClient(new Octokit.ProductHeaderValue("MyApp"))
                    {
                        Credentials = new Octokit.Credentials(installationToken.Token, Octokit.AuthenticationType.Bearer)
                    };

                    var limits = await appClient.Miscellaneous.GetRateLimits();

                    logger.LogInformation("Ratelimits:\n");
                    logger.LogInformation(JsonConvert.SerializeObject(limits));

                    // check if an 'imgbot' branch is open
                    var branches = await appClient.Repository.Branch.GetAll(installation.Owner, installation.RepoName);

                    var imgbotBranches = branches.Where(x => x.Name == "imgbot");

                    if (imgbotBranches.Count() == 0)
                    {
                        // we have no open imgbot branches right now, let's just leave
                        continue;
                    }
                    else
                    {
                        logger.LogInformation("Open 'imgbot' branch found");
                    }

                    // check for ImgBot PRs
                    var prs = await appClient.Repository.PullRequest.GetAllForRepository(installation.Owner, installation.RepoName);

                    var imgbotPrs = prs.Where(x => x.Head.Ref == "imgbot");

                    if (imgbotPrs.Count() > 0)
                    {
                        // we have an open imgbot PR right now, let's just leave
                        continue;
                    }
                    else
                    {
                        logger.LogInformation("Open 'imgbot' PR not found, do we need to open one?");
                    }

                    // query for closed ImgBot PRs
                    var searchRequest = new Octokit.SearchIssuesRequest("imgbot")
                    {
                        Type  = Octokit.IssueTypeQualifier.PullRequest,
                        Repos = new Octokit.RepositoryCollection {
                            installation.Owner + "/" + installation.RepoName
                        }
                    };

                    var imgbotIssues = await appClient.Search.SearchIssues(searchRequest);

                    if (imgbotIssues.TotalCount == 0)
                    {
                        // no imgbot prs in history, let's queue a message to get the pr open
                        await openPrQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(topMessage)));
                    }
                    else
                    {
                        // this is the case where an 'imgbot' branch exists, but there are closed imgbot prs
                        var latestClosedPr  = imgbotIssues.Items.OrderByDescending(x => x.ClosedAt).First();
                        var potentialBranch = branches.First(x => x.Name == "imgbot");

                        var commitInImgbotBranch = await appClient.Repository.Commit
                                                   .Get(installation.Owner, installation.RepoName, potentialBranch.Commit.Sha);

                        if (commitInImgbotBranch.Commit.Author.Date > latestClosedPr.ClosedAt)
                        {
                            // if the branch is newer than the last closed imgbot PR then we should queue a message to get the pr open
                            await openPrQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(topMessage)));
                        }
                    }
                }
                catch (Exception e)
                {
                    logger.LogError(e, "ERROR!");

                    // add it back to the poison queue
                    await openPrPoisonQueue.AddMessageAsync(topQueueMessage);
                }
            }
        }
Ejemplo n.º 27
0
 public Task AddMessageAsync(CloudQueueMessage message)
 {
     return(_cloudQueue.AddMessageAsync(message));
 }
        private async Task ExecuteImpl(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (_initialized == false)
            {
                await Initialize().ConfigureAwait(false);
            }

            bool gotSomeMessages;

            do
            {
                // Always start by assuming we won't get any messages from the queue
                gotSomeMessages = false;

                // Get a batch of messages
                IEnumerable <CloudQueueMessage> messages = await _queue.GetMessagesAsync(MessagesPerGet, cancellationToken).ConfigureAwait(false);

                foreach (CloudQueueMessage message in messages)
                {
                    // Check for cancellation before processing a message
                    cancellationToken.ThrowIfCancellationRequested();

                    // We obviously got some messages since we're processing one
                    gotSomeMessages = true;

                    // Try to deserialize the message to an EncodingJobEvent
                    EncodingJobEvent jobEvent = null;
                    try
                    {
                        var settings = new JsonSerializerSettings {
                            DateTimeZoneHandling = DateTimeZoneHandling.Utc
                        };
                        jobEvent = JsonConvert.DeserializeObject <EncodingJobEvent>(message.AsString, settings);
                    }
                    catch (Exception e)
                    {
                        Logger.Warning(e, "Exception while deserializing event, message will be deleted");
                    }

                    // If there was a problem with deserialization, just assume a poison message and delete it
                    if (jobEvent == null)
                    {
                        await _queue.DeleteMessageAsync(message, cancellationToken).ConfigureAwait(false);

                        continue;
                    }

                    // Ignore any messages that aren't for JobStateChanges
                    if (jobEvent.IsJobStateChangeEvent() == false)
                    {
                        await _queue.DeleteMessageAsync(message, cancellationToken).ConfigureAwait(false);

                        continue;
                    }

                    // Otherwise, handle the event
                    bool handledSuccessfully = false;
                    try
                    {
                        await HandleEncodingJobEvent(jobEvent).ConfigureAwait(false);

                        handledSuccessfully = true;
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Error while handling JobStateChanged message");
                    }

                    // If the message was handled successfully, just delete it
                    if (handledSuccessfully)
                    {
                        await _queue.DeleteMessageAsync(message, cancellationToken).ConfigureAwait(false);

                        continue;
                    }

                    // If the message is over the number of retries, consider it a poison message, log it and delete it
                    if (jobEvent.RetryAttempts >= MaxRetries)
                    {
                        // Move the message to a poison queue (NOTE: because Add + Delete aren't "transactional" it is possible
                        // a poison message might get added more than once to the poison queue, but that's OK)
                        Logger.Fatal("Giving up on message: {Message}", message.AsString);
                        await _poisonQueue.AddMessageAsync(message, cancellationToken).ConfigureAwait(false);

                        await _queue.DeleteMessageAsync(message, cancellationToken).ConfigureAwait(false);

                        continue;
                    }

                    // Increment the retry attempts and then modify the message in place so it will be processed again
                    int secondsUntilRetry = (2 ^ jobEvent.RetryAttempts) * 10;
                    jobEvent.RetryAttempts++;
                    message.SetMessageContent(JsonConvert.SerializeObject(jobEvent));
                    await _queue.UpdateMessageAsync(message, TimeSpan.FromSeconds(secondsUntilRetry),
                                                    MessageUpdateFields.Content | MessageUpdateFields.Visibility, cancellationToken).ConfigureAwait(false);
                }

                // If we got some messages from the queue, keep processing until we don't get any
            } while (gotSomeMessages);

            // Exit method to allow a cooldown period (10s) between polling the queue whenever we run out of messages
        }
Ejemplo n.º 29
0
        public async Task <ActionResult> CreateReport(string sdatemonth, string sdateday, string sdateyear,
                                                      string edatemonth, string edateday, string edateyear,
                                                      string period, string detailed, string [] subslist)
        {
            try
            {
                dm.startDateMonth = Convert.ToInt32(sdatemonth);
                dm.startDateDay   = Convert.ToInt32(sdateday);
                dm.startDateYear  = Convert.ToInt32(sdateyear);

                dm.endDateMonth = Convert.ToInt32(edatemonth);
                dm.endDateDay   = Convert.ToInt32(edateday);
                dm.endDateYear  = Convert.ToInt32(edateyear);

                dm.detailedReport = (detailed == "d" ? true : false);
                dm.dailyReport    = (period == "d" ? true : false);

                if (subslist != null)
                {
                    dm.selectedUserSubscriptions = subslist.ToList();
                }

                if (dm.selectedUserSubscriptions.Count() < 1)
                {
                    throw new Exception("No Azure subscription is selected. Select from list and try again (may use CTRL for multiple selection).");
                }

                DateTime s, e;
                try
                {
                    s = new DateTime(dm.startDateYear, dm.startDateMonth, dm.startDateDay);
                    e = new DateTime(dm.endDateYear, dm.endDateMonth, dm.endDateDay);
                } catch
                {
                    throw new Exception("Invalid date input.");
                }

                TimeSpan ts = e.Subtract(s);
                if (ts.TotalDays > 150)
                {
                    throw new Exception("Time interval can not be greater than 150 (5 month) days");
                }

                if (ts.TotalDays < 1)
                {
                    throw new Exception("Start date can not be later than or equal to the end date.");
                }



                // now we have all parameters set. Ready to create jobs and send them to Storage queue to be processed by webjob
                ReportRequest rr = new ReportRequest();
                rr.reportDate     = DateTime.UtcNow;
                rr.startDate      = s;
                rr.endDate        = e;
                rr.detailedReport = dm.detailedReport;
                rr.dailyReport    = dm.dailyReport;

                foreach (string sid in dm.selectedUserSubscriptions)
                {
                    Subscription subs = dm.userSubscriptionsList[sid];
                    if (subs == null)
                    {
                        continue;
                    }

                    Report rjd = new Report();
                    rjd.subscriptionID = subs.Id;
                    rjd.organizationID = subs.OrganizationId;
                    rr.repReqs.Add(rjd);
                }

                db.ReportRequests.Add(rr);
                db.SaveChanges();


                var queueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(rr));
                await reportRequestsQueue.AddMessageAsync(queueMessage);
            }
            catch (Exception e)
            {
                //dm.Reset();
                return(RedirectToAction("Error", "Home", new { msg = e.Message }));
            }

            return(RedirectToAction("Index", "Dashboard"));
        }
Ejemplo n.º 30
0
 public async Task SendMessage(string message)
 {
     var cloudQueuMessage = new CloudQueueMessage(message);
     await _cloudQueue.AddMessageAsync(cloudQueuMessage);
 }