Beispiel #1
0
        /// <inheritdoc />
        public void Stop()
        {
            lock (this)
            {
                if (_fullyStopped)
                {
                    return;
                }

                IsRunning = false;

                // see PipelineExecution_Thread, basically we release for every thread
                // so every thread is able to stop
                // when stopping, every thread consumes exactly one semaphore
                _queuedSemaphore.Release(Executors.Length);

                foreach (var executor in Executors)
                {
                    executor.Join();
                }

                _fullyStopped = true;
            }

            LifeCycleEvent?.Invoke(this, new ExecutorLifeCycleEventArgs(ExecutorLifeCycleEventType.Stop));
        }
Beispiel #2
0
        /// <inheritdoc />
        public bool Add(IFileSource fileSource)
        {
            if (fileSource == null)
            {
                throw new ArgumentNullException(nameof(fileSource));
            }

            lock (this)
            {
                if (!IsRunning)
                {
                    return(false);
                }

                lock (ActiveFileSources)
                {
                    if (!TryAddConflicting(fileSource))
                    {
                        if (!PendingFileSources.TryAdd(fileSource))
                        {
                            return(false);
                        }

                        _queuedSemaphore.Release();
                    }
                }
            }

            LifeCycleEvent?.Invoke(this,
                                   new ExecutorLifeCycleEventArgs(ExecutorLifeCycleEventType.SourceAdded, fileSource));

            return(true);
        }
Beispiel #3
0
        /// <inheritdoc />
        /// <summary>
        ///		This method assigns the given pipeline once (i.e. cannot be changed afterwards), and creates and starts the threads.
        ///		Calling this method a second time will be ignored.
        /// </summary>
        public void Initialize(IEnumerable <IDataPipelineOrBuilder> pipeline)
        {
            lock (this)
            {
                if (Initialized)
                {
                    return;
                }

                Pipeline = pipeline ?? throw new ArgumentNullException(nameof(pipeline));

                IsRunning = true;

                for (int i = 0; i < Executors.Length; i++)
                {
                    Executors[i] = new Thread(PipelineExecution_Thread);
                    Executors[i].Start();
                }

                Initialized = true;
            }

            Log.Info("Executor has been initialized.");

            LifeCycleEvent?.Invoke(this, new ExecutorLifeCycleEventArgs(ExecutorLifeCycleEventType.Initialize));
        }
Beispiel #4
0
        /// <summary>
        /// This method actually processes the sources, invokes all required events, and handles errors.
        /// </summary>
        /// <param name="source">The source that will be executed.</param>
        protected virtual void ProcessSource(IFileSource source)
        {
            LifeCycleEvent?.Invoke(this,
                                   new ExecutorLifeCycleEventArgs(ExecutorLifeCycleEventType.SourceExecutionStart, source));

            Log.DebugFormat("Starting to process {0}.", source);

            bool error = false;

            foreach (var p in Pipeline)
            {
                try
                {
                    p.Build().Execute(source);
                }
                catch (Exception e)
                {
                    error     = true;
                    IsRunning = !DieOnException;                     // prevent new sources from being added (if DieOnException)

                    LifeCycleEvent?.Invoke(this, new ExecutorLifeCycleEventArgs(p, source, e));
                    Log.WarnFormat("Source {0} has thrown an exception.", source);
                    if (AbortPipelineOnError)
                    {
                        break;
                    }
                }
            }

            LifeCycleEvent?.Invoke(this,
                                   new ExecutorLifeCycleEventArgs(ExecutorLifeCycleEventType.SourceExecutionFinished, source));

            if (error && DieOnException)
            {
                Log.Warn("Executor is about to stop due to an exception ...");
                new Thread(Stop).Start();                 // call stop from another thread, so that it won't cause a deadlock.
            }
        }
Beispiel #5
0
 /// <summary>
 ///     Execute a lifecycle event with given parameters.
 /// </summary>
 /// <param name="eventType">The type of the event.</param>
 protected virtual void ExecuteEvent(SchedulerLifeCycleEventType eventType)
 {
     LifeCycleEvent?.Invoke(this, new SchedulerLifeCycleEventArgs(IsRunning, eventType));
 }
Beispiel #6
0
 /// <summary>
 ///     Execute a lifecycle event with given parameters.
 /// </summary>
 /// <param name="eventType">The type of the event.</param>
 /// <param name="source">The source that is processed by this event. May be <code>null</code>.</param>
 protected virtual void ExecuteEvent(SchedulerLifeCycleEventType eventType, IFileSource source = null)
 {
     LifeCycleEvent?.Invoke(this, new SchedulerLifeCycleEventArgs(IsRunning, source, eventType));
 }