Esempio n. 1
0
        //5-ReportCollectionTask
        protected void ReportCollectionTask()
        {
            Logger.Log(GetContext() + "ReportCollectionTask(): #" + _currentTask.Id);

            //MetaData
            _currentTask.Station   = _station;
            _currentTask.Collector = _collector;
            _currentTask.Collected = CollectionsTaskHelper.Now();

            //SaveBlob
            CloudBlob blob = AzureClientService.GetBlobReference(_currentTask.Project, _currentTask.BlobName);

            blob.Attributes.Properties.ContentMD5 = _currentTask.ResultHash;
            blob.Attributes.Metadata["Id"]        = _currentTask.Id.ToString();
            blob.Attributes.Metadata["State"]     = StateTypeEnum.Collected.ToString();
            blob.Attributes.Metadata["Project"]   = _currentTask.Project;
            blob.Attributes.Metadata["Collected"] = _currentTask.Collected;
            blob.Attributes.Metadata["Command"]   = _currentTask.Command;
            //blob.Attributes.Metadata["TargetId"] = _currentTask.
            blob.UploadText(_currentTask.ResultJson);

            //AddQueue
            string message = CollectionsTaskHelper.CollectionsTaskToMessage(_currentTask);

            AzureClientService.AddQueueMessage(QueueTypeEnum.ProcessingTwitter, message);
            AzureClientService.DeleteQueueMessage(_collectionsQueue, _currentQueueMessage);
        }
        public void ProccesCollectionTasks()
        {
            bool loop = true;

            while (loop)
            {
                IEnumerable <CloudQueueMessage> queueMessages = AzureClientService.GetQueueMessages(QueueTypeEnum.ProcessingTwitter, 32);

                int messageCount = queueMessages.Count <CloudQueueMessage>();

                if (messageCount < 1)
                {
                    Console.WriteLine("No Messages");
                    loop = false;
                }
                else
                {
                    Console.WriteLine("GetQueueMessage = " + queueMessages.Count <CloudQueueMessage>());

                    foreach (CloudQueueMessage queueMessage in queueMessages)
                    {
                        //1. Get Queue
                        T_CollectionTask t_newTask = CollectionsTaskHelper.MessageToT_CollectionTask(queueMessage.AsString);
                        //string taskString = CollectionsTaskHelper.T_CollectionsTaskToMessage(newTask);
                        //Console.WriteLine(taskString);

                        //2. Get Blob
                        CloudBlob blob     = AzureClientService.GetBlobReference(t_newTask.Project, t_newTask.BlobName);
                        string    blobText = blob.DownloadText();
                        //TODO: Blob Validate Hash
                        //Console.WriteLine(blobText);

                        //3. Update Database: Results
                        if (t_newTask.Command == TwitterCommandEnum.Followers.ToString())
                        {
                            ProcessTwitterFollowers(blobText, t_newTask);
                        }
                        else if (t_newTask.Command == TwitterCommandEnum.Friends.ToString())
                        {
                            ProcessTwitterFriends(blobText, t_newTask);
                        }
                        else if (t_newTask.Command == TwitterCommandEnum.Profile.ToString())
                        {
                            ProcessTwitterProfile(blobText, t_newTask);
                        }
                        else if (t_newTask.Command == TwitterCommandEnum.Tweets.ToString())
                        {
                            ProcessTwitterTweets(blobText, t_newTask);
                        }
                        else if (t_newTask.Command == TwitterCommandEnum.Search.ToString())
                        {
                            ProcessTwitterSearch(blobText, t_newTask);
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }

                        //4. Update Database: Tasks
                        LinqToSqlAzureHaystackDataContext context = new LinqToSqlAzureHaystackDataContext();
                        T_CollectionTask t_oldTask = (from t in context.T_CollectionTasks
                                                      where t.Id == t_newTask.Id
                                                      orderby t.Id descending
                                                      select t).First <T_CollectionTask>();

                        t_oldTask.State      = StateTypeEnum.Processed.ToString();
                        t_oldTask.Collected  = t_newTask.Collected;
                        t_oldTask.Station    = t_newTask.Station;
                        t_oldTask.Collector  = t_newTask.Collector;
                        t_oldTask.BlobName   = t_newTask.BlobName;
                        t_oldTask.ResultHash = blob.Attributes.Properties.ContentMD5;
                        t_oldTask.Processed  = DateTime.UtcNow;
                        context.SubmitChanges();

                        //5. Update Blob Metadata
                        blob.Attributes.Metadata["State"] = StateTypeEnum.Processed.ToString();

                        //6. Delete Queue
                        AzureClientService.DeleteQueueMessage(QueueTypeEnum.ProcessingTwitter, queueMessage);

                        //7. Print Success
                        string output = CollectionsTaskHelper.T_CollectionsTaskToMessage(t_newTask);
                        Console.WriteLine(output);
                    }
                }
            }
        }