メインコンテンツへスキップ

動画の分散エンコードクラスタを自作・運用している話・79

    目次

    ノードの設定も外部化したい 

    マネージャを変えたら当然ノードも変更したくなりますよね。

    今回はこちらを改良してみましょう。

    public partial class APPLICATION
    {
        // 外部ファイルの設定
        public string FFmpegPath;
    
        // リモートの待ち受けポート
        public int Port;
    
        public ISERVER_STATE State;
        public Task<string> Task;
        public Dictionary<string, string> RecieveCommand;
    
        public int OperationSize;
        public CancellationTokenSource CTS;
    
        public static string RecieveData;
    
        public APPLICATION()
        {
            // タスクを保存する変数
            Task = null;
    
            // ノードの状態を初期化
            State = new SERVER_STATE_NULL();
        }
    }
    
    public partial class APPLICATION
    {
        public APPLICATION Initialize()
        {
            APPLICATION app_config = new APPLICATION();
    
            // 設定ファイルの読み込み
            var cfg_path = $@"{Directory.GetCurrentDirectory()}\config.cfg";
            var cfg_string = ReadConfigFile(cfg_path);
    
            var cfg_list = GetConfig(cfg_string);
    
            app_config = SetConfig(app_config, cfg_list);
    
            return app_config;
        }
    
        APPLICATION SetConfig(APPLICATION app_config, Dictionary<string, string> cfg_list)
        {
            {
                var value = IncludeKey(cfg_list, CONFIG.KEY.FFMPEG_PATH);
                if (value != "" && File.Exists(value) == true)
                    app_config.FFmpegPath = value;
            }
    
            {
                var value = IncludeKey(cfg_list, CONFIG.KEY.SERVER_PORT);
                if (value != "")
                    app_config.Port = value.ToInt(10000);
            }
    
            return app_config;
        }
    
        string IncludeKey(Dictionary<string, string> list, string key)
        {
            if (list.Keys.ToList().IndexOf(key) > -1)
                return list[key];
    
            return "";
        }
    
        static Dictionary<string, string> GetConfig(string cfg_string)
        {
            var cfg_list = JsonSerializer.Deserialize<Dictionary<string, string>>(cfg_string);
    
            if (cfg_list.Keys.ToList().IndexOf(CONFIG.KEY.FFMPEG_PATH) < 0)
                cfg_list[CONFIG.KEY.FFMPEG_PATH] = @"C:\Program Files\ffmpeg\bin\ffmpeg.exe";
    
            if (cfg_list.Keys.ToList().IndexOf(CONFIG.KEY.SERVER_PORT) < 0)
                cfg_list[CONFIG.KEY.SERVER_PORT] = "10000";
    
            return cfg_list;
        }
    
        static string ReadConfigFile(string cfg_path)
        {
            var cfg_string = "";
            using (var s = new StreamReader(cfg_path))
            {
                cfg_string = s.ReadToEnd();
            }
            return cfg_string;
        }
    }
    public struct CONFIG
    {
        public struct KEY
        {
            public const string FFMPEG_PATH = "FFMPEG_PATH";
            public const string SERVER_PORT = "SERVER_PORT";
        }
    }

    マネージャ同様、APPLICATIONクラスに全部まとめました。

    今のところ変更したい設定は「ffmpegのパス」と「待ち受けポート」くらいなので、それだけの設定です。

    これで、いよいよどんな環境にも持ち運べるようになりますね。

     
     
    私は自宅で動画エンコードクラスタを運用している日曜プログラマです。13年以上にわたる運用での気付きや自作クラスタへのフィードバックをまとめていきたいと思います。

    あなたへのおすすめ