示例#1
0
            protected virtual void threadLoop()
            {
                QueueStorageMessage active_message = new QueueStorageMessage();

                bool pending_statements = false;

                while (running || pending_statements == true)
                {
                    pending_statements = false;
                    TLC.Report();
                    try
                    {
                        bool message_found = false;

                        lock (queueStatements)
                        {
                            int statements_count = queueStatements.Count();
                            if (statements_count > 0)
                            {
                                if (statements_count > 1)
                                {
                                    pending_statements = true;
                                }
                                QueueStorageMessage candidate = queueStatements[0];
                                active_message = candidate;
                                queueStatements.Remove(candidate);
                                message_found = true;
                            }
                        }

                        if (message_found)
                        {
                            if (active_message.code == QueueStorageCode.insertTransaction)
                            {
                                insertTransactionInternal((Transaction)active_message.data);
                            }
                            else if (active_message.code == QueueStorageCode.insertBlock)
                            {
                                insertBlockInternal((Block)active_message.data);
                            }
                        }
                        else
                        {
                            // Sleep for 10ms to prevent cpu waste
                            Thread.Sleep(10);
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.error("Exception occured in storage thread loop: " + e);
                    }
                    Thread.Yield();
                }
                cleanupCache();
            }
示例#2
0
        public static async System.Threading.Tasks.Task RunAsync([QueueTrigger("mapqueue", Connection = "AzureWebJobsStorage")] string queueMessage, TraceWriter log)
        {
            try
            {
                // Create the HttpClient
                var client = new HttpClient();

                // Convert Json message back to its original object (lon, lat, blobname, blobcontainerreference)
                QueueStorageMessage queueStorageMessage = JsonConvert.DeserializeObject <QueueStorageMessage>(queueMessage);

                // Retrieve storage account
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));

                // Retrieve container if it exists, create one if not
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                var             container  = blobClient.GetContainerReference("mapblob");
                await container.CreateIfNotExistsAsync();

                // Create the blob
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(queueStorageMessage.blobName);

                // Retrieve the picture for the given city using its lat and lon
                var url = String.Format("https://atlas.microsoft.com/map/static/png?subscription-key={0}&api-version=1.0&center={1},{2}", Environment.GetEnvironmentVariable("MapsAPIKey"), queueStorageMessage.lon, queueStorageMessage.lat);
                client.BaseAddress = new Uri(url);
                HttpResponseMessage responseMessage = await client.GetAsync(url);

                if (responseMessage.IsSuccessStatusCode)
                {
                    Stream stream = await responseMessage.Content.ReadAsStreamAsync();

                    // Check if the the temprature is high enough to have a beer
                    string bier = checkForBier(queueStorageMessage);

                    // Format the string to add the celcius degree symbol
                    string temprature = String.Format("Temp: {0} \u2103", queueStorageMessage.temp.ToString());

                    // Draw on the image using the ImageHelper class
                    Stream renderedImage = ImageHelper.AddTextToImage(stream, (temprature, (10, 20)), (bier, (10, 50)));

                    // Upload the image to the blob
                    await blockBlob.UploadFromStreamAsync(renderedImage);

                    log.Info("City has been found and image was uploaded succesfully");
                }
                else
                {
                    log.Error("Could not retrieve the map for the given coordinates");
                }
            }
            catch
            {
                log.Error("Something went wrong.");
            }
        }
示例#3
0
            public virtual void insertTransaction(Transaction transaction)
            {
                // Make a copy of the transaction for the queue storage message processing
                QueueStorageMessage message = new QueueStorageMessage
                {
                    code = QueueStorageCode.insertTransaction,
                    data = new Transaction(transaction)
                };

                lock (queueStatements)
                {
                    queueStatements.Add(message);
                }
            }
示例#4
0
            public virtual void insertBlock(Block block)
            {
                // Make a copy of the block for the queue storage message processing
                QueueStorageMessage message = new QueueStorageMessage
                {
                    code = QueueStorageCode.insertBlock,
                    data = new Block(block)
                };

                lock (queueStatements)
                {
                    queueStatements.Add(message);
                }
            }
示例#5
0
        public static void insertActivity(Activity activity)
        {
            // Make a copy of the block for the queue storage message processing
            QueueStorageMessage message = new QueueStorageMessage
            {
                code       = QueueStorageCode.insertActivity,
                retryCount = 0,
                data       = activity
            };

            lock (queueStatements)
            {
                queueStatements.Add(message);
            }
        }
示例#6
0
        public static void updateValue(byte[] data, IxiNumber value)
        {
            // Make a copy of the block for the queue storage message processing
            QueueStorageMessage message = new QueueStorageMessage
            {
                code       = QueueStorageCode.updateValue,
                retryCount = 0,
                data       = new MessageDataValue {
                    data = data, value = value
                }
            };

            lock (queueStatements)
            {
                queueStatements.Add(message);
            }
        }
示例#7
0
        public static void updateStatus(byte[] data, ActivityStatus status, ulong block_height)
        {
            // Make a copy of the block for the queue storage message processing
            QueueStorageMessage message = new QueueStorageMessage
            {
                code       = QueueStorageCode.updateStatus,
                retryCount = 0,
                data       = new MessageDataStatus {
                    data = data, status = status, blockHeight = block_height
                }
            };

            lock (queueStatements)
            {
                queueStatements.Add(message);
            }
        }
示例#8
0
        public static string checkForBier(QueueStorageMessage queueStorageMessage)
        {
            const double BIERMIN = 15.00;
            string       bier;

            if (queueStorageMessage.temp >= BIERMIN)
            {
                return(bier = "Time for beer !");
            }
            else if (queueStorageMessage.temp == BIERMIN)
            {
                return(bier = "Water maybe?");
            }
            else
            {
                return(bier = "Id go for a hot chocolate");
            }
        }
示例#9
0
 private void debugDumpCrashObject(QueueStorageMessage message)
 {
     Logging.error("Crashed on message: (code: {0}, retry count: {1})", message.code.ToString(), message.retryCount);
     if (message.retryCount == 1 || message.retryCount >= 10)
     {
         if (message.code == QueueStorageCode.insertBlock)
         {
             debugDumpCrashBlock((Block)message.data);
         }
         else if (message.code == QueueStorageCode.insertTransaction)
         {
             debugDumpCrashTX((Transaction)message.data);
         }
         else
         {
             Logging.error("Message is 'updateTXAppliedFlag'.");
         }
     }
 }
示例#10
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestMessage req, TraceWriter log)
        {
            try
            {
                //Get streetname from query parameter
                string city = req.GetQueryNameValuePairs()
                              .FirstOrDefault(q => string.Compare(q.Key, "city", true) == 0)
                              .Value;

                if (city == null)
                {
                    dynamic data = await req.Content.ReadAsAsync <object>();

                    city = data?.city;
                }

                //Get country from query parameter
                string country = req.GetQueryNameValuePairs()
                                 .FirstOrDefault(q => string.Compare(q.Key, "country", true) == 0)
                                 .Value;

                if (country == null)
                {
                    dynamic data = await req.Content.ReadAsAsync <object>();

                    country = data?.country;
                }

                //Als er wel een stad en land is ingevuld
                if (country != null && city != null)
                {
                    //Google api key ophalen en API url opstellen om te checken of de combinatie bestaat en de long en lat op te halen.
                    string googleapikey = Environment.GetEnvironmentVariable("GoogleAPIkey");
                    var    url          = String.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0},+{1}&key={2}", city, country, googleapikey);
                    log.Info("Google api link: " + url);
                    using (var client = new HttpClient())
                    {
                        //Wachten op een response
                        client.BaseAddress = new Uri(url);
                        HttpResponseMessage response = await client.GetAsync(url);

                        //Als de response een sucess was
                        if (response.IsSuccessStatusCode)
                        {
                            //String ophalen en omzetten naar een json object
                            string strResult = await response.Content.ReadAsStringAsync();

                            dynamic googlemapsobject = JsonConvert.DeserializeObject <dynamic>(strResult);

                            //De status uit het json object lezen
                            string status = (string)googlemapsobject.status;

                            //Als er geen resultaten waren voor de opgegeven combinatie
                            if (status == "ZERO_RESULTS")
                            {
                                return(req.CreateErrorResponse(HttpStatusCode.NotFound, "Your combination of city and country could not be found, please enter a valid city and country!"));
                            }

                            //Als er wel resultaten waren
                            else
                            {
                                //Geocoordinaten ophalen voor de locatie
                                string latitude   = googlemapsobject.results[0].geometry.location.lat;
                                string longtitude = googlemapsobject.results[0].geometry.location.lng;

                                //Bloblocatie opstellen
                                string blobname = String.Format("Generatedmap-{0},{1}-{2}.png", city, country, DateTime.Now.ToFileTime());
                                string blobcontainerreference = "mapblob";
                                log.Info(Environment.GetEnvironmentVariable("StorageConnectionString"));
                                CloudStorageAccount account = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("StorageConnectionString") + ";EndpointSuffix=core.windows.net");
                                string blobUrl = account.BlobStorageUri.PrimaryUri.AbsoluteUri + blobcontainerreference + "/" + blobname;
                                log.Info("bloburl= " + blobUrl);

                                //Object voor het doorsturen naar de queue aanmaken
                                QueueStorageMessage message = new QueueStorageMessage(longtitude, latitude, blobname, blobcontainerreference);

                                //Als json string doorsturen naar de queue storage
                                string json = JsonConvert.SerializeObject(message);
                                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("StorageConnectionString") + ";EndpointSuffix=core.windows.net");
                                CloudQueueClient    queueClient    = storageAccount.CreateCloudQueueClient();
                                CloudQueue          queue          = queueClient.GetQueueReference("bierapi-queue");
                                queue.CreateIfNotExists();
                                CloudQueueMessage queueumessage = new CloudQueueMessage(json);
                                queue.AddMessage(queueumessage);

                                var myObj        = new { status = "request made", url = blobUrl, message = "Your requested map will be available at the url shortly" };
                                var jsonToReturn = JsonConvert.SerializeObject(myObj);

                                return(new HttpResponseMessage(HttpStatusCode.OK)
                                {
                                    Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
                                });
                            }
                        }
                    }
                }

                else
                {
                    var myObj        = new { status = "ERROR", message = "Please use a valid country name and city name!" };
                    var jsonToReturn = JsonConvert.SerializeObject(myObj);

                    return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json")
                    });
                }
            }

            catch (Exception ex)
            {
                log.Info("Exception" + ex.Message + " " + ex.InnerException);
            }

            return(null);
        }
示例#11
0
        public static void threadLoop()
        {
            QueueStorageMessage active_message = new QueueStorageMessage();

            bool pending_statements = false;

            while (running || pending_statements == true)
            {
                bool message_found = false;
                pending_statements = false;
                TLC.Report();
                try
                {
                    lock (queueStatements)
                    {
                        int statements_count = queueStatements.Count();
                        if (statements_count > 0)
                        {
                            if (statements_count > 1)
                            {
                                pending_statements = true;
                            }
                            QueueStorageMessage candidate = queueStatements[0];
                            active_message = candidate;
                            message_found  = true;
                        }
                    }

                    if (message_found)
                    {
                        if (active_message.code == QueueStorageCode.insertActivity)
                        {
                            insertActivityInternal((Activity)active_message.data);
                        }
                        else if (active_message.code == QueueStorageCode.updateStatus)
                        {
                            MessageDataStatus mds = (MessageDataStatus)active_message.data;
                            updateStatusInternal(mds.data, mds.status, mds.blockHeight);
                        }
                        else if (active_message.code == QueueStorageCode.updateValue)
                        {
                            MessageDataValue mdv = (MessageDataValue)active_message.data;
                            updateValueInternal(mdv.data, mdv.value);
                        }
                        lock (queueStatements)
                        {
                            queueStatements.RemoveAt(0);
                        }
                    }
                    else
                    {
                        // Sleep for 50ms to yield CPU schedule slot
                        Thread.Sleep(50);
                    }
                }
                catch (Exception e)
                {
                    Logging.error("Exception occured in Activity storage thread loop: " + e);
                    if (message_found)
                    {
                        active_message.retryCount += 1;
                        if (active_message.retryCount > 10)
                        {
                            lock (queueStatements)
                            {
                                queueStatements.RemoveAt(0);
                            }
                            Logging.error("Too many retries, aborting...");
                            shutdown();
                            throw new Exception("Too many Activity storage retries. Aborting storage thread.");
                        }
                    }
                }
                Thread.Yield();
            }
            shutdown();
        }
示例#12
0
            protected virtual void threadLoop()
            {
                QueueStorageMessage active_message = new QueueStorageMessage();

                bool pending_statements = false;

                while (running || pending_statements == true)
                {
                    bool message_found = false;
                    pending_statements = false;
                    TLC.Report();
                    try
                    {
                        lock (queueStatements)
                        {
                            int statements_count = queueStatements.Count();
                            if (statements_count > 0)
                            {
                                if (statements_count > 1)
                                {
                                    pending_statements = true;
                                }
                                QueueStorageMessage candidate = queueStatements[0];
                                active_message = candidate;
                                message_found  = true;
                            }
                        }

                        if (message_found)
                        {
                            if (active_message.code == QueueStorageCode.insertTransaction)
                            {
                                insertTransactionInternal((Transaction)active_message.data);
                            }
                            else if (active_message.code == QueueStorageCode.insertBlock)
                            {
                                insertBlockInternal((Block)active_message.data);
                            }
                            lock (queueStatements)
                            {
                                queueStatements.RemoveAt(0);
                            }
                        }
                        else
                        {
                            long cur_time = Clock.getTimestamp();
                            if (cur_time - lastCleanupPass > 60)
                            {
                                lastCleanupPass = cur_time;
                                // this is only enabled on Rocks for now
                                if (this is RocksDBStorage)
                                {
                                    cleanupCache();
                                }
                            }
                            // Sleep for 50ms to yield CPU schedule slot
                            Thread.Sleep(50);
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.error("Exception occured in storage thread loop: " + e);
                        if (message_found)
                        {
                            debugDumpCrashObject(active_message);
                            active_message.retryCount += 1;
                            if (active_message.retryCount > 10)
                            {
                                lock (queueStatements)
                                {
                                    queueStatements.RemoveAt(0);
                                }
                                Logging.error("Too many retries, aborting...");
                                shutdown();
                                throw new Exception("Too many storage retries. Aborting storage thread.");
                            }
                        }
                    }
                    Thread.Yield();
                }
                shutdown();
                Logging.info("Storage stopped.");
            }