public IDisposable Subscribe(IObserver <int> observer)
        {
            if (observer == null)
            {
                throw new ArgumentNullException("observer");
            }

            var observerContext = new ObserverContext <int>(observer);

            observerContext.Counter = 0;

            var timer = new Timer(ctx =>
            {
                var context = ctx as ObserverContext <int>;

                if (context == null)
                {
                    var msg = "Unable to start timer delegate. Supplied ObserverContext<int> is either null or is not of the correct type.";

                    var ex = new ArgumentNullException("context", msg);

                    observer.OnError(ex);

                    throw ex;
                }

                try
                {
                    if (context.Counter == _maxNumbersToGenerate)
                    {
                        observer.OnCompleted();
                    }

                    var n = GenerateNumber();

                    ++context.Counter;

                    observer.OnNext(n);
                }
                catch (Exception ex)
                {
                    observer.OnError(ex);
                }
            }, observerContext, _startAfterMilliseconds, _generateEveryMilliseconds);

            observerContext.Timer = timer;

            var subscription = Disposable.Create(() =>
            {
                observerContext?.Timer?.Change(Timeout.Infinite, Timeout.Infinite);
                observerContext?.Timer?.Dispose();
            });

            observerContext.Subscription = subscription;

            _observers.Add(observerContext);

            return(subscription);
        }
Example #2
0
 public static void PushFor(string key, Observation glue, ObserverContext context)
 {
     if (RegisterInstance.For(key) == null) return;
     foreach (var observer in RegisterInstance.For(key))
     {
         observer.Context = context;
         foreach (var observation in observer.GetObservations(glue))
         {
             Push(observation, glue);
         }
     }
 }
Example #3
0
 public override void OnActionExecuted(ActionExecutedContext filterContext)
 {
     var context = new ObserverContext
           {
               {"ControllerName", _controllerName},
               {"ControllerAction", _controllerAction}
           };
     try{
         ThreadPool.QueueUserWorkItem(state =>
         {
             var glue = new ControllerTimeMeasurement(_controllerName, _controllerAction, _sw.ElapsedMilliseconds);
             PulseManager.Push(glue, null);
             PulseManager.PushFor("per_mvc_action_request", glue, context);
         });
     }
     catch{
         //TODO: Handle gracefully i.e. log and restart
     }
     finally{
         _sw.Stop();
     }
     base.OnActionExecuted(filterContext);
 }