Example #1
0
        public KStart(string Namespace, string LockName, string Id, double LeaseDuration, double RetryPeriod, double RenewDeadline)
        {
            Logger = KLog.GetLogger <KStart>();
            Kube   = new(KubernetesClientConfiguration.BuildDefaultConfig());

            this.Namespace = Namespace;
            this.Id        = Id;
            this.LockName  = LockName;

            EndpointsLock EndpointLock = new(Kube, Namespace, LockName, Id);

            Elector = new(new LeaderElectionConfig(EndpointLock)
            {
                LeaseDuration = TimeSpan.FromSeconds(LeaseDuration),
                RetryPeriod = TimeSpan.FromSeconds(RetryPeriod),
                RenewDeadline = TimeSpan.FromSeconds(RenewDeadline),
            });

            Elector.OnStartedLeading += Elector_OnStartedLeading;
            Elector.OnStoppedLeading += Elector_OnStoppedLeading;
            Elector.OnNewLeader      += Elector_OnNewLeader;
        }
Example #2
0
 public KElector(LeaderElectionConfig config)
 {
     Logger      = KLog.GetLogger <KElector>();
     this.config = config;
 }
Example #3
0
        private Process Exec(string ExecPath, ExecEventArgs e)
        {
            if (string.IsNullOrEmpty(ExecPath))
            {
                return(null);
            }

            try
            {
                ExecPath = ExecPath.FormatWith(e);
            }
            catch (Exception Ex)
            {
                Logger.LogError(Ex, $"Failed to format command: {ExecPath}");
                return(null);
            }

            try
            {
                Logger.LogInformation($"Exec: {ExecPath}");
                IEnumerator <string> Args   = ExecPath.SplitArgs().GetEnumerator();
                ProcessStartInfo     PStart = new(Args.First());
                foreach (string s in Args.Rests())
                {
                    PStart.ArgumentList.Add(s);
                }

                PStart.CreateNoWindow         = true;
                PStart.UseShellExecute        = false;
                PStart.RedirectStandardOutput = true;
                PStart.RedirectStandardError  = true;

                Process P = new() { StartInfo = PStart };

                P.EnableRaisingEvents = true;
                P.Exited += (object sender, EventArgs e) =>
                {
                    if (P.ExitCode == 0)
                    {
                        Logger.LogInformation($"Process completed successfully: {ExecPath}");
                    }
                    else
                    {
                        Logger.LogError($"Process exited with code {P.ExitCode}: {ExecPath}");
                    }
                };
                P.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
                {
                    if (e.Data == null)
                    {
                        return;
                    }
                    KLog.GetLogger <Process>().LogInformation($"{PStart.FileName}: {e.Data}");
                };
                P.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
                {
                    if (e.Data == null)
                    {
                        return;
                    }
                    KLog.GetLogger <Process>().LogError($"{P.StartInfo.FileName}: {e.Data}");
                };

                P.Start();
                P.BeginErrorReadLine();
                P.BeginOutputReadLine();
                return(P);
            }
            catch (Exception Ex)
            {
                Logger.LogError(Ex, $"Failed to exec: {ExecPath}");
            }

            return(null);
        }