public Subscription(
     IProcessInstancesClient userTaskClient,
     OnProcessInstanceCallback callback,
     InstanceSubscriptionSettings subscriptionSettings)
 {
     _instanceClient       = userTaskClient ?? throw new ArgumentNullException(nameof(userTaskClient));
     _callback             = callback ?? throw new ArgumentNullException(nameof(callback));
     _subscriptionSettings = subscriptionSettings ?? throw new ArgumentNullException(nameof(subscriptionSettings));
     _stopSignal           = new CancellationTokenSource();
 }
예제 #2
0
        public void SubscribeForProcessInstanceStateChange(string processId, Func <ProcessInstance, ProcessState> action)
        {
            var subscriptionSettings = new InstanceSubscriptionSettings
            {
                SubscribeOnce  = false,
                ConfigureQuery = o =>
                {
                    o.FilterByProcessInstanceId(processId);
                    o.FilterByStates(ProcessState.Error, ProcessState.Finished, ProcessState.Terminated);
                }
            };

            var handledInstance = ProcessState.Running;

            Func <ProcessState> getRecentInstanceState = () => handledInstance;

            Action <ProcessState> updateRecentInstanceState = instance => { handledInstance = instance; };

            void Callback(IEnumerable <ProcessInstance> instances)
            {
                try
                {
                    var processInstances = instances.ToList();

                    switch (processInstances.ToList().Count)
                    {
                    case 0: return;
                    }

                    var instance = processInstances.First();

                    if (!instance.State.Equals(getRecentInstanceState()))
                    {
                        updateRecentInstanceState(action(instance));
                    }

                    _logger.Log(LogLevel.Debug,
                                $"Handled instance state change: {processId}:{getRecentInstanceState()}");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

            _instanceClient.SubscribeForProcessesInstances(Callback, subscriptionSettings);
        }
        public static ISubscription SubscribeForProcessesInstances(
            this IProcessInstancesClient client,
            OnProcessInstanceCallback callback,
            InstanceSubscriptionSettings subscriptionSettings
            )
        {
            var subscriptionSettings1 = subscriptionSettings ?? new InstanceSubscriptionSettings();

            if (subscriptionSettings1.FetchInterval <= TimeSpan.Zero)
            {
                throw new ArgumentException(
                          $"Fetch interval '{subscriptionSettings1.FetchInterval}' should be positive.");
            }

            var subscription = new Subscription(client, callback, subscriptionSettings1);

            subscription.StartAsync();
            return(subscription);
        }