private void _eventQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
 {
     if (!IsExiting())
     {
         if (e.Message?.Body != null && e.Message.Body is RFWorkQueueItem)
         {
             var wki = e.Message.Body as RFWorkQueueItem;
             if (wki.Item is RFEvent)
             {
                 var evt = wki.Item as RFEvent;
                 Log.Debug(this, "Received event {0} from MSMQ", evt);
                 _eventSink.RaiseEvent(this, evt, wki.ProcessingKey);
             }
             else if (wki.Item is RFInstruction)
             {
                 var ins = wki.Item as RFInstruction;
                 Log.Debug(this, "Received instruction {0} from MSMQ", ins);
                 _instructionSink.QueueInstruction(this, ins, wki.ProcessingKey);
             }
             else
             {
                 Log.Warning(this, "Unknown item type on MSMQ event queue: {0}", wki.Item.GetType().FullName);
             }
         }
         _eventQueue.BeginReceive();
     }
 }
예제 #2
0
        protected override void Run()
        {
            var prevNow = DateTime.Now;

            if (_intervalLength > 1000)
            {
                // if interval is longer than a second, align ticks to next full minute
                var secondsToAlign = 60 - prevNow.TimeOfDay.Seconds;
                for (int n = 0; n < secondsToAlign && !IsExiting(); ++n)
                {
                    Thread.Sleep(1000);
                }
            }
            while (!IsExiting())
            {
                if (!_isSuspended)
                {
                    var now      = DateTime.Now;
                    var interval = new RFInterval(prevNow, now);
                    if (!IsDowntime(interval))
                    {
                        _eventManager.RaiseEvent(this, new RFIntervalEvent(interval), null);
                        prevNow = now;
                    }
                }

                // sleep one second max at a time
                for (int n = 0; n < _intervalLength / 1000 && !IsExiting(); ++n)
                {
                    Thread.Sleep(1000);
                }
            }
        }
예제 #3
0
        private void ProcessInstructionThread(RFWorkQueueItem i)
        {
            try
            {
                // To make transaction work we'd have to use a single SQL connection to do all reads and writes

                /* using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions
                 * {
                 *  IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted,
                 *  Timeout = TimeSpan.FromSeconds(30)
                 * }))*/
                {
                    Log.Debug(this, "Started thread to process instruction {0}", i.Item as RFProcessInstruction);
                    var sink = new RFBufferingSink(); // create thread-local manager to buffer events and instructions, then send them back in bulk
                    RFProcessingResult result = null;
                    try
                    {
                        result = _context.Engine.Process(i.Item as RFInstruction, _context.GetProcessingContext(i.ProcessingKey, sink, sink, null));
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(this, ex, "Exception Thread processing queue item ", i);
                        result = RFProcessingResult.Error(new string[] { ex.Message }, ex is DbException || ex is TimeoutException);
                    }
                    // send result and all buffered events/instructions to external event manager (since
                    // we can't guarantee delivery order with MSMQ)
                    _eventSink.RaiseEvent(this, new RFProcessingFinishedEvent(i, result, sink.GetItems()), i.ProcessingKey);
                    //ts.Complete();
                }
            }
            catch (Exception ex)
            {
                Log.Exception(this, ex, "Exception Thread processing queue item ", i);
            }
        }
예제 #4
0
 public void RaiseEvent(object raisedBy, RFEvent evt)
 {
     _events.RaiseEvent(raisedBy, evt, ProcessingKey);
 }