Exemple #1
0
        public void Enque_files_for_pending_download()
        {
            var queue = new PersistentQueue(Path.Combine(_fileStorePath, "_FileQueueTest"));

            using (var session = queue.OpenSession())
            {
                while (session.Dequeue() != null)
                {
                    ;
                }
                session.Flush();
            }

            //start file server
            IWorker fileServer = new FileServer(_fileServer_Port, _fileStorePath, queue);

            fileServer.Start();

            //clean queue

            //start worker
            IWorker fileEnqueuer = new FileEnqueuer(_fileStorePath,
                                                    _fileEnqueuer_SyncApi, _fileEnqueuer_Interval, queue);

            fileEnqueuer.Start();
            //wait work complete
            Thread.Sleep(3000);
            fileEnqueuer.Stop();

            //check if files in queue
            HttpClient client = new HttpClient();
            var        get    = client.GetAsync(_fileEnqueuer_SyncApi).Result;

            get.EnsureSuccessStatusCode();
            var result = get.Content.ReadAsStringAsync().Result.ToDynamicObject();

            if (result.res_code == 0)
            {
                using (var session = queue.OpenSession())
                    Assert.AreEqual((result.Result as IList <dynamic>).Count, queue.EstimatedCountOfItemsInQueue);
            }
        }
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();
                }
            }
        }