Esempio n. 1
0
        private int FlushOnce()
        {
            // StackifyLib.Utils.StackifyAPILogger.Log("Calling FlushOnceAsync");

            int messageSize = 0;
            var chunk       = new List <LogMsg>();

            //we only want to do this once at a time but the actual send is done async
            long startMs = (long)DateTime.UtcNow.Subtract(_Epoch).TotalMilliseconds;

            try
            {
                while (true)
                {
                    LogMsg msg;
                    if (_MessageBuffer.TryDequeue(out msg))
                    {
                        //do not log our own messages. This is to prevent any sort of recursion that could happen since calling to send this will cause even more logging to happen
                        if (msg.Msg != null && msg.Msg != null && msg.Msg.Contains("StackifyLib:"))
                        {
                            //skip!
                            continue;
                        }

                        chunk.Add(msg);

                        messageSize++;

                        //if we get something newer than when we started reading, break so it doesn't keep reading perpetually.
                        //Let it finish so the timer can run again and do a new batch

                        if (msg.EpochMs > startMs)
                        {
                            break;
                        }

                        //send this packet in a batch
                        if (messageSize > 100)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (chunk.Any())
                {
                    var response = _LogClient.SendLogsByGroups(chunk.ToArray());

                    if (response != null && response.Exception != null)
                    {
                        Utils.StackifyAPILogger.Log("Requeueing log messages due to error: " + response.Exception.ToString(), true);

                        if (response.IsClientError())
                        {
                            Utils.StackifyAPILogger.Log("#LogQueue Not requeueing log messages due to client error: " + response.StatusCode, true);
                        }
                        else
                        {
                            try
                            {
                                bool messagesSentTooManyTimes = EnqueueForRetransmission(chunk);

                                if (messagesSentTooManyTimes)
                                {
                                    Utils.StackifyAPILogger.Log("#LogQueue Some messages not queued again due to too many failures uploading");
                                }
                            }
                            catch (Exception ex2)
                            {
                                Utils.StackifyAPILogger.Log("#LogQueue Error trying to requeue messages " + ex2.ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                StackifyAPILogger.Log("#LogQueue #FlushOnce failed", ex);

                EnqueueForRetransmission(chunk);
            }

            return(messageSize);
        }
Esempio n. 2
0
        private Task FlushOnceAsync(out int messageSize)
        {
            // StackifyLib.Utils.StackifyAPILogger.Log("Calling FlushOnceAsync");

            messageSize = 0;
            var chunk = new List <LogMsg>();

            //we only want to do this once at a time but the actual send is done async
            long startMs = (long)DateTime.UtcNow.Subtract(_Epoch).TotalMilliseconds;

            try
            {
                while (true)
                {
                    LogMsg msg;
                    if (_MessageBuffer.TryDequeue(out msg))
                    {
                        //do not log our own messages. This is to prevent any sort of recursion that could happen since calling to send this will cause even more logging to happen
                        if (msg.Msg != null & msg.Msg.IndexOf("StackifyLib:") > -1)
                        {
                            //skip!
                            continue;
                        }

                        chunk.Add(msg);

                        messageSize++;

                        //if we get something newer than when we started reading, break so it doesn't keep reading perpetually.
                        //Let it finish so the timer can run again and do a new batch

                        if (msg.EpochMs > startMs)
                        {
                            break;
                        }

                        //send this packet in a batch
                        if (messageSize > 100)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (chunk.Any())
                {
                    return(_LogClient.SendLogsByGroups(chunk.ToArray()).ContinueWith((continuation) =>
                    {
                        if (continuation.Exception != null)
                        {
                            Utils.StackifyAPILogger.Log("Requeueing log messages due to error: " + continuation.Exception.ToString(), true);
                        }

                        if (continuation.Result != null && continuation.Result.Exception != null)
                        {
                            Utils.StackifyAPILogger.Log("Requeueing log messages due to error: " + continuation.Result.Exception.ToString(), true);
                        }

                        if (continuation.Exception != null ||
                            (continuation.Result != null && continuation.Result.Exception != null))
                        {
                            try
                            {
                                bool messagesSentTooManyTimes = false;

                                foreach (var item in chunk)
                                {
                                    item.UploadErrors++;

                                    // try to upload up to 10 times
                                    if (item.UploadErrors < 100)
                                    {
                                        _MessageBuffer.Enqueue(item);
                                    }
                                    else
                                    {
                                        messagesSentTooManyTimes = true;
                                    }
                                }

                                if (messagesSentTooManyTimes)
                                {
                                    Utils.StackifyAPILogger.Log(
                                        "Some messages not queued again due to too many failures uploading");
                                }
                            }
                            catch (Exception ex2)
                            {
                                Utils.StackifyAPILogger.Log("Error trying to requeue messages " + ex2.ToString());
                            }
                        }
                    }));
                }
            }
            catch (Exception ex)
            {
                Utils.StackifyAPILogger.Log(ex.ToString());


                //requeue the messages that errored trying to upload
                try
                {
                    foreach (var item in chunk)
                    {
                        item.UploadErrors++;

                        // try to upload up to 10 times
                        if (item.UploadErrors < 10)
                        {
                            _MessageBuffer.Enqueue(item);
                        }
                    }
                }
                catch (Exception ex2)
                {
                    Utils.StackifyAPILogger.Log(ex2.ToString());
                }
            }


            return(null);
        }