Ejemplo n.º 1
0
        private Dictionary <string, Object> GetConfigOptions()
        {
            Amazon.S3.Model.GetObjectResponse rsp;
            IAWSContext ctx  = AppUtility.GetContext();
            var         name = AppUtility.GetContext().Bucket;
            var         key  = "config";
            string      content;

            using (var client = ctx.GetS3Client())
            {
                var req = new Amazon.S3.Model.GetObjectRequest()
                          .WithBucketName(name).WithKey(key);
                try
                {
                    rsp = client.GetObject(req);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("Bucket {0} key {1}: {2}", name, key, ex.Message), GetType());
                    throw;
                }
                using (StreamReader r = new StreamReader(rsp.ResponseStream))
                {
                    content = r.ReadToEnd();
                }
            }
            var dict = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(content);

            return(dict);
        }
        public void UnRegister()
        {
            IAWSContext awsCtx = Turbine.Consumer.AWS.AppUtility.GetContext();
            var         unreg  = new UnRegister();

            unreg.Id         = Guid.NewGuid();
            unreg.ConsumerId = this.regInfo.Id;
            this.jobQueue    = null;
            SimpleMessageConnect.Send(unreg);
        }
        /// <summary>
        /// Register the consumer by sending a formatted request to a SQS.  This request contains a
        /// URL to the queue this method will poll for a response.  The response will be checked
        /// via a GUID provided in the request and returned in the reply.  The response will also
        /// contain a SNS Topic that will be used to send updates to.
        ///
        /// If no reply on long poll return null.
        /// </summary>
        public IJobQueue Register()
        {
            IAWSContext awsCtx      = Turbine.Consumer.AWS.AppUtility.GetContext();
            var         registerMsg = new Register();

            registerMsg.Id         = Guid.NewGuid();
            registerMsg.InstanceID = awsCtx.InstanceId;
            registerMsg.AMI        = awsCtx.AmiId;
            regInfo       = SimpleMessageConnect.SendReceive(registerMsg);
            this.jobQueue = new JobQueue(regInfo);
            return(jobQueue);
        }
        static SimpleMessageConnect()
        {
            IAWSContext awsCtx = Turbine.Consumer.AWS.AppUtility.GetContext();

            topicArn     = awsCtx.RequestTopicArn;
            notification = Amazon.AWSClientFactory
                           .CreateAmazonSNSClient(awsCtx.AccessKey, awsCtx.SecretKey,
                                                  new AmazonSimpleNotificationServiceConfig().WithServiceURL(awsCtx.SNSServiceURL));
            Debug.WriteLine("topicArn: " + topicArn);
            Debug.WriteLine("SQSServiceURL: " + awsCtx.SQSServiceURL);
            Debug.WriteLine("SNSServiceURL: " + awsCtx.SNSServiceURL);
            queue = Amazon.AWSClientFactory
                    .CreateAmazonSQSClient(
                awsCtx.AccessKey, awsCtx.SecretKey,
                new Amazon.SQS.AmazonSQSConfig().WithServiceURL(awsCtx.SQSServiceURL));
            responseQueuePrefix = awsCtx.ResponseQueuePrefix;
        }
Ejemplo n.º 5
0
        private void UpdateInstances(List <string> instances)
        {
            Amazon.S3.Model.PutObjectResponse rsp;
            IAWSContext ctx     = AppUtility.GetContext();
            var         name    = AppUtility.GetContext().Bucket;
            var         key     = "floor";
            var         request = new Amazon.S3.Model.PutObjectRequest();

            request.WithBucketName(name);
            request.WithKey("instances");
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(instances);

            request.WithContentBody(json);
            using (var client = ctx.GetS3Client())
            {
                rsp = client.PutObject(request);
            }
        }
Ejemplo n.º 6
0
        public IEnumerable <SimpleFile> GetSimulationInputFiles()
        {
            // S3:URL in description
            string bucketName = "Simulations";
            string key        = job.SimulationId.ToString();
            //string dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "name.bin");
            IAWSContext awsCtx = Turbine.Consumer.AWS.AppUtility.GetContext();

            byte[] bytes;

            using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(awsCtx.AccessKey, awsCtx.SecretKey))
            {
                ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                                                        .WithBucketName(bucketName)
                                                        .WithDelimiter("/")
                                                        .WithPrefix(String.Format("/{0}/StagedInputFiles/", key));

                using (ListObjectsResponse listObjectsResponse = client.ListObjects(listObjectsRequest))
                {
                    foreach (S3Object obj in listObjectsResponse.S3Objects)
                    {
                        GetObjectRequest getObjectRequest = new GetObjectRequest()
                                                            .WithBucketName(bucketName)
                                                            .WithKey(String.Format("/{0}/StagedInputFiles/{1}", key, obj.Key));
                        using (S3Response getObjectResponse = client.GetObject(getObjectRequest))
                        {
                            using (System.IO.Stream s = getObjectResponse.ResponseStream)
                            {
                                using (var ms = new System.IO.MemoryStream())
                                {
                                    s.CopyTo(ms);
                                    bytes = ms.ToArray();
                                }
                            }
                        }
                        var f = new SimpleFile()
                        {
                            content = bytes, name = obj.Key
                        };
                        yield return(f);
                    }
                }
            }
        }
Ejemplo n.º 7
0
 static SQSTraceListener()
 {
     container.LoadConfiguration("aws");
     context = container.Resolve <IAWSContext>();
 }
        /// <summary>
        /// ReceiveRegistration:  Response occurs on a dedicated queue so all messages not
        /// conforming to expected result are deleted and ignored.
        /// </summary>
        /// <param name="registerRequest"></param>
        /// <returns></returns>
        internal static RegistrationInfo ReceiveRegistration(RPCEnvelope registerRequest)
        {
            Turbine.Consumer.Contract.Behaviors.IConsumerContext ctx = Turbine.Consumer.AppUtility.GetConsumerContext();
            IAWSContext awsCtx = Turbine.Consumer.AWS.AppUtility.GetContext();

            var queue = Amazon.AWSClientFactory.CreateAmazonSQSClient(
                awsCtx.AccessKey, awsCtx.SecretKey,
                new Amazon.SQS.AmazonSQSConfig().WithServiceURL(awsCtx.Region)
                );

            Amazon.SQS.Model.ReceiveMessageResponse receiveMsgResponse;
            var entries = new List <Amazon.SQS.Model.DeleteMessageBatchRequestEntry>();

            // NOTE: Should only be 1 message on queue, but grab 'all' and delete 'all'.
            do
            {
                Debug.WriteLine(String.Format("Register: Poll Queue {0} for response", registerRequest.ResponseQueueUrl),
                                registerRequest.Operation);
                receiveMsgResponse = queue.ReceiveMessage(new Amazon.SQS.Model.ReceiveMessageRequest()
                                                          .WithWaitTimeSeconds(20)
                                                          .WithMaxNumberOfMessages(10)
                                                          .WithQueueUrl(registerRequest.ResponseQueueUrl));
            } while (receiveMsgResponse.IsSetReceiveMessageResult() == false || receiveMsgResponse.ReceiveMessageResult.IsSetMessage() == false);

            // Response Message Should Have ResponseQueue & SNS Topic
            List <Amazon.SQS.Model.Message> msgResultMsg = receiveMsgResponse.ReceiveMessageResult.Message;

            Debug.WriteLine(String.Format("Count {0} Messages found", msgResultMsg.Count), registerRequest.Operation);

            if (msgResultMsg.Count == 0)
            {
                return(null);
            }

            RegistrationInfo regInfo = null;

            //Dictionary<string, Object> d;

            // NOTE: Very inefficient, double parsing etc.
            // Consider typing at the RPCEnvelope level
            foreach (Amazon.SQS.Model.Message smsg in msgResultMsg)
            {
                Debug.WriteLine(String.Format("Messages found {0}", smsg.Body), registerRequest.Operation);
                entries.Add(new Amazon.SQS.Model.DeleteMessageBatchRequestEntry()
                            .WithId(smsg.MessageId)
                            .WithReceiptHandle(smsg.ReceiptHandle));
                RPCEnvelope registerResponse = null;
                JToken      token            = JObject.Parse(smsg.Body);
                JToken      jmsg             = token.SelectToken("Message");

                try
                {
                    registerResponse = JsonConvert.DeserializeObject <RPCEnvelope>(smsg.Body);
                    //d = JsonConvert.DeserializeObject<Dictionary<string, Object>>(jmsg.ToString());
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("RPCEnvelope ignore exception deserializing: {0}", ex),
                                    registerRequest.Operation);
                    continue;
                }

                //ack = (Turbine.Orchestrator.AWS.Data.Contract.Messages.RegistrationInfo)rsp.Message;
                var serializer = new Newtonsoft.Json.JsonSerializer();
                Newtonsoft.Json.JsonReader reader = jmsg.CreateReader();
                //var content = d["Message"].ToString();
                var content = jmsg.ToString();
                try
                {
                    regInfo = JsonConvert.DeserializeObject <RegistrationInfo>(content);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(String.Format("RegistrationInfo ignore exception deserializing: {0}", ex),
                                    registerRequest.Operation);
                    continue;
                }

                //registerRequest.Message = regInfo;
                if (regInfo.RequestId != registerRequest.Message.Id)
                {
                    var s = String.Format("Mismatch Request-Response Id : {0} != {1}", regInfo.Id, registerRequest.Message.Id);
                    Debug.WriteLine(s, regInfo.GetType().Name);
                    continue;
                }
                break;
            }

            if (entries.Count > 0)
            {
                // Delete all messages found
                var delMsgBatchRsp = queue.DeleteMessageBatch(
                    new Amazon.SQS.Model.DeleteMessageBatchRequest()
                    .WithQueueUrl(registerRequest.ResponseQueueUrl)
                    .WithEntries(entries.ToArray <Amazon.SQS.Model.DeleteMessageBatchRequestEntry>()));
            }
            return(regInfo);
        }