コード例 #1
0
ファイル: SocketConnector.cs プロジェクト: pents/FileGate
        public async Task StartClient(Uri uri)
        {
            OnBeforeStart?.Invoke();
            await _client.ConnectAsync(uri, _tokenSource.Token);

            //Console.WriteLine($"Current file URL: http://localhost:8095/File/{_currentClientId}");

            StartListening(_client, token: _tokenSource.Token).ConfigureAwait(false);

            OnAfterStart?.Invoke();
        }
コード例 #2
0
        protected void OnAfterStarting()
        {
            OnAfterStart?.Invoke();
            if (AfterStarting == null)
            {
                return;
            }
            var exceptions = InvokeServerEventHandlers(AfterStarting.GetInvocationList().Reverse().Cast <ServerEventHandler>());

            if (exceptions.Count > 0)
            {
                throw new AggregateException(exceptions);
            }
        }
コード例 #3
0
        public void Start()
        {
            if (IsListening || IsStarting)
            {
                return;
            }
            if (IsStopping)
            {
                throw new UnableToStartHostException("Cannot start server until server has finished stopping");
            }
            IsStarting = true;

            try
            {
                OnBeforeStart?.Invoke();
                if (Router.RoutingTable.Count == 0)
                {
                    Router.ScanAssemblies();
                }

                Listener.Prefixes?.Add(ListenerPrefix);
                Listener.Start();
                if (!TestingMode)
                {
                    Listening.Start();

                    Workers = new Thread[_connections * Environment.ProcessorCount];
                    for (var i = 0; i < Workers.Length; i++)
                    {
                        Workers[i] = new Thread(Worker);
                        Workers[i].Start();
                    }
                }

                Logger.Trace($"Listening: {ListenerPrefix}");
                if (IsListening)
                {
                    OnAfterStart?.Invoke();
                }
            }
            catch (Exception e)
            {
                throw new UnableToStartHostException($"An error occured when trying to start the {GetType().FullName}", e);
            }
            finally
            {
                IsStarting = false;
            }
        }
コード例 #4
0
        public void Start()
        {
            if (_host == null)
            {
                lock (_hostSync)
                {
                    if (_host == null)
                    {
                        IDisposable host = null;

                        try
                        {
                            OnBeforeStart?.Invoke(this, EventArgs.Empty);

                            host = WebApp.Start(_baseAddress, Startup);

                            OnAfterStart?.Invoke(this, EventArgs.Empty);
                        }
                        catch (Exception exception)
                        {
                            try
                            {
                                host?.Dispose();
                            }
                            catch
                            {
                            }

                            throw new AggregateException(Resources.CannotStartServiceCorrectly, exception);
                        }

                        _host = host;
                    }
                }
            }
        }
コード例 #5
0
ファイル: RESTServer.cs プロジェクト: gitter-badger/Grapevine
        /// <summary>
        /// Starts the server
        /// </summary>
        public void Start()
        {
            if (IsListening)
            {
                return;
            }
            try
            {
                if (Router.RoutingTable.Count == 0)
                {
                    Router.RegisterAssembly();
                }

                OnBeforeStart?.Invoke();

                _listener.Prefixes.Add(Origin);
                _listener.Start();
                _listening.Start();

                _workers = new Thread[_connections * Environment.ProcessorCount];
                for (var i = 0; i < _workers.Length; i++)
                {
                    _workers[i] = new Thread(Worker);
                    _workers[i].Start();
                }

                if (IsListening)
                {
                    OnAfterStart?.Invoke();
                }
            }
            catch (Exception e)
            {
                throw new CantStartHostException($"An error occured when trying to start the {GetType().FullName}", e);
            }
        }
コード例 #6
0
ファイル: TaskPool.cs プロジェクト: geniussheep/MyTest
        private void ExecuteTask()
        {
            var    currentTaskId = Thread.CurrentThread.ManagedThreadId;
            string debugStr      = $"Thread -- {currentTaskId}";

            while (!_isFinished && !_cts.IsCancellationRequested)
            {
                LogInfoWriter.GetInstance().Info($"{debugStr} --Check Start [taskInfoList:{_taskInfoList.Count()}, hasCompleteTaskInfoList:{_hasCompleteTaskInfoList.Count()}, runningTaskInfoDic:{_runningTaskInfoDic.Count()}]");
                Thread.Sleep(1000);
                lock (_lockObj)
                {
                    //所有任务已完成
                    if (_taskInfoList.Count() <= 0)
                    {
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Check taskInfoList.Count() <= 100 \r\n");
                        _isFinished = true;
                        return;
                    }

                    //任务失败数超过任务要求的最大可失败数
                    LogInfoWriter.GetInstance().Info($"{debugStr} --Check hasCompleteTaskInfoList.Count(s => s.IsFailed):{_hasCompleteTaskInfoList.Count(s => s.IsFailed)}");
                    if (_hasCompleteTaskInfoList.Count(s => s.IsFailed) >= _taskFailedMaxCount)
                    {
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Check hasCompleteTaskInfoList.Count(s => s.IsFailed) >= {_taskFailedMaxCount} \r\n");
                        _isFinished = true;
                        return;
                    }

                    //正在进行的任务数 = 最大可并发任务 则跳过获取新任务  继续等待当前运行的任务完成
                    LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Count():{_runningTaskInfoDic.Count()}");
                    if (_runningTaskInfoDic.Count() == _concurrencyRunTaskMaxcount)
                    {
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Count() == {_concurrencyRunTaskMaxcount} \r\n");
                        continue;
                    }

                    //正在运行的任务中存在失败的任务且还有其他任务未结束 则不去获取新任务
                    LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Values.Any(s => s.IsFailed):{_runningTaskInfoDic.Values.Any(s => s.IsFailed)}");
                    LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Values.Any(s => !s.IsCompleted):{_runningTaskInfoDic.Values.Any(s => !s.IsCompleted)}");
                    if (_runningTaskInfoDic.Values.Any(s => s.IsFailed) &&
                        _runningTaskInfoDic.Values.Any(s => !s.IsCompleted))
                    {
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Check runningTaskInfoDic.Values.Any(s => s.IsFailed) == _runningTaskInfoDic.Values.Any(s => !s.IsCompleted) == true \r\n");
                        continue;
                    }
                    //如果当前正在运行任务小于最大 并发任务数 且当前线程没有正在运行的任务  则去拉取新任务执行
                    if (_runningTaskInfoDic.Count < _concurrencyRunTaskMaxcount &&
                        !_runningTaskInfoDic.ContainsKey(currentTaskId))
                    {
                        var t = _taskInfoList.First(s => !s.IsCompleted);
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Get GetTask Success :{t.TaskName}");
                        _runningTaskInfoDic.Add(currentTaskId, t);
                        _taskInfoList.Remove(t);
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Get taskInfoList.Count():{_taskInfoList.Count()}");
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Get runningTaskInfoDic.Count():{_runningTaskInfoDic.Count()}\r\n");
                    }
                }
                if (_runningTaskInfoDic.ContainsKey(currentTaskId))
                {
                    var task = _runningTaskInfoDic[currentTaskId];

                    try
                    {
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Execute Start");
                        OnBeforeStart?.Invoke(task);
                        task.TaskAction?.Invoke();
                        Interlocked.Increment(ref _successTaskCount);
                        OnAfterStart?.Invoke(task);
                    }
                    catch (Exception e)
                    {
                        task.ExecuteFailed(e);
                        Interlocked.Increment(ref _failedTaskCount);
                        OnExceptionOccurred?.Invoke(e);
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Execute Failed");
                    }
                    finally
                    {
                        task.HasCompleted();
                        Interlocked.Increment(ref _completedTaskCount);
                        OnCompleted?.Invoke(task);
                        LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute runningTaskInfoDic.Values.Any(s => s.IsFailed):{_runningTaskInfoDic.Values.Any(s => s.IsFailed)}!");
                        LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute hasCompleteTaskInfoList.Any(s => s.IsFailed)):{_hasCompleteTaskInfoList.Any(s => s.IsFailed)}!");
                        LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute runningTaskInfoDic.Values.Any(s=>!s.IsCompleted):{_runningTaskInfoDic.Values.Any(s => !s.IsCompleted)}!");

                        while ((_runningTaskInfoDic.Values.Any(s => s.IsFailed) || _hasCompleteTaskInfoList.Any(s => s.IsFailed)) && _runningTaskInfoDic.Values.Any(s => !s.IsCompleted))
                        {
                            LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Wait [taskInfoList:{_taskInfoList.Count()}, hasCompleteTaskInfoList:{_hasCompleteTaskInfoList.Count()}, runningTaskInfoDic:{_runningTaskInfoDic.Count()}]");
                            LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Wait there has some task failed ,so wait all current running task has finished!");
                            LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Wait [runningTaskInfoDic:{_runningTaskInfoDic.Values.Select(s=>s.TaskName + ":[" +s.IsFailed+","+s.IsCompleted+"]").Aggregate((s,r)=>s+" - "+r)}]");
                            Thread.Sleep(1000);
                        }
                        if (_runningTaskInfoDic.ContainsKey(currentTaskId))
                        {
                            LogInfoWriter.GetInstance().Info($"{debugStr} -- Execute Add [runningTaskInfoDic:{_runningTaskInfoDic.Values.Select(s => s.TaskName + ":[" + s.IsFailed + "," + s.IsCompleted + "]").Aggregate((s, r) => s + " - " + r)}]");
                            LogInfoWriter.GetInstance().Info($"{debugStr} --Execute Add {task.TaskName} HasComplete and Remove running");
                            _runningTaskInfoDic.Remove(currentTaskId);
                            _hasCompleteTaskInfoList.Add(task);
                        }
                        LogInfoWriter.GetInstance().Info($"{debugStr} --Execute End");
                    }
                }

                LogInfoWriter.GetInstance().Info($"{debugStr} --Check End [taskInfoList:{_taskInfoList.Count()}, hasCompleteTaskInfoList:{_hasCompleteTaskInfoList.Count()}, runningTaskInfoDic:{_runningTaskInfoDic.Count()}]");
            }
        }