Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PoolConfig"/> class.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="coinConfig"></param>
        public PoolConfig(dynamic config, ICoinConfig coinConfig)
        {
            try
            {
                Enabled = config.enabled ? config.enabled : false;

                if (Enabled == false) // if the configuration is not enabled
                {
                    return;           // just skip reading rest of the parameters.
                }
                Coin = coinConfig;    // assign the coin config.

                // load the sections.
                Daemon   = new DaemonConfig(config.daemon);
                Meta     = new MetaConfig(config.meta);
                Wallet   = new WalletConfig(config.wallet);
                Rewards  = new RewardsConfig(config.rewards);
                Payments = new PaymentConfig(config.payments);
                Miner    = new MinerConfig(config.miner);
                Job      = new JobConfig(config.job);
                Stratum  = new StratumServerConfig(config.stratum);
                Banning  = new BanConfig(config.banning);
                Storage  = new RedisConfig(config.storage.redis);
                Vanilla  = new VanillaServerConfig(config.vanilla);

                Valid = true;
            }
            catch (Exception e)
            {
                Valid = false;
                Log.Logger.ForContext <PoolConfig>().Error(e, "Error loading pool configuration");
            }
        }
        public void TestGetDefualtEndpoint()
        {
            DaemonConfig daemonConfig = DaemonConfig.GetEndPoint();

            Assert.IsTrue(daemonConfig.UDPEndpoint.Equals(DaemonConfig.DefaultEndpoint));
            Assert.IsTrue(daemonConfig.TCPEndpoint.Equals(DaemonConfig.DefaultEndpoint));
        }
        public override void OnAfterBootStrapComplete(IFoundation foundation)
        {
            base.OnAfterBootStrapComplete(foundation);

            // Replace Exception Handlers
            foundation.Container.RegisterType <IHandleException, HealthFriendlyExceptionHandler>(new ContainerControlledLifetimeManager());
            foundation.Container.RegisterType <IHandleExceptionProvider, HealthFriendlyExceptionHandlerProvider>(new ContainerControlledLifetimeManager());
            foundation.Container.RegisterInstance <HealthFriendlyExceptionHandlerProvider>(new HealthFriendlyExceptionHandlerProvider(foundation, foundation.GetLogger()), new ContainerControlledLifetimeManager());

            foundation.Container.RegisterType <IHandleException, HealthSwallowExceptionHandler>(Assumptions.SWALLOWED_EXCEPTION_HANDLER, new ContainerControlledLifetimeManager());
            foundation.Container.RegisterType <IHandleExceptionProvider, HealthSwallowExceptionHandlerProvider>(Assumptions.SWALLOWED_EXCEPTION_HANDLER, new ContainerControlledLifetimeManager());
            foundation.Container.RegisterInstance <HealthSwallowExceptionHandlerProvider>(Assumptions.SWALLOWED_EXCEPTION_HANDLER, new HealthSwallowExceptionHandlerProvider(foundation, foundation.GetLogger()), new ContainerControlledLifetimeManager());

            foundation.Container.RegisterInstance <ServerHealthExtractor>(new ServerHealthExtractor(foundation), new ContainerControlledLifetimeManager());

            DaemonConfig healthConfig = new DaemonConfig()
            {
                InstanceName           = HealthReportDaemon.DAEMON_NAME,
                ContinueOnError        = true,
                IntervalMilliSeconds   = 15 * 1000, // every 15 seconds
                StartDelayMilliSeconds = 60 * 1000,
                TaskConfiguration      = string.Empty
            };

            foundation.GetDaemonManager().RegisterDaemon(healthConfig, new HealthReportDaemon(foundation), true);
        }
Example #4
0
        /// <summary>
        /// Not Aspect Wrapped
        /// </summary>
        protected static TWorker EnsureWorker <TWorker>(IFoundation foundation, string workerName, int millisecondInterval = 5000)
            where TWorker : WorkerBase <TRequest>
        {
            IDaemonManager daemonManager = foundation.GetDaemonManager();
            IDaemonTask    daemonTask    = daemonManager.GetRegisteredDaemonTask(workerName);

            if (daemonTask == null)
            {
                lock (_RegistrationLock)
                {
                    daemonTask = daemonManager.GetRegisteredDaemonTask(workerName);
                    if (daemonTask == null)
                    {
                        if (millisecondInterval <= 1000)
                        {
                            millisecondInterval = 5000; // if you give bad data, we force to 5 seconds.
                        }
                        DaemonConfig config = new DaemonConfig()
                        {
                            InstanceName           = workerName,
                            ContinueOnError        = true,
                            IntervalMilliSeconds   = millisecondInterval,
                            StartDelayMilliSeconds = 0,
                            TaskConfiguration      = string.Empty
                        };
                        TWorker worker = (TWorker)foundation.Container.Resolve(typeof(TWorker), null);
                        daemonManager.RegisterDaemon(config, worker, true);
                    }
                }
                daemonTask = daemonManager.GetRegisteredDaemonTask(workerName);
            }
            return(daemonTask as TWorker);
        }
        public void RegisterCustomRouting(System.Web.Routing.RouteCollection routes)
        {
            base.ExecuteMethod("RegisterCustomRouting", delegate()
            {
                this.IFoundation.Container.RegisterInstance <INotifySynchronizer>(new ElasticSyncNotifySynchronizer(this.IFoundation));

                IDaemonManager daemonManager       = this.IFoundation.GetDaemonManager();
                ISettingsResolver settingsResolver = this.IFoundation.Resolve <ISettingsResolver>();
                bool isBackPane = settingsResolver.IsBackPane();
                bool isHydrate  = settingsResolver.IsHydrate();
                if (isBackPane)
                {
                    DaemonConfig config = new DaemonConfig()
                    {
                        InstanceName           = string.Format(ElasticSearchDaemon.DAEMON_NAME_FORMAT, Agents.AGENT_DEFAULT),
                        ContinueOnError        = true,
                        IntervalMilliSeconds   = (int)TimeSpan.FromSeconds(30).TotalMilliseconds,
                        StartDelayMilliSeconds = 15,
                        TaskConfiguration      = string.Empty
                    };
                    daemonManager.RegisterDaemon(config, new ElasticSearchDaemon(this.IFoundation, Agents.AGENT_DEFAULT), true);

                    config = new DaemonConfig()
                    {
                        InstanceName           = string.Format(ElasticSearchDaemon.DAEMON_NAME_FORMAT, Agents.AGENT_STATS),
                        ContinueOnError        = true,
                        IntervalMilliSeconds   = (int)TimeSpan.FromSeconds(30).TotalMilliseconds,
                        StartDelayMilliSeconds = 25,
                        TaskConfiguration      = string.Empty
                    };
                    daemonManager.RegisterDaemon(config, new ElasticSearchDaemon(this.IFoundation, Agents.AGENT_STATS), true);
                }
            });
        }
Example #6
0
        /// <nodoc/>
        public MaterializationDaemon(
            IParser parser,
            DaemonConfig daemonConfig,
            MaterializationDaemonConfig materializationConfig,
            IIpcProvider rpcProvider = null,
            Client bxlClient         = null)
            : base(parser,
                   daemonConfig,
                   !string.IsNullOrWhiteSpace(materializationConfig.LogDir) ? new FileLogger(materializationConfig.LogDir, LogFileName, daemonConfig.Moniker, logVerbose: true, MaterializationDaemonLogPrefix) : daemonConfig.Logger,
                   rpcProvider,
                   bxlClient)
        {
            m_config                = materializationConfig;
            m_actionQueue           = new ActionQueue(m_config.MaxDegreeOfParallelism);
            m_materializationStatus = new ConcurrentBigMap <string, bool>();
            m_counters              = new CounterCollection <MaterializationDaemonCounter>();

            m_macros = new Dictionary <string, string>
            {
                ["$(build.nttree)"] = Environment.GetEnvironmentVariable("_NTTREE")
            };

            m_logger.Info($"MaterializationDaemon config: {JsonConvert.SerializeObject(m_config)}");
            m_logger.Info($"Defined macros (count={m_macros.Count}):{Environment.NewLine}{string.Join(Environment.NewLine, m_macros.Select(kvp => $"{kvp.Key}={kvp.Value}"))}");
        }
        public void TestGetEndpointInvalidDoubleForm3()
        {
            string       daemonAddress = "lmn:127.0.0.2:2001 udp:127.0.0.1:2000";
            DaemonConfig daemonConfig  = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.IsTrue(DaemonConfig.DefaultEndpoint.Equals(daemonConfig.UDPEndpoint));
            Assert.IsTrue(DaemonConfig.DefaultEndpoint.Equals(daemonConfig.TCPEndpoint));
        }
Example #8
0
        private UdpSegmentEmitter(ISegmentMarshaller marshaller)
        {
            _marshaller = marshaller;
            _udpClient  = new UdpClient();
            DaemonConfig daemonEndPoint = DaemonConfig.GetEndPoint();

            EndPoint = daemonEndPoint.UDPEndpoint;
        }
        public void TestGetEndpointInvalidSingleForm()
        {
            string       daemonAddress = "1000001.1.1.1:9000"; // invalid ip address
            DaemonConfig daemonConfig  = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.IsTrue(DaemonConfig.DefaultEndpoint.Equals(daemonConfig.UDPEndpoint));
            Assert.IsTrue(DaemonConfig.DefaultEndpoint.Equals(daemonConfig.TCPEndpoint));
        }
        private AmazonXRayClient CreateXRayClient(DaemonConfig endpoint)
        {
            var config = new AmazonXRayConfig();

            config.ServiceURL = "http://" + endpoint.TCPEndpoint.Address.ToString() + ":" + endpoint.TCPEndpoint.Port;
            AWSCredentials credentials = new AnonymousAWSCredentials(); // sends unsigned requests to daemon endpoint

            return(new AmazonXRayClient(credentials, config));
        }
Example #11
0
        public void TestGetEndpointSingleFormWithHostname()
        {
            var daemonAddress       = "localhost:3001";
            var expectedTCPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3001);
            var expectedUDPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3001);
            var daemonConfig        = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.AreEqual(daemonConfig.UDPEndpoint, expectedUDPEndpoint);
            Assert.AreEqual(daemonConfig.TCPEndpoint, expectedTCPEndpoint);
        }
        public void TestGetEndpointInvalidDoubleForm2()
        {
            string       daemonAddress       = "udp:127.0.0.2:2001 udp:127.0.0.1:2000";
            IPEndPoint   expectedUDPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 2001);
            IPEndPoint   expectedTCPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);
            DaemonConfig daemonConfig        = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.IsTrue(DaemonConfig.DefaultEndpoint.Equals(daemonConfig.UDPEndpoint));
            Assert.IsTrue(DaemonConfig.DefaultEndpoint.Equals(daemonConfig.TCPEndpoint));
        }
Example #13
0
        private void WithSetup(IDropClient dropClient, Action <global::Tool.DropDaemon.DropDaemon, DropEtwListener> action, Client apiClient = null)
        {
            var    etwListener  = ConfigureEtwLogging();
            string moniker      = ServicePipDaemon.IpcProvider.RenderConnectionString(ServicePipDaemon.IpcProvider.CreateNewMoniker());
            var    daemonConfig = new DaemonConfig(VoidLogger.Instance, moniker: moniker, enableCloudBuildIntegration: false);
            var    dropConfig   = new DropConfig(string.Empty, null);
            var    daemon       = new global::Tool.DropDaemon.DropDaemon(UnixParser.Instance, daemonConfig, dropConfig, Task.FromResult(dropClient), client: apiClient);

            action(daemon, etwListener);
        }
Example #14
0
        static void Main(string[] args)
        {
            var initBarrier = new Barrier(2);
            var manager     = DaemonConfig.Default()
                              .RegisterServiceInstance(initBarrier)
                              .BuildManager();

            manager.SpawnWithReactor <ClientDaemonReactor>();
            manager.SpawnWithReactor <PrinterDaemonReactor>();
            Console.ReadLine();
        }
        public void TestGetEndpointSingleForm()
        {
            string       ip               = "1.0.0.1";
            int          port             = 1000;
            string       daemonAddress    = ip + ":" + port;
            IPEndPoint   expectedEndpoint = new IPEndPoint(IPAddress.Parse(ip), port);
            DaemonConfig daemonConfig     = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.IsTrue(expectedEndpoint.Equals(daemonConfig.UDPEndpoint));
            Assert.IsTrue(expectedEndpoint.Equals(daemonConfig.TCPEndpoint));
        }
Example #16
0
        public void TestGetEndpointDoubleFormWithHostnameIpMix2()
        {
            var tcpPort             = 3001;
            var udpPort             = 2001;
            var daemonAddress       = $"tcp:localhost:{tcpPort} udp:127.0.0.1:{udpPort}";
            var expectedTCPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), tcpPort);
            var expectedUDPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), udpPort);
            var daemonConfig        = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.AreEqual(daemonConfig.UDPEndpoint, expectedUDPEndpoint);
            Assert.AreEqual(daemonConfig.TCPEndpoint, expectedTCPEndpoint);
        }
Example #17
0
 /// <nodoc/>
 public MaterializationDaemon(
     IParser parser,
     DaemonConfig daemonConfig,
     MaterializationDaemonConfig materializationConfig,
     IIpcProvider rpcProvider = null,
     Client bxlClient         = null)
     : base(parser,
            daemonConfig,
            !string.IsNullOrWhiteSpace(materializationConfig.LogDir) ? new FileLogger(materializationConfig.LogDir, LogFileName, daemonConfig.Moniker, logVerbose: true, MaterializationDaemonLogPrefix) : daemonConfig.Logger,
            rpcProvider,
            bxlClient)
 {
 }
        public ServiceConnector(DaemonConfig daemonConfig, AmazonXRayClient xrayClient)
        {
            ClientID = ThreadSafeRandom.GenerateHexNumber(24);
            if (daemonConfig == null)
            {
                daemonConfig = DaemonConfig.GetEndPoint();
            }

            if (xrayClient == null)
            {
                xrayClient = CreateXRayClient(daemonConfig);
            }

            _xrayClient = xrayClient;
        }
        public ServiceConnector(DaemonConfig daemonConfig, XRayConfig xrayConfig)
        {
            ClientID = ThreadSafeRandom.GenerateHexNumber(24);
            if (daemonConfig == null)
            {
                daemonConfig = DaemonConfig.GetEndPoint();
            }
            _daemonConfig = daemonConfig;

            if (xrayConfig == null)
            {
                xrayConfig = CreateXRayConfig();
            }

            _xrayConfig = xrayConfig;
        }
Example #20
0
        /// <remarks>
        /// If an apiClient is not passed (i.e., null by default), we create a new Client that returns success for any bool command called.
        /// </remarks>
        private void WithSetup(IDropClient dropClient, Action <global::Tool.DropDaemon.DropDaemon, DropEtwListener, DropConfig> action, Client apiClient = null)
        {
            var    etwListener       = ConfigureEtwLogging();
            string moniker           = ServicePipDaemon.IpcProvider.RenderConnectionString(ServicePipDaemon.IpcProvider.CreateNewMoniker());
            var    daemonConfig      = new DaemonConfig(VoidLogger.Instance, moniker: moniker, enableCloudBuildIntegration: false);
            var    dropConfig        = new DropConfig("test", new Uri("file://xyz"));
            var    dropServiceConfig = new DropServiceConfig();

            if (apiClient == null)
            {
                apiClient = new Client(new MockClient(ipcOperation => IpcResult.Success("true")));
            }
            var daemon = new global::Tool.DropDaemon.DropDaemon(UnixParser.Instance, daemonConfig, dropServiceConfig, client: apiClient);

            daemon.RegisterDropClientForTesting(dropConfig, dropClient);
            action(daemon, etwListener, dropConfig);
        }
        public void TestGetEndpointEnvironemntVariableGettingPreference()
        {
            string daemonAddress = "1.0.0.2:2001";
            // Setting Enviornment variable
            string     envName             = DaemonConfig.EnvironmentVariableDaemonAddress;
            string     value               = "udp:127.0.0.2:2001 tcp:127.0.0.1:2000";
            IPEndPoint expectedUDPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.2"), 2001);
            IPEndPoint expectedTCPEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);

            Environment.SetEnvironmentVariable(envName, value);
            DaemonConfig daemonConfig = DaemonConfig.GetEndPoint(daemonAddress);

            Assert.IsTrue(expectedUDPEndpoint.Equals(daemonConfig.UDPEndpoint)); // Environment value used
            Assert.IsTrue(expectedTCPEndpoint.Equals(daemonConfig.TCPEndpoint));

            Environment.SetEnvironmentVariable(envName, null); // cleaning the environment variable
        }
Example #22
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PoolConfig"/> class.
        /// </summary>
        /// <param name="config">The configuration.</param>
        /// <param name="coinConfig"></param>
        public PoolConfig(dynamic config, ICoinConfig coinConfig)
        {
            try
            {
                _logger = Log.ForContext <PoolConfig>().ForContext("Component", coinConfig.Name);

                Enabled = config.enabled ? config.enabled : false;

                if (Enabled == false) // if the configuration is not enabled
                {
                    return;           // just skip reading rest of the parameters.
                }
                Coin = coinConfig;    // assign the coin config.

                // load the sections.
                Daemon   = new DaemonConfig(config.daemon);
                Meta     = new MetaConfig(config.meta);
                Wallet   = new WalletConfig(config.wallet);
                Rewards  = new RewardsConfig(config.rewards);
                Payments = new PaymentConfig(config.payments);
                Miner    = new MinerConfig(config.miner);
                Job      = new JobConfig(config.job);
                Stratum  = new StratumServerConfig(config.stratum);
                Banning  = new BanConfig(config.banning);
                Storage  = new StorageConfig(config.storage);
                Vanilla  = new VanillaServerConfig(config.vanilla);

                // process extra checks
                if (Storage.Layer is MposStorageLayerConfig)
                {
                    if (Payments.Enabled)
                    {
                        Payments.Disable();
                        _logger.Information("Disabled payments processor as it can not be enabled when MPOS mode is on");
                    }
                }

                Valid = true;
            }
            catch (Exception e)
            {
                Valid = false;
                _logger.Error(e, "Error loading pool configuration");
            }
        }
Example #23
0
        static void Main(string[] args)
        {
            var config = DaemonConfig
                         .Default()
                         .WithMq((conf => conf.WithUdpEmCaster(emcasterConfig =>
            {
                emcasterConfig.AddStressTestLayer(0.1);
                emcasterConfig.AddReliabilityLayer(TimeSpan.FromSeconds(1));
                emcasterConfig.AddRoute("chat");
                emcasterConfig.AddRoute(@"chat/[\w]*");
            })));
            var bus = config.BuildMessageBus();

            Console.Write("Enter user name : ");
            var userName = Console.ReadLine();

            Console.Write("Enter channel : #");
            var channel = Console.ReadLine();

            var daemon = new ThreadPoolDaemon();

            daemon.Schedule(
                () =>
            {
                bus.Subscribe <UserJoined>("chat",
                                           message => Console.WriteLine("# " + message.UserName + " joined #" + message.Channel));
                bus.Subscribe <UserMessage>(string.Format(@"chat\{0}", channel),
                                            message => Console.WriteLine("{0}>{1}", message.UserName, message.Message));
                bus.Publish("chat", new UserJoined {
                    Channel = channel, UserName = userName
                });
            });
            while (true)
            {
                var line = Console.ReadLine();
                bus.Publish(string.Format(@"chat\{0}", channel), new UserMessage()
                {
                    Message = line, UserName = userName
                });
            }
        }
Example #24
0
        public void RegisterCustomRouting(System.Web.Routing.RouteCollection routes)
        {
            base.ExecuteMethod("RegisterCustomRouting", delegate()
            {
                AmazonUploadFile uploadFile = new AmazonUploadFile(this.IFoundation);
                this.IFoundation.Container.RegisterInstance <IUploadFiles>(uploadFile);
                this.IFoundation.Container.RegisterInstance <INotifyEncoder>(uploadFile);

                this.IFoundation.Container.RegisterInstance <IProcessImage>(new AmazonImageResizeDaemon(this.IFoundation)); // isolated instance, isnt actually ran as a daemon

                IDaemonManager daemonManager       = this.IFoundation.GetDaemonManager();
                ISettingsResolver settingsResolver = this.IFoundation.Resolve <ISettingsResolver>();
                bool isBackPane  = settingsResolver.IsBackPane();
                bool isLocalHost = settingsResolver.IsLocalHost();
                bool isHydrate   = settingsResolver.IsHydrate();
                if (isBackPane && !isLocalHost && !isHydrate)
                {
                    DaemonConfig config = new DaemonConfig()
                    {
                        InstanceName           = AmazonEncodingDaemon.DAEMON_NAME,
                        ContinueOnError        = true,
                        IntervalMilliSeconds   = (int)TimeSpan.FromMinutes(1).TotalMilliseconds,
                        StartDelayMilliSeconds = 15,
                        TaskConfiguration      = string.Empty
                    };
                    daemonManager.RegisterDaemon(config, new AmazonEncodingDaemon(this.IFoundation), true);

                    config = new DaemonConfig()
                    {
                        InstanceName           = AmazonImageResizeDaemon.DAEMON_NAME,
                        ContinueOnError        = true,
                        IntervalMilliSeconds   = (int)TimeSpan.FromMinutes(1).TotalMilliseconds,
                        StartDelayMilliSeconds = 15,
                        TaskConfiguration      = string.Empty
                    };
                    daemonManager.RegisterDaemon(config, new AmazonImageResizeDaemon(this.IFoundation), true);
                }
            });
        }
Example #25
0
        public bool Run()
        {
            try
            {
                // スリープを禁止する。
                NativeMethods.SetThreadExecutionState(NativeMethods.ExecutionState.Continuous);

                // カレントディレクトリをexeと同じディレクトリパスへ変更。
                Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

                // ハンドルしていない例外をログ出力させる。
                Thread.GetDomain().UnhandledException += this.Program_UnhandledException;

                string sessionId = NetworkConverter.ToHexString(Sha256.Compute(Path.GetFullPath(Assembly.GetEntryAssembly().Location)));

                // 多重起動防止
                {
                    _mutex = new Mutex(false, sessionId);

                    if (!_mutex.WaitOne(0))
                    {
                        return(false);
                    }
                }

                // 既定のフォルダを作成する。
                {
                    foreach (var propertyInfo in typeof(AmoebaEnvironment.PathsEnvironment).GetProperties())
                    {
                        string path = propertyInfo.GetValue(AmoebaEnvironment.Paths) as string;
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                    }
                }

                // Tempフォルダを環境変数に登録。
                {
                    // Tempフォルダ内を掃除。
                    try
                    {
                        foreach (string path in Directory.GetFiles(AmoebaEnvironment.Paths.TempDirectoryPath, "*", SearchOption.AllDirectories))
                        {
                            File.Delete(path);
                        }

                        foreach (string path in Directory.GetDirectories(AmoebaEnvironment.Paths.TempDirectoryPath, "*", SearchOption.AllDirectories))
                        {
                            Directory.Delete(path, true);
                        }
                    }
                    catch (Exception)
                    {
                    }

                    Environment.SetEnvironmentVariable("TMP", Path.GetFullPath(AmoebaEnvironment.Paths.TempDirectoryPath), EnvironmentVariableTarget.Process);
                    Environment.SetEnvironmentVariable("TEMP", Path.GetFullPath(AmoebaEnvironment.Paths.TempDirectoryPath), EnvironmentVariableTarget.Process);
                }

                // ログファイルを設定する。
                this.Setting_Log();

                // アップデート
                {
                    // 一時的に作成された"Amoeba.Update.exe"を削除する。
                    {
                        string tempUpdateExeFilePath = Path.Combine(AmoebaEnvironment.Paths.WorkDirectoryPath, "Amoeba.Update.exe");

                        if (File.Exists(tempUpdateExeFilePath))
                        {
                            File.Delete(tempUpdateExeFilePath);
                        }
                    }

                    if (Directory.Exists(AmoebaEnvironment.Paths.UpdateDirectoryPath))
                    {
                        string zipFilePath = null;

                        // 最新のバージョンのzipを検索。
                        {
                            var map   = new Dictionary <string, Version>();
                            var regex = new Regex(@"Amoeba.+?((\d*)\.(\d*)\.(\d*)).*?\.zip", RegexOptions.Compiled);

                            foreach (string path in Directory.GetFiles(AmoebaEnvironment.Paths.UpdateDirectoryPath))
                            {
                                var match = regex.Match(Path.GetFileName(path));
                                if (!match.Success)
                                {
                                    continue;
                                }

                                var version = new Version(match.Groups[1].Value);
                                if (version < AmoebaEnvironment.Version)
                                {
                                    continue;
                                }

                                map.Add(path, version);
                            }

                            if (map.Count > 0)
                            {
                                var sortedList = map.ToList();
                                sortedList.Sort((x, y) => y.Value.CompareTo(x.Value));

                                zipFilePath = sortedList.First().Key;
                            }
                        }

                        if (zipFilePath != null)
                        {
                            string tempUpdateDirectoryPath = Path.Combine(AmoebaEnvironment.Paths.WorkDirectoryPath, "Update");

                            if (Directory.Exists(tempUpdateDirectoryPath))
                            {
                                Directory.Delete(tempUpdateDirectoryPath, true);
                            }

                            using (var zipfile = new ZipFile(zipFilePath))
                            {
                                zipfile.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
                                zipfile.ExtractAll(tempUpdateDirectoryPath);
                            }

                            if (File.Exists(zipFilePath))
                            {
                                File.Delete(zipFilePath);
                            }

                            string tempUpdateExeFilePath = Path.Combine(AmoebaEnvironment.Paths.WorkDirectoryPath, "Amoeba.Update.exe");

                            File.Copy("Amoeba.Update.exe", tempUpdateExeFilePath);

                            var startInfo = new ProcessStartInfo();
                            startInfo.FileName  = Path.GetFullPath(tempUpdateExeFilePath);
                            startInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"",
                                                                sessionId,
                                                                Path.GetFullPath(Path.Combine(tempUpdateDirectoryPath, "Core")),
                                                                Path.GetFullPath(AmoebaEnvironment.Paths.CoreDirectoryPath),
                                                                Path.GetFullPath(Assembly.GetEntryAssembly().Location));
                            startInfo.WorkingDirectory = Path.GetFullPath(Path.GetDirectoryName(tempUpdateExeFilePath));

                            Process.Start(startInfo);

                            return(false);
                        }
                    }
                }

                // マイグレーション
                {
                    if (AmoebaEnvironment.Config.Version <= new Version(5, 0, 60))
                    {
                        try
                        {
                            var basePath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, @"Service\Core\Cache");

                            if (!Directory.Exists(Path.Combine(basePath, "Blocks")))
                            {
                                Directory.CreateDirectory(Path.Combine(basePath, "Blocks"));
                            }

                            var renameList = new List <(string oldPath, string newPath)>();
                            renameList.Add((@"CacheInfos.json.gz", @"ContentInfos.json.gz"));
                            renameList.Add((@"Size.json.gz", @"Blocks\Size.json.gz"));
                            renameList.Add((@"ClusterIndex.json.gz", @"Blocks\ClusterIndex.json.gz"));

                            foreach (var(oldPath, newPath) in renameList)
                            {
                                if (File.Exists(Path.Combine(basePath, newPath)) ||
                                    !File.Exists(Path.Combine(basePath, oldPath)))
                                {
                                    continue;
                                }

                                File.Copy(Path.Combine(basePath, oldPath), Path.Combine(basePath, newPath));
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                        }
                    }

                    if (AmoebaEnvironment.Config.Version <= new Version(5, 1, 1))
                    {
                        try
                        {
                            var sourcePath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, @"View\Settings");
                            var destPath   = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, @"Control\Settings");

                            if (Directory.Exists(sourcePath))
                            {
                                if (!Directory.Exists(destPath))
                                {
                                    Directory.CreateDirectory(destPath);
                                }

                                foreach (var oldPath in Directory.GetFiles(sourcePath))
                                {
                                    var newPath = Path.Combine(destPath, Path.GetFileName(oldPath));
                                    if (File.Exists(newPath) || !File.Exists(oldPath))
                                    {
                                        continue;
                                    }

                                    File.Copy(oldPath, newPath);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                        }

                        try
                        {
                            var basePath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, @"Control\Settings");

                            var renameList = new List <(string oldPath, string newPath)>();
                            renameList.Add((@"AccountInfo.json.gz", @"AccountSetting.json.gz"));
                            renameList.Add((@"UpdateInfo.json.gz", @"UpdateSetting.json.gz"));

                            foreach (var(oldPath, newPath) in renameList
                                     .Select(tuple => (Path.Combine(basePath, tuple.oldPath), Path.Combine(basePath, tuple.newPath))))
                            {
                                if (File.Exists(newPath) || !File.Exists(oldPath))
                                {
                                    continue;
                                }

                                File.Copy(oldPath, newPath);
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Error(e);
                        }
                    }
                }

#if !DEBUG
                // デーモンプロセス起動。
                {
                    var daemonExeFilePath    = Path.Combine(AmoebaEnvironment.Paths.DaemonDirectoryPath, "Amoeba.Daemon.exe");
                    var daemonConfigFilePath = Path.Combine(AmoebaEnvironment.Paths.ConfigDirectoryPath, "Daemon.toml");

                    if (!File.Exists(daemonConfigFilePath))
                    {
                        // 「Amoeba/Core/Daemon」のような階層を想定。
                        var basePath = "../../";

                        var config = new DaemonConfig(
                            new Version(0, 0, 0),
                            new DaemonConfig.CommunicationConfig("tcp:127.0.0.1:4040"),
                            new DaemonConfig.CacheConfig(Path.Combine(basePath, "Config", "Cache.blocks")),
                            new DaemonConfig.PathsConfig(
                                Path.Combine(basePath, "Temp"),
                                Path.Combine(basePath, "Config", "Service"),
                                Path.Combine(basePath, "Log")));

                        var tomlSettings = TomlSettings.Create(builder => builder
                                                               .ConfigureType <Version>(type => type
                                                                                        .WithConversionFor <TomlString>(convert => convert
                                                                                                                        .ToToml(tt => tt.ToString())
                                                                                                                        .FromToml(ft => Version.Parse(ft.Value)))));

                        Toml.WriteFile(config, daemonConfigFilePath, tomlSettings);
                    }

                    var startInfo = new ProcessStartInfo();
                    startInfo.FileName        = daemonExeFilePath;
                    startInfo.Arguments       = string.Format("-c \"{0}\"", daemonConfigFilePath);
                    startInfo.CreateNoWindow  = true;
                    startInfo.UseShellExecute = false;

                    try
                    {
                        _process = Process.Start(startInfo);
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                }
#endif

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex);

                return(false);
            }
        }
Example #26
0
 public EnvironmentConfig(Version version, DaemonConfig daemon, CacheConfig cache)
 {
     this.Version = version;
     this.Cache   = cache;
     this.Daemon  = daemon;
 }
Example #27
0
 public TcpServer()
     : this(DaemonConfig.Default().BuildManager())
 {
 }
 /// <summary>
 /// Configures X-Ray client with given <see cref="DaemonConfig"/> instance.
 /// </summary>
 /// <param name="daemonConfig">An instance of <see cref="DaemonConfig"/>.</param>
 public void LoadDaemonConfig(DaemonConfig daemonConfig)
 {
     DaemonCfg = daemonConfig;
 }
Example #29
0
        private void Setting_Log(DaemonConfig config)
        {
            var    now            = DateTime.Now;
            string logFilePath    = null;
            bool   isHeaderWriten = false;

            for (int i = 0; i < 1024; i++)
            {
                if (i == 0)
                {
                    logFilePath = Path.Combine(config.Paths.LogDirectoryPath,
                                               string.Format("Daemon_{0}.txt", now.ToString("yyyy-MM-dd_HH-mm-ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)));
                }
                else
                {
                    logFilePath = Path.Combine(config.Paths.LogDirectoryPath,
                                               string.Format("Daemon_{0}.({1}).txt", now.ToString("yyyy-MM-dd_HH-mm-ss", System.Globalization.DateTimeFormatInfo.InvariantInfo), i));
                }

                if (!File.Exists(logFilePath))
                {
                    break;
                }
            }

            if (logFilePath == null)
            {
                return;
            }

            Log.MessageEvent += (sender, e) =>
            {
                if (e.Level == LogMessageLevel.Information)
                {
                    return;
                }
#if !DEBUG
                if (e.Level == LogMessageLevel.Debug)
                {
                    return;
                }
#endif

                lock (_lockObject)
                {
                    try
                    {
                        using (var writer = new StreamWriter(logFilePath, true, new UTF8Encoding(false)))
                        {
                            if (!isHeaderWriten)
                            {
                                writer.WriteLine(GetMachineInfomation());
                                isHeaderWriten = true;
                            }

                            writer.WriteLine(MessageToString(e));
                            writer.Flush();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            };

            Log.ExceptionEvent += (sender, e) =>
            {
                if (e.Level == LogMessageLevel.Information)
                {
                    return;
                }
#if !DEBUG
                if (e.Level == LogMessageLevel.Debug)
                {
                    return;
                }
#endif

                lock (_lockObject)
                {
                    try
                    {
                        using (var writer = new StreamWriter(logFilePath, true, new UTF8Encoding(false)))
                        {
                            if (!isHeaderWriten)
                            {
                                writer.WriteLine(GetMachineInfomation());
                                isHeaderWriten = true;
                            }

                            writer.WriteLine(ExceptionToString(e));
                            writer.Flush();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            };

            string MessageToString(LogMessageEventArgs e)
            {
                var sb = new StringBuilder();

                sb.AppendLine("--------------------------------------------------------------------------------");
                sb.AppendLine();
                sb.AppendLine($"Time: {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}");
                sb.AppendLine($"Level: {e.Level}");
                sb.AppendLine($"Message: {e.Message}");

                sb.AppendLine();

                return(sb.ToString());
            }

            string ExceptionToString(LogExceptionEventArgs e)
            {
                var sb = new StringBuilder();

                sb.AppendLine("--------------------------------------------------------------------------------");
                sb.AppendLine();
                sb.AppendLine($"Time: {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}");
                sb.AppendLine($"Level: {e.Level}");

                var list = new List <Exception>();

                if (e.Exception is AggregateException aggregateException)
                {
                    list.AddRange(aggregateException.Flatten().InnerExceptions);
                }
                else
                {
                    var exception = e.Exception;

                    while (exception != null)
                    {
                        list.Add(exception);

                        try
                        {
                            exception = exception.InnerException;
                        }
                        catch (Exception)
                        {
                            break;
                        }
                    }
                }

                foreach (var exception in list)
                {
                    try
                    {
                        sb.AppendLine();
                        sb.AppendLine($"Exception: {exception.GetType().ToString()}");
                        if (!string.IsNullOrWhiteSpace(exception.Message))
                        {
                            sb.AppendLine($"Message: {exception.Message}");
                        }
                        if (!string.IsNullOrWhiteSpace(exception.StackTrace))
                        {
                            sb.AppendLine($"StackTrace: {exception.StackTrace}");
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                sb.AppendLine();

                return(sb.ToString());
            }
        }
Example #30
0
        public void Run()
        {
            // カレントディレクトリをexeと同じディレクトリパスへ変更。
            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

            // ハンドルしていない例外をログ出力させる。
            AppDomain.CurrentDomain.UnhandledException += this.Program_UnhandledException;
            Thread.GetDomain().UnhandledException += this.Program_UnhandledException;

#if !DEBUG
            // Tomlファイルを読み込み。
            DaemonConfig config = null;
            {
                // コマンドライン引数を解析。
                var options = CommandLine.Parser.Default.ParseArguments <AmoebaDaemonOptions>(Environment.GetCommandLineArgs())
                              .MapResult(
                    (AmoebaDaemonOptions x) => x,
                    errs => null);
                if (options == null)
                {
                    return;
                }

                if (File.Exists(options.ConfigFilePath))
                {
                    var tomlSettings = TomlSettings.Create(builder => builder
                                                           .ConfigureType <Version>(type => type
                                                                                    .WithConversionFor <TomlString>(convert => convert
                                                                                                                    .ToToml(tt => tt.ToString())
                                                                                                                    .FromToml(ft => Version.Parse(ft.Value)))));

                    config = Toml.ReadFile <DaemonConfig>(options.ConfigFilePath, tomlSettings);
                }

                if (config == null)
                {
                    return;
                }
            }
#else
            DaemonConfig config = null;
            {
                var basePath = Environment.GetCommandLineArgs()[0];
                if (!Directory.Exists(basePath))
                {
                    Directory.CreateDirectory(basePath);
                }

                config = new DaemonConfig(
                    new Version(0, 0, 0),
                    new DaemonConfig.CacheConfig(Path.Combine(basePath, "Cache.blocks")),
                    new DaemonConfig.PathsConfig(
                        Path.Combine(basePath, "Temp"),
                        Path.Combine(basePath, "Config/Service"),
                        Path.Combine(basePath, "Log")));
            }
#endif

            // 既定のフォルダを作成する。
            {
                foreach (var propertyInfo in typeof(DaemonConfig.PathsConfig).GetProperties())
                {
                    string path = propertyInfo.GetValue(config.Paths) as string;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                }
            }

            // Tempフォルダを環境変数に登録。
            {
                // Tempフォルダ内を掃除。
                try
                {
                    foreach (string path in Directory.GetFiles(config.Paths.TempDirectoryPath, "*", SearchOption.AllDirectories))
                    {
                        File.Delete(path);
                    }

                    foreach (string path in Directory.GetDirectories(config.Paths.TempDirectoryPath, "*", SearchOption.AllDirectories))
                    {
                        Directory.Delete(path, true);
                    }
                }
                catch (Exception)
                {
                }

                Environment.SetEnvironmentVariable("TMP", Path.GetFullPath(config.Paths.TempDirectoryPath), EnvironmentVariableTarget.Process);
                Environment.SetEnvironmentVariable("TEMP", Path.GetFullPath(config.Paths.TempDirectoryPath), EnvironmentVariableTarget.Process);
            }

            // ログファイルを設定する。
            this.Setting_Log(config);

            // 30分置きにLOHの圧縮を実行する。
            _timer = new WatchTimer(() =>
            {
                GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                GC.Collect();
            });
            _timer.Start(new TimeSpan(0, 30, 0));

            Socket targetSocket = null;

            // サービス開始。
            try
            {
#if !DEBUG
                for (int i = 50000; i < 60000; i++)
                {
                    try
                    {
                        using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                        {
                            var endpoint = new IPEndPoint(IPAddress.Loopback, i);
                            socket.Bind(endpoint);
                            socket.Listen(1);

                            Console.Out.WriteLine(endpoint.ToString());

                            var sw = Stopwatch.StartNew();

                            for (; ;)
                            {
                                if (socket.Poll(0, SelectMode.SelectRead))
                                {
                                    break;
                                }
                                if (sw.Elapsed.TotalSeconds > 30)
                                {
                                    return;
                                }

                                Thread.Sleep(1000);
                            }

                            targetSocket = socket.Accept();

                            break;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
#else
                try
                {
                    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                    {
                        var endpoint = new IPEndPoint(IPAddress.Loopback, 60000);
                        socket.Bind(endpoint);
                        socket.Listen(1);

                        Console.Out.WriteLine(endpoint.ToString());

                        var sw = Stopwatch.StartNew();

                        for (; ;)
                        {
                            if (socket.Poll(0, SelectMode.SelectRead))
                            {
                                break;
                            }
                            if (sw.Elapsed.TotalSeconds > 30)
                            {
                                return;
                            }

                            Thread.Sleep(1000);
                        }

                        targetSocket = socket.Accept();
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
#endif

                using (var bufferManager = new BufferManager(1024 * 1024 * 1024))
                    using (var serviceManager = new ServiceManager(config.Paths.ConfigDirectoryPath, config.Cache.BlocksFilePath, bufferManager))
                        using (var daemonManager = new AmoebaDaemonManager <ServiceManager>(targetSocket, serviceManager, bufferManager))
                        {
                            try
                            {
                                daemonManager.Watch();
                            }
                            catch (Exception e)
                            {
                                Log.Error(e);

                                Console.WriteLine(e.Message);
                            }
                        }
            }
            catch (Exception e)
            {
                Log.Error(e);

                Console.WriteLine(e.Message);
            }
            finally
            {
                if (targetSocket != null)
                {
                    targetSocket.Dispose();
                }
            }
        }