Ejemplo n.º 1
0
        public void ChangeScale(int newScaleSize)
        {
            if (newScaleSize <= 0)
            {
                throw new ArgumentOutOfRangeException("newScaleSize", "Must be Greater than Zero");
            }

            ScaleSize = newScaleSize;

            if (!running)
            {
                return;
            }

            lock (lockWorkers)
            {
                // Scale down
                while (workers.Count > ScaleSize)
                {
                    workers[0].Cancel();
                    workers.RemoveAt(0);
                }

                // Scale up
                while (workers.Count < ScaleSize)
                {
                    var worker = new ServiceWorkerAdapter <TNotification>(this, ServiceConnectionFactory.Create());
                    workers.Add(worker);
                    worker.Start();
                }

                LogAgent.Debug("Scaled Changed to: " + workers.Count);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 开始任务
        /// </summary>
        public void Start()
        {
            WorkerTask = Task.Factory.StartNew(async delegate {
                while (!CancelTokenSource.IsCancellationRequested && !this.IsCompleted)
                {
                    try
                    {
                        //发送任务列表
                        var toSend = new List <Task>();
                        foreach (var n in this.TakeMany())
                        {
                            var t = Connection.Send(n);
                            // Keep the continuation
                            var cont = t.ContinueWith(ct => {
                                var cn = n;
                                var ex = t.Exception;
                                if (ex == null)
                                {
                                    Broker.RaiseNotificationSucceeded(cn);
                                }
                                else
                                {
                                    Broker.RaiseNotificationFailed(cn, ex);
                                }
                            });
                            // Let's wait for the continuation not the task itself
                            toSend.Add(cont);
                        }

                        if (toSend.Count <= 0)
                        {
                            continue;
                        }
                        try
                        {
                            LogAgent.Info("Waiting on all tasks {0}", toSend.Count());
                            await Task.WhenAll(toSend).ConfigureAwait(false);
                            LogAgent.Info("All Tasks Finished");
                        }
                        catch (Exception ex)
                        {
                            LogAgent.Error("Waiting on all tasks Failed: {0}", ex);
                        }
                        LogAgent.Info("Passed WhenAll");
                    }
                    catch (Exception ex)
                    {
                        LogAgent.Error("Broker.Take: {0}", ex);
                    }
                }

                if (CancelTokenSource.IsCancellationRequested)
                {
                    LogAgent.Info("Cancellation was requested");
                }
                if (this.IsCompleted)
                {
                    LogAgent.Info("Broker IsCompleted");
                }
                LogAgent.Debug("Broker Task Ended");
            }, CancelTokenSource.Token, TaskCreationOptions.LongRunning
                                               | TaskCreationOptions.DenyChildAttach, TaskScheduler.Default).Unwrap();

            //工作任务异常的延续任务
            WorkerTask.ContinueWith(t => {
                var ex = t.Exception;
                if (ex != null)
                {
                    LogAgent.Error("ServiceWorkerAdapter.WorkerTask Error: {0}", ex);
                }
            }, TaskContinuationOptions.OnlyOnFaulted);
        }