public AppMetr(
            string token,
            string url,
            IBatchPersister batchPersister = null,
            IJsonSerializer serializer     = null)
        {
            _log.InfoFormat("Start Appmetr for token={0}, url={1}", token, url);

            _token              = token;
            _url                = url;
            _batchPersister     = batchPersister ?? new MemoryBatchPersister();
            _httpRequestService = new HttpRequestService(serializer ?? new BasicJsonSerializer());

            _batchPersister.SetServerId(Guid.NewGuid().ToString());

            _flushTimer = new AppMetrTimer(FlushPeriod, Flush, "FlushJob");
            new Thread(_flushTimer.Start).Start();

            _uploadTimer = new AppMetrTimer(UploadPeriod, Upload, "UploadJob");
            new Thread(_uploadTimer.Start).Start();
        }
Exemple #2
0
        private void Upload()
        {
            lock (_uploadLock)
            {
                if (Log.IsDebugEnabled)
                {
                    Log.Debug("Upload started");
                }

                Batch batch;
                int   uploadedBatchCounter = 0;
                int   allBatchCounter      = 0;
                while ((batch = _batchPersister.GetNext()) != null)
                {
                    allBatchCounter++;

                    if (HttpRequestService.SendRequest(_url, _token, batch))
                    {
                        _batchPersister.Remove();
                        uploadedBatchCounter++;

                        if (Log.IsDebugEnabled)
                        {
                            Log.DebugFormat("Batch {0} successfully uploaded", batch.GetBatchId());
                        }
                    }
                    else
                    {
                        Log.ErrorFormat("Error while upload batch {0}", batch.GetBatchId());
                        break;
                    }
                }

                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("{0} from {1} batches uploaded", uploadedBatchCounter, allBatchCounter);
                }
            }
        }