Exemple #1
0
        /// <summary>
        /// 适用于控制台的后台服务框架
        /// </summary>
        /// <param name="configKeyAndFieldNames">需要初始化的配置项字段 key:配置项的key,例:"KeyOfUser",value:配置项的名称,例:nameof(_keyOfUser)</param>
        /// <param name="tryBlock">核心代码</param>
        /// <param name="finallyBlock">清理代码</param>
        /// <param name="userRights">需要的权限</param>
        /// <param name="updateInterval">升级检查时间,默认1小时</param>
        public static void RunAsBackgroundService(Dictionary <string, string> configKeyAndFieldNames, Action tryBlock, Action finallyBlock = null, UserRights userRights = UserRights.NORMAL_USER, int updateInterval = 3600000)
        {
            var mutex           = new Mutex(true, ProductDescription.AppName);
            var exitForUpdating = false;

            try
            {
                EnsureSingleRunning(mutex);

                InitDeployQueryString();

                EnsureUserRights(userRights);

                Info(ApplicationDeployment.IsNetworkDeployed ? $"{ProductDescription.Product} {ProductDescription.Version}" : ProductDescription.AppName);

                InitConfigurations(configKeyAndFieldNames);

                tryBlock?.Invoke();

                var updateWorker = new UpdateCheckingWorker(updateInterval);
                updateWorker.Start();
                updateWorker.WaitForExit();
                exitForUpdating = true;
                Info("找到更新,准备重启...\n");
            }
            catch (Exception ex)
            {
                Error(ex);
                Thread.Sleep(5000);
            }
            finally
            {
                finallyBlock?.Invoke();

                WorkerFactory.StopAll();

                mutex.Close();

                if (exitForUpdating)
                {
                    Application.Restart();
                }
            }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            List <IWorker>  wokers          = new List <IWorker>();
            PersistentQueue queue           = null;
            var             exitForUpdating = false;

            try
            {
                Info($"CDN Starting... Version : {ApplicationHelper.Version}");
                ApplicationHelper.CheckSingleRunning(mutex);
                ApplicationHelper.InitDeployQueryString("SyncApiParam");

                #region Get Configs From url or app.config

                string _fileStorePath = GetConfigFromDeployThenAppConfig <string>("FileStorePath");

                Boolean _fileServer_Enabled = GetConfigFromDeployThenAppConfig <Boolean>("FileServer_Enabled");

                int _fileServer_Port = GetConfigFromDeployThenAppConfig <int>("FileServer_Port");

                Boolean _fileEnqueuer_Enabled = GetConfigFromDeployThenAppConfig <Boolean>("FileEnqueuer_Enabled");

                Int32 _fileEnqueuer_Interval = GetConfigFromDeployThenAppConfig <Int32>("FileEnqueuer_Interval");

                string _fileEnqueuer_SyncApi = GetConfigFromDeployThenAppConfig <string>("FileEnqueuer_SyncApi");

                Boolean _filePuller_Enabled = GetConfigFromDeployThenAppConfig <Boolean>("FilePuller_Enabled");

                Int32 _filePuller_DownloadTimeout = GetConfigFromDeployThenAppConfig <Int32>("FilePuller_DownloadTimeout");

                Int32 _filePuller_Interval = GetConfigFromDeployThenAppConfig <Int32>("FilePuller_Interval");

                Int32 _filePuller_RetryTimes = GetConfigFromDeployThenAppConfig <Int32>("FilePuller_RetryTimes");

                Int32 _filePuller_DownloadThreadCount =
                    GetConfigFromDeployThenAppConfig <Int32>("FilePuller_DownloadThreadCount");

                Int32 _updateInterval = GetConfigFromDeployThenAppConfig <Int32>("UpdateInterval");

                #endregion Get Configs From url or app.config

                queue = new PersistentQueue(Path.Combine(_fileStorePath, "_FileQueue"));

                if (_fileEnqueuer_Enabled)
                {
                    IWorker fileEnqueuer = new FileEnqueuer(_fileStorePath,
                                                            _fileEnqueuer_SyncApi, _fileEnqueuer_Interval, queue);
                    fileEnqueuer.Start();
                    wokers.Add(fileEnqueuer);
                    Info("FileEnqueuer Started");
                }

                if (_filePuller_Enabled)
                {
                    for (int i = 0; i < _filePuller_DownloadThreadCount; i++)
                    {
                        IWorker filePuller = new FilePuller(_fileStorePath,
                                                            _filePuller_Interval, _filePuller_DownloadTimeout,
                                                            _filePuller_RetryTimes, queue);
                        filePuller.Start();
                        wokers.Add(filePuller);
                        Info($"FilePuller {i} Started");
                    }
                }

                if (_fileServer_Enabled)
                {
                    IWorker fileServer = new FileServer(_fileServer_Port, _fileStorePath, queue);
                    fileServer.Start();
                    wokers.Add(fileServer);

                    Info($"FileServer Started at port {_fileServer_Port}");
                }

                IWorker updateWorker = new UpdateCheckingWorker(_updateInterval);
                updateWorker.Start();
                updateWorker.Wait();
                Info("Restart for updating...");
                exitForUpdating = true;
            }
            catch (Exception ex)
            {
                Info(ex.Message);
                Console.ReadLine();
            }
            finally
            {
                //Clean
                Parallel.ForEach(wokers, w => w.Stop());
                queue?.Dispose();
                mutex.Close();

                if (exitForUpdating)
                {
                    Application.Restart();
                }
            }
        }