/// <summary>
        /// Creates a PollingFileSystemEventProducer
        /// </summary>		
        /// <param name="directory">The directory to watch</param>
        /// <param name="channel">The channel where events should be sent</param>
        /// <param name="scheduler">Event scheduler</param>
        /// <param name="fiber">Fiber to schedule on</param>
        /// <param name="checkInterval">The maximal time between events or polls on a given file</param>
        /// <param name="checkSubDirectory">Indicates if subdirectorys will be checked or ignored</param>
        public PollingFileSystemEventProducer(string directory, UntypedChannel channel, [NotNull] Scheduler scheduler, Fiber fiber, TimeSpan checkInterval, bool checkSubDirectory)
        {
            if (scheduler == null)
                throw new ArgumentNullException("scheduler");

            _directory = directory;
            _channel = channel;
            _fiber = fiber;
            _hashes = new Dictionary<string, Guid>();
            _scheduler = scheduler;
            _checkInterval = checkInterval;

            _scheduledAction = scheduler.Schedule(3.Seconds(), _fiber, HashFileSystem);

            var myChannel = new ChannelAdapter();

            _connection = myChannel.Connect(connectionConfigurator =>
            {
                connectionConfigurator.AddConsumerOf<FileSystemChanged>().UsingConsumer(HandleFileSystemChangedAndCreated);
                connectionConfigurator.AddConsumerOf<FileSystemCreated>().UsingConsumer(HandleFileSystemChangedAndCreated);
                connectionConfigurator.AddConsumerOf<FileSystemRenamed>().UsingConsumer(HandleFileSystemRenamed);
                connectionConfigurator.AddConsumerOf<FileSystemDeleted>().UsingConsumer(HandleFileSystemDeleted);
            });

            _fileSystemEventProducer = new FileSystemEventProducer(directory, myChannel, checkSubDirectory);
        }
Example #2
0
        public void Stop()
        {
            if (_scheduled != null)
            {
                _scheduled.Cancel();
                _scheduled = null;
            }

            if (_scheduler != null)
            {
                _scheduler.Stop(60.Seconds());
                _scheduler = null;
            }

            if (_fiber != null)
            {
                _fiber.Shutdown(60.Seconds());
            }

            if (_unsubscribeAction != null)
            {
                _unsubscribeAction();
                _unsubscribeAction = null;
            }
        }
Example #3
0
        private void CheckExistingTimeouts()
        {
            DateTime now = SystemUtil.UtcNow;

            _log.DebugFormat("TimeoutService Checking For Existing Timeouts: {0}", now.ToLocalTime());

            try
            {
                var sagas = _repository.Where(x => x.TimeoutAt < now && x.CurrentState == TimeoutSaga.WaitingForTime).ToArray();
                foreach (TimeoutSaga saga in sagas)
                {
                    TimeoutSaga instance = saga;

                    _fiber.Add(() => PublishTimeoutExpired(instance.TimeoutId, instance.Tag));
                }
            }
            catch (Exception ex)
            {
                _log.Error("Error rescheduling existing timeouts", ex);
            }
            finally
            {
                if (_unschedule != null)
                {
                    _unschedule.Cancel();
                }

                _log.DebugFormat("Scheduling next check at " + DateTime.Now);
                _unschedule = _scheduler.Schedule(1000, _fiber, CheckExistingTimeouts);
            }
        }
Example #4
0
        public void Should_run_the_action_until_disabled()
        {
            Fiber     fiber     = new PoolFiber();
            Scheduler scheduler = new TimerScheduler(new PoolFiber());

            Stopwatch elapsed = Stopwatch.StartNew();

            int count  = 0;
            var called = new Future <int>();
            var failed = new Future <bool>();

            ScheduledOperation scheduledAction = null;

            scheduledAction = scheduler.Schedule(TimeSpan.Zero, 100.Milliseconds(), fiber, () =>
            {
                count++;
                if (count == 10)
                {
                    called.Complete(count);
                    scheduledAction.Cancel();
                }
                else if (count > 10)
                {
                    failed.Complete(true);
                }
            });

            called.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();

            elapsed.Stop();

            failed.WaitUntilCompleted(200.Milliseconds()).ShouldBeFalse();

            Trace.WriteLine("Time Period: " + elapsed.ElapsedMilliseconds);
        }
Example #5
0
        /// <summary>
        /// Creates a PollingFileSystemEventProducer
        /// </summary>
        /// <param name="directory">The directory to watch</param>
        /// <param name="channel">The channel where events should be sent</param>
        /// <param name="scheduler">Event scheduler</param>
        /// <param name="fiber">Fiber to schedule on</param>
        /// <param name="checkInterval">The maximal time between events or polls on a given file</param>
        /// <param name="checkSubDirectory">Indicates if subdirectorys will be checked or ignored</param>
        public PollingFileSystemEventProducer(string directory, UntypedChannel channel, [NotNull] Scheduler scheduler, Fiber fiber,
                                              TimeSpan checkInterval, bool checkSubDirectory)
        {
            if (scheduler == null)
            {
                throw new ArgumentNullException("scheduler");
            }

            _directory     = directory;
            _channel       = channel;
            _fiber         = fiber;
            _hashes        = new Dictionary <string, Guid>();
            _scheduler     = scheduler;
            _checkInterval = checkInterval;

            _scheduledAction = scheduler.Schedule(3.Seconds(), _fiber, HashFileSystem);

            var myChannel = new ChannelAdapter();

            _connection = myChannel.Connect(connectionConfigurator =>
            {
                connectionConfigurator.AddConsumerOf <FileSystemChanged>().UsingConsumer(HandleFileSystemChangedAndCreated);
                connectionConfigurator.AddConsumerOf <FileSystemCreated>().UsingConsumer(HandleFileSystemChangedAndCreated);
                connectionConfigurator.AddConsumerOf <FileSystemRenamed>().UsingConsumer(HandleFileSystemRenamed);
                connectionConfigurator.AddConsumerOf <FileSystemDeleted>().UsingConsumer(HandleFileSystemDeleted);
            });

            _fileSystemEventProducer = new FileSystemEventProducer(directory, myChannel, checkSubDirectory);
        }
Example #6
0
        public void Stop()
        {
            _scheduled.Cancel();
            _scheduled = null;

            lock (_workers)
                _workers.Clear();

            _unsubscribeAction();
        }
Example #7
0
        public void Start(IServiceBus bus)
        {
            _bus = bus;

            _unsubscribeAction = bus.SubscribeHandler <WorkerAvailable <TMessage> >(Consume);

            // don't plan to unsubscribe this since it's an important thing
            bus.SubscribeInstance(this);

            _scheduled = _scheduler.Schedule(_pingTimeout, _pingTimeout, _fiber, PingWorkers);

            _bus.Publish(new PingWorker());
        }
Example #8
0
        void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            if (disposing)
            {
                _scheduledAction.Cancel();
                _scheduledAction = null;
            }

            _disposed = true;
        }
Example #9
0
        public void Start(IServiceBus bus)
        {
            _bus = bus;

            _controlUri = _bus.ControlBus.Endpoint.Address.Uri;
            _dataUri = _bus.Endpoint.Address.Uri;

            _unsubscribe = _bus.ControlBus.SubscribeInstance(this);

            var message = new EndpointCameOnline(SystemId, _controlUri, _dataUri, _heartbeatIntervalInSeconds);
            _bus.ControlBus.Publish(message);

            _unschedule = _scheduler.Schedule(_heartbeatIntervalInMilliseconds, _heartbeatIntervalInMilliseconds, _fiber,
                PublishHeartbeat);
        }
Example #10
0
        public void Start(IServiceBus bus)
        {
            _bus        = bus;
            _controlBus = bus.ControlBus;

            _dataUri    = _bus.Endpoint.Address.Uri;
            _controlUri = _controlBus.Endpoint.Address.Uri;

            _unsubscribeAction  = bus.ControlBus.SubscribeHandler <ConfigureWorker>(Consume, Accept);
            _unsubscribeAction += bus.ControlBus.SubscribeHandler <PingWorker>(Consume);

            _unsubscribeAction += bus.SubscribeInstance(this);

            _scheduler = new TimerScheduler(new PoolFiber());
            _scheduled = _scheduler.Schedule(3.Seconds(), 3.Seconds(), _fiber, PublishWorkerAvailability);
        }
        void HashFileSystem()
        {
            try
            {
                Dictionary <string, Guid> newHashes = new Dictionary <string, Guid>();

                ProcessDirectory(newHashes, _directory);

                // process all the new hashes found
                newHashes.ToList().ForEach(x => HandleHash(x.Key, x.Value));

                // remove any hashes we couldn't process
                _hashes.Where(x => !newHashes.ContainsKey(x.Key)).ToList().ConvertAll(x => x.Key).ForEach(RemoveHash);
            }
            finally
            {
                _scheduledAction = _scheduler.Schedule(_checkInterval, _fiber, HashFileSystem);
            }
        }
Example #12
0
 /// <inheritdoc/>
 public bool TryScheduleServiceCall <T>(CredentialModel elevation, int priority,
                                        Func <Session, Task <T> > serviceCall, Func <Exception, bool> handler,
                                        TimeSpan?timeout, CancellationToken?ct, out Task <T> completion)
 {
     if (!_cts.IsCancellationRequested)
     {
         var op = new ScheduledOperation <T>(serviceCall, handler, elevation,
                                             timeout ?? _opTimeout, ct);
         _queue.Enqueue(priority, op);
         // Notify of new operation enqueued.
         _enqueueEvent.TrySetResult(true);
         // Return completion event to wait for
         completion = op.Completed;
         return(true);
     }
     // Session is closed - indicate to caller to schedule on different session.
     completion = null;
     return(false);
 }
Example #13
0
 void ScheduleNextSend()
 {
     _scheduledAction = _scheduler.Schedule(Interval, _fiber, SendMessagesToOutputChannel);
 }
        void HashFileSystem()
        {
            try
            {
                var newHashes = new Dictionary<string, Guid>();

                ProcessDirectory(newHashes, _directory);

                // process all the new hashes found
                newHashes.ToList().ForEach(x => HandleHash(x.Key, x.Value));

                // remove any hashes we couldn't process
                _hashes.Where(x => !newHashes.ContainsKey(x.Key)).ToList().ConvertAll(x => x.Key).ForEach(RemoveHash);
            }
            finally
            {
                _scheduledAction = _scheduler.Schedule(_checkInterval, _fiber, HashFileSystem);
            }
        }
Example #15
0
 public void ScheduleTimeout(Func <PendingReceiveImpl <TMessage>, ScheduledOperation> scheduleAction)
 {
     _scheduledAction = scheduleAction(this);
 }