Пример #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));
        }
Пример #2
0
        private void OnLifeCycleEvent(CancellationTokenSource cancellationToken, LifeCycleEvent evt)
        {
            // Dispose data and signal workflow completed
            if (evt is WorkflowCompleted)
            {
                DisposeData();
                _resetEvent.Set();

                return;
            }

            // Ignore other lifecycle events which aren't related to user cancellation
            if (!cancellationToken.IsCancellationRequested)
            {
                return;
            }

            var data = (WorkflowData)GetWorkflow().Data;

            if (data.ProgressBar != null)
            {
                data.ProgressBar.Message = $"Cancelled after rendering {data.RenderedCount} images";
            }

            _host.TerminateWorkflow(evt.WorkflowInstanceId);
            DisposeData();

            _resetEvent.Set();
        }
Пример #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));
        }
Пример #4
0
        private static void Host_OnLifeCycleEvent(LifeCycleEvent evt)
        {
            // TODO: logging
            // ..

            Console.WriteLine($"{evt.EventTimeUtc}, {evt.GetType().Name}");
        }
Пример #5
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);
        }
        public void PublishNotification(LifeCycleEvent evt)
        {
            if (_outbox.IsAddingCompleted || !_workflowOptions.EnableLifeCycleEventsPublisher)
            {
                return;
            }

            _outbox.Add(evt);
        }
Пример #7
0
        public void PublishNotification(LifeCycleEvent evt)
        {
            if (_outbox.IsAddingCompleted)
            {
                return;
            }

            _outbox.Add(evt);
        }
Пример #8
0
        public async Task PublishNotification(LifeCycleEvent evt)
        {
            if (_subscriber == null)
            {
                throw new InvalidOperationException();
            }

            var data = JsonConvert.SerializeObject(evt, _serializerSettings);
            await _subscriber.PublishAsync(_channel, data);
        }
Пример #9
0
        public async Task PublishNotification(LifeCycleEvent evt)
        {
            var payload = JsonConvert.SerializeObject(evt, _serializerSettings);
            var message = new Message(Encoding.Default.GetBytes(payload))
            {
                Label = evt.Reference
            };

            await _topicClient.SendAsync(message);
        }
Пример #10
0
        private void Host_OnLifeCycleEvent(LifeCycleEvent evt)
        {
            if (evt is WorkflowCompleted)
            {
                var type = evt as WorkflowCompleted;

                _workflowDataMappings.TryRemove(type.WorkflowInstanceId, out string id);

                var file = Path.Combine(_fileConfig.OutputFolder, type.WorkflowInstanceId + ".txt");
                using (StreamReader sr = new StreamReader(file, Encoding.GetEncoding("big5")))
                {
                    var line = sr.ReadLine();

                    _rLogger.WriteDebug($"執行結束 {id}:{line}");
                }

                _fileWatcher.MoveToBackupFolder(file);
                Console.WriteLine($"Life cycle type: {nameof(WorkflowCompleted)}, workflow id {type.WorkflowInstanceId}");
            }
            else if (evt is WorkflowError)
            {
                var type = evt as WorkflowError;
                _workflowDataMappings.TryRemove(type.WorkflowInstanceId, out string id);
                Console.WriteLine($"Life cycle type: {nameof(WorkflowError)}, workflow id {type.WorkflowInstanceId}, message: {type.Message}, step: {type.StepId}, ExecutionPointerId: {type.ExecutionPointerId}");
            }
            //else if (evt is WorkflowSuspended)
            //{
            //    var type = evt as WorkflowSuspended;
            //    Console.WriteLine($"Life cycle type: {nameof(WorkflowSuspended)}, workflow id {type.WorkflowInstanceId}");
            //} else if (evt is WorkflowStarted)
            //{
            //    var type = evt as WorkflowStarted;
            //    Console.WriteLine($"Life cycle type: {nameof(WorkflowStarted)}, workflow id {type.WorkflowInstanceId}");
            //}
            //else if (evt is WorkflowResumed)
            //{
            //    var type = evt as WorkflowResumed;
            //    Console.WriteLine($"Life cycle type: {nameof(WorkflowResumed)}, workflow id {type.WorkflowInstanceId}");
            //}
            //else if (evt is WorkflowTerminated)
            //{
            //    var type = evt as WorkflowTerminated;
            //    Console.WriteLine($"Life cycle type: {nameof(WorkflowTerminated)}, workflow id {type.WorkflowInstanceId}");
            //}
            //else if (evt is StepCompleted)
            //{
            //    var type = evt as StepCompleted;
            //    Console.WriteLine($"Life cycle type: {nameof(StepCompleted)}, workflow id {type.WorkflowInstanceId}, step: {type.StepId}, ExecutionPointerId: {type.ExecutionPointerId}");
            //}
            //else if (evt is StepStarted)
            //{
            //    var type = evt as StepStarted;
            //    Console.WriteLine($"Life cycle type: {nameof(StepStarted)}, workflow id {type.WorkflowInstanceId}, step: {type.StepId}, ExecutionPointerId: {type.ExecutionPointerId}");
            //}
        }
Пример #11
0
 private void NotifySubscribers(LifeCycleEvent evt)
 {
     foreach (var subscriber in _subscribers)
     {
         try
         {
             subscriber(evt);
         }
         catch (Exception ex)
         {
             _logger.LogWarning(default(EventId), ex, $"Error on event subscriber: {ex.Message}");
         }
     }
 }
Пример #12
0
 public Task PublishNotification(LifeCycleEvent evt)
 {
     Task.Run(() =>
     {
         foreach (var subscriber in _subscribers)
         {
             try
             {
                 subscriber(evt);
             }
             catch (Exception ex)
             {
                 _logger.LogWarning(default(EventId), ex, $"Error on event subscriber: {ex.Message}");
             }
         }
     });
     return(Task.CompletedTask);
 }
        public async Task PublishNotification(LifeCycleEvent evt)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                _serializer.Serialize(writer, evt);
                writer.Flush();

                var response = await _client.PutRecordAsync(new PutRecordRequest()
                {
                    StreamName   = _streamName,
                    PartitionKey = evt.WorkflowInstanceId,
                    Data         = stream
                });

                //if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
                //{
                //    _logger.LogWarning($"Failed to send event to Kinesis {response.HttpStatusCode}");
                //}
            }
        }
Пример #14
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.
            }
        }
Пример #15
0
 public void HandleLifeCycleEvent(LifeCycleEvent evt)
 {
     OnLifeCycleEvent?.Invoke(evt);
 }
Пример #16
0
		internal void WireupAutomaticEvents ()
		{
			Type cacheKey = this.GetType ();
			Hashtable eventMethodCache = EventMethodCache;
			ArrayList eventMethodList = eventMethodCache == null ? null : (ArrayList) eventMethodCache [cacheKey];

			if (eventMethodList == null) {
				eventMethodList = new ArrayList ();

				if (!SupportAutoEvents || !AutoEventWireup)
					return;

				Type thisType = typeof (TemplateControl);
				Type voidType = typeof (void);
				Type [] DefaultParams = new Type [] {
				typeof (object),
				typeof (EventArgs) };

                LifeCycleEvent[] _pageEvents = new LifeCycleEvent[] { 
                    LifeCycleEvent.PreInit,
                    LifeCycleEvent.PreLoad,
                    LifeCycleEvent.LoadComplete,
                    LifeCycleEvent.PreRenderComplete,
                    LifeCycleEvent.SaveStateComplete,
                    LifeCycleEvent.InitComplete
                };
                List<LifeCycleEvent> pageEvents = new List<LifeCycleEvent>(_pageEvents);

                bool isPage = Page.GetType().IsAssignableFrom(GetType());

				for (int i = 0; i < methodNames.Length; i++) {
                    
                    // Don't look for page-only events in non-page controls.
                    if (!isPage && pageEvents.Contains((LifeCycleEvent)i))
                        continue;

					string methodName = methodNames [i];
					MethodInfo method;
					bool noParams = false;
					Type type = GetType ();
					do {
						method = type.GetMethod (methodName, bflags, null, DefaultParams, null);
						if (method != null) {
							break;
						}

						type = type.BaseType;
					}
					while (type != thisType);

					if (method == null) {
						type = GetType ();
						do {
							method = type.GetMethod (methodName, bflags, null, Type.EmptyTypes, null);
							if (method != null) {
								noParams = true;
								break;
							}

							type = type.BaseType;
						}
						while (type != thisType);

						if (method == null)
							continue;
					}
					if (method.ReturnType != voidType)
						continue;

					eventMethodList.Add (new EventMethodMap ((LifeCycleEvent) i, method, noParams));
				}
				// We copy to not lock

				Hashtable newEventMethodCache = eventMethodCache == null ? new Hashtable () : (Hashtable) eventMethodCache.Clone ();
				newEventMethodCache [cacheKey] = eventMethodList;
				EventMethodCache = newEventMethodCache;
			}

			foreach (EventMethodMap eventMethod in eventMethodList) {
				EventHandler handler = eventMethod.NoParameters ?
					new NoParamsInvoker (this, eventMethod.Method).FakeDelegate :
					(EventHandler)Delegate.CreateDelegate (typeof (EventHandler), this, eventMethod.Method);

				object eventKey = EventKeys [(int) eventMethod.EventKeyIndex];

				Delegate existing = Events [eventKey];
				if (existing != null && handler.Equals(existing))
					continue;

				switch (eventMethod.EventKeyIndex) {
				case LifeCycleEvent.Init:
					Init += handler;
					break;
#if NET_2_0
				case LifeCycleEvent.PreInit:
					((Page)this).PreInit += handler;
					break;
				case LifeCycleEvent.PreLoad:
					((Page) this).PreLoad += handler;
					break;
				case LifeCycleEvent.LoadComplete:
					((Page) this).LoadComplete += handler;
					break;
				case LifeCycleEvent.PreRenderComplete:
					((Page) this).PreRenderComplete += handler;
					break;
				case LifeCycleEvent.SaveStateComplete:
					((Page) this).SaveStateComplete += handler;
					break;
				case LifeCycleEvent.InitComplete:
					((Page) this).InitComplete += handler;
					break;
#endif
				case LifeCycleEvent.Load:
					Load += handler;
					break;
				case LifeCycleEvent.DataBinding:
					DataBinding += handler;
					break;
				case LifeCycleEvent.PreRender:
					PreRender += handler;
					break;
				case LifeCycleEvent.Disposed:
					Disposed += handler;
					break;
				case LifeCycleEvent.Unload:
					Unload += handler;
					break;
				case LifeCycleEvent.Error:
					Error += handler;
					break;
				case LifeCycleEvent.AbortTransaction:
					AbortTransaction += handler;
					break;
				case LifeCycleEvent.CommitTransaction:
					CommitTransaction += handler;
					break;
				}
			}
		}
Пример #17
0
			public EventMethodMap (LifeCycleEvent EventKeyIndex, MethodInfo Method, bool NoParameters)
			{
				this.EventKeyIndex = EventKeyIndex;
				this.Method = Method;
				this.NoParameters = NoParameters;
			}
Пример #18
0
 public EventMethodMap(LifeCycleEvent EventKeyIndex, MethodInfo Method, bool NoParameters)
 {
     this.EventKeyIndex = EventKeyIndex;
     this.Method        = Method;
     this.NoParameters  = NoParameters;
 }
Пример #19
0
        internal void WireupAutomaticEvents()
        {
            Type      cacheKey         = this.GetType();
            Hashtable eventMethodCache = EventMethodCache;
            ArrayList eventMethodList  = eventMethodCache == null ? null : (ArrayList)eventMethodCache [cacheKey];

            if (eventMethodList == null)
            {
                eventMethodList = new ArrayList();

                if (!SupportAutoEvents || !AutoEventWireup)
                {
                    return;
                }

                Type    thisType      = typeof(TemplateControl);
                Type    voidType      = typeof(void);
                Type [] DefaultParams = new Type [] {
                    typeof(object),
                    typeof(EventArgs)
                };

                LifeCycleEvent[] _pageEvents = new LifeCycleEvent[] {
                    LifeCycleEvent.PreInit,
                    LifeCycleEvent.PreLoad,
                    LifeCycleEvent.LoadComplete,
                    LifeCycleEvent.PreRenderComplete,
                    LifeCycleEvent.SaveStateComplete,
                    LifeCycleEvent.InitComplete
                };
                List <LifeCycleEvent> pageEvents = new List <LifeCycleEvent>(_pageEvents);

                bool isPage = Page.GetType().IsAssignableFrom(GetType());

                for (int i = 0; i < methodNames.Length; i++)
                {
                    // Don't look for page-only events in non-page controls.
                    if (!isPage && pageEvents.Contains((LifeCycleEvent)i))
                    {
                        continue;
                    }

                    string     methodName = methodNames [i];
                    MethodInfo method;
                    bool       noParams = false;
                    Type       type     = GetType();
                    do
                    {
                        method = type.GetMethod(methodName, bflags, null, DefaultParams, null);
                        if (method != null)
                        {
                            break;
                        }

                        type = type.BaseType;
                    }while (type != thisType);

                    if (method == null)
                    {
                        type = GetType();
                        do
                        {
                            method = type.GetMethod(methodName, bflags, null, Type.EmptyTypes, null);
                            if (method != null)
                            {
                                noParams = true;
                                break;
                            }

                            type = type.BaseType;
                        }while (type != thisType);

                        if (method == null)
                        {
                            continue;
                        }
                    }
                    if (method.ReturnType != voidType)
                    {
                        continue;
                    }

                    eventMethodList.Add(new EventMethodMap((LifeCycleEvent)i, method, noParams));
                }
                // We copy to not lock

                Hashtable newEventMethodCache = eventMethodCache == null ? new Hashtable() : (Hashtable)eventMethodCache.Clone();
                newEventMethodCache [cacheKey] = eventMethodList;
                EventMethodCache = newEventMethodCache;
            }

            foreach (EventMethodMap eventMethod in eventMethodList)
            {
                EventHandler handler = eventMethod.NoParameters ?
                                       new NoParamsInvoker(this, eventMethod.Method).FakeDelegate :
                                       (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, eventMethod.Method);

                object eventKey = EventKeys [(int)eventMethod.EventKeyIndex];

                Delegate existing = Events [eventKey];
                if (existing != null && handler.Equals(existing))
                {
                    continue;
                }

                switch (eventMethod.EventKeyIndex)
                {
                case LifeCycleEvent.Init:
                    Init += handler;
                    break;

#if NET_2_0
                case LifeCycleEvent.PreInit:
                    ((Page)this).PreInit += handler;
                    break;

                case LifeCycleEvent.PreLoad:
                    ((Page)this).PreLoad += handler;
                    break;

                case LifeCycleEvent.LoadComplete:
                    ((Page)this).LoadComplete += handler;
                    break;

                case LifeCycleEvent.PreRenderComplete:
                    ((Page)this).PreRenderComplete += handler;
                    break;

                case LifeCycleEvent.SaveStateComplete:
                    ((Page)this).SaveStateComplete += handler;
                    break;

                case LifeCycleEvent.InitComplete:
                    ((Page)this).InitComplete += handler;
                    break;
#endif
                case LifeCycleEvent.Load:
                    Load += handler;
                    break;

                case LifeCycleEvent.DataBinding:
                    DataBinding += handler;
                    break;

                case LifeCycleEvent.PreRender:
                    PreRender += handler;
                    break;

                case LifeCycleEvent.Disposed:
                    Disposed += handler;
                    break;

                case LifeCycleEvent.Unload:
                    Unload += handler;
                    break;

                case LifeCycleEvent.Error:
                    Error += handler;
                    break;

                case LifeCycleEvent.AbortTransaction:
                    AbortTransaction += handler;
                    break;

                case LifeCycleEvent.CommitTransaction:
                    CommitTransaction += handler;
                    break;
                }
            }
        }
Пример #20
0
 public void PublishNotification(LifeCycleEvent evt)
 {
     _outbox.Enqueue(evt);
 }
Пример #21
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));
 }
Пример #22
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));
 }