예제 #1
0
 public TimerService(
     IOptions <TimerConfiguration> config,
     IWebhookHandler webhookHandler)
 {
     _webhookHandler = webhookHandler;
     _config         = config.Value;
 }
예제 #2
0
        public RepeatingTimer(TimerConfiguration configuration)
        {
            var due    = configuration.DueSeconds > 0 ? configuration.DueSeconds * 1000 : configuration.DueSeconds;
            var period = configuration.PeriodSeconds > 0 ? configuration.PeriodSeconds * 1000 : configuration.PeriodSeconds;

            timer = new Timer(Tick, null, due, period);
        }
예제 #3
0
파일: Timers.cs 프로젝트: pk8est/Antd
        public void Remove(string name)
        {
            var bash = new Bash();

            bash.Execute($"systemctl stop {name}.target", false);
            var timerFile = $"{TargetDirectory}/{name}.target";

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

            bash.Execute($"systemctl stop {name}.service", false);
            var serviceFile = $"{TargetDirectory}/{name}.service";

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

            var schedulerConfiguration = new TimerConfiguration();
            var tryget = schedulerConfiguration.Get().Timers.FirstOrDefault(_ => _.Alias == name);

            if (tryget != null)
            {
                schedulerConfiguration.RemoveTimer(tryget.Guid);
            }

            bash.Execute("systemctl daemon-reload", false);
        }
예제 #4
0
파일: Timers.cs 프로젝트: diycp/Antd
        public static void Export()
        {
            var schedulerConfiguration = new TimerConfiguration();
            var all = schedulerConfiguration.Get().Timers;

            foreach (var tt in all)
            {
                Create(tt.Alias, tt.Time, tt.Command);
            }
        }
예제 #5
0
파일: Timers.cs 프로젝트: diycp/Antd
        public static void Import()
        {
            var files = Directory.EnumerateFiles(TargetDirectory).ToList();

            foreach (var file in files.Where(_ => _.EndsWith(".target")))
            {
                var name = Path.GetFileName(file);
                if (name != null)
                {
                    var coreName = name.Replace(".timer", "");
                    var schedulerConfiguration = new TimerConfiguration();
                    var tryget = schedulerConfiguration.Get().Timers.FirstOrDefault(_ => _.Alias == coreName);
                    var time   = "";
                    if (!File.Exists(file))
                    {
                        continue;
                    }
                    var lines = File.ReadAllLines(file);
                    var t     = lines.FirstOrDefault(_ => _.Contains("OnCalendar"));
                    if (t != null)
                    {
                        time = t.SplitToList("=").Last();
                    }

                    var command = "";
                    var eqPath  = files.FirstOrDefault(_ => _.Contains(coreName) && _.EndsWith(".service"));
                    if (eqPath != null)
                    {
                        if (!File.Exists(eqPath))
                        {
                            continue;
                        }
                        var clines = File.ReadAllLines(eqPath);
                        var c      = clines.FirstOrDefault(_ => _.Contains("ExecStart"));
                        if (c != null)
                        {
                            command = c.SplitToList("=").Last();
                        }
                    }

                    if (tryget == null)
                    {
                        schedulerConfiguration.AddTimer(new TimerModel {
                            Alias     = coreName,
                            Command   = command,
                            Time      = time,
                            IsEnabled = true
                        });
                    }
                }
            }
        }
예제 #6
0
        public AntdSchedulerModule()
        {
            Get["/scheduler"] = x => {
                var schedulerConfiguration = new TimerConfiguration();
                var scheduledJobs          = schedulerConfiguration.Get().Timers;
                var model = new PageSchedulerModel {
                    Jobs = scheduledJobs?.ToList().OrderBy(_ => _.Alias)
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/scheduler/set"] = x => {
                var schedulerConfiguration = new TimerConfiguration();
                schedulerConfiguration.Set();
                return(HttpStatusCode.OK);
            };

            Post["/scheduler/enable"] = x => {
                var dhcpdConfiguration = new TimerConfiguration();
                dhcpdConfiguration.Enable();
                return(HttpStatusCode.OK);
            };

            Post["/scheduler/disable"] = x => {
                var dhcpdConfiguration = new TimerConfiguration();
                dhcpdConfiguration.Disable();
                return(HttpStatusCode.OK);
            };

            Post["/scheduler/timer"] = x => {
                string alias   = Request.Form.Alias;
                string time    = Request.Form.Time;
                string command = Request.Form.Command;
                var    model   = new TimerModel {
                    Alias     = alias,
                    Time      = time,
                    Command   = command,
                    IsEnabled = true
                };
                var schedulerConfiguration = new TimerConfiguration();
                schedulerConfiguration.AddTimer(model);
                return(HttpStatusCode.OK);
            };

            Post["/scheduler/timer/del"] = x => {
                string guid = Request.Form.Guid;
                var    schedulerConfiguration = new TimerConfiguration();
                schedulerConfiguration.RemoveTimer(guid);
                return(HttpStatusCode.OK);
            };
        }
예제 #7
0
파일: Zpool.cs 프로젝트: pk8est/Antd
        public List <ZpoolModel> List()
        {
            var result = Bash.Execute("zpool list");
            var list   = new List <ZpoolModel>();

            if (string.IsNullOrEmpty(result))
            {
                return(list);
            }
            var lines = result.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList().Skip(1);

            foreach (var line in lines)
            {
                var cells = Regex.Split(line, @"\s+");
                var model = new ZpoolModel {
                    Guid     = Guid.NewGuid().ToString(),
                    Name     = cells[0],
                    Size     = cells[1],
                    Alloc    = cells[2],
                    Free     = cells[3],
                    Expandsz = cells[4],
                    Frag     = cells[5],
                    Cap      = cells[6],
                    Dedup    = cells[7],
                    Health   = cells[8],
                    Altroot  = cells[9],
                    Status   = Bash.Execute($"zpool status {cells[0]}")
                };

                var schedulerConfiguration = new TimerConfiguration();

                var jobs = schedulerConfiguration.Get().Timers.Where(_ => _.Alias == model.Name).ToList();
                if (jobs.Any())
                {
                    var j = jobs.FirstOrDefault();
                    if (j != null)
                    {
                        model.HasSnapshot  = _timers.IsActive(model.Name);
                        model.Snapshot     = j.Time;
                        model.SnapshotGuid = j.Guid;
                    }
                }

                list.Add(model);
            }
            return(list);
        }
예제 #8
0
파일: Timers.cs 프로젝트: diycp/Antd
        public static void Remove(string name)
        {
            Bash.Execute($"systemctl stop {name}.target", false);
            var timerFile = $"{Parameter.TimerUnitsLinks}/{name}.target";

            if (File.Exists(timerFile))
            {
                File.Delete(timerFile);
            }
            var timerFile2 = $"{Parameter.TimerUnits}/{name}.target";

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

            Bash.Execute($"systemctl stop {name}.service", false);
            var serviceFile = $"{Parameter.TimerUnitsLinks}/{name}.service";

            if (File.Exists(serviceFile))
            {
                File.Delete(serviceFile);
            }
            var serviceFile2 = $"{Parameter.TimerUnits}/{name}.service";

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

            var schedulerConfiguration = new TimerConfiguration();
            var tryget = schedulerConfiguration.Get().Timers.FirstOrDefault(_ => _.Alias == name);

            if (tryget != null)
            {
                schedulerConfiguration.RemoveTimer(tryget.Guid);
            }

            Bash.Execute("systemctl daemon-reload", false);
        }
예제 #9
0
파일: Timers.cs 프로젝트: diycp/Antd
        public static void Create(string name, string time, string command)
        {
            var timerFile = $"{TargetDirectory}/{name}.timer";

            if (File.Exists(timerFile))
            {
                File.Delete(timerFile);
            }
            var timerText = new List <string> {
                "[Unit]",
                $"Description={name} Timer",
                "",
                "[Timer]",
                $"OnCalendar={time}",
                "Persistent=true",
                "",
                "[Install]",
                "WantedBy=tt.target",
                ""
            };

            FileWithAcl.WriteAllLines(timerFile, timerText, "644", "root", "wheel");

            var serviceFile = $"{TargetDirectory}/{name}.service";

            if (File.Exists(serviceFile))
            {
                File.Delete(serviceFile);
            }
            var serviceText = new List <string> {
                "[Unit]",
                $"Description={name} Service",
                "",
                "[Service]",
                "Type=oneshot",
                "ExecStartPre=/bin/bash -c \"/usr/bin/systemctl set-environment TTDATE=$(/bin/date +'%%Y%%m%%d-%%H%%M%%S')\"",
                $"ExecStart={command}",
                "",
                "[Install]",
                "WantedBy=tt.target",
                ""
            };

            FileWithAcl.WriteAllLines(serviceFile, serviceText, "644", "root", "wheel");

            var schedulerConfiguration = new TimerConfiguration();
            var tryget = schedulerConfiguration.Get().Timers.FirstOrDefault(_ => _.Alias == name);

            if (tryget == null)
            {
                schedulerConfiguration.AddTimer(new TimerModel {
                    Alias     = name,
                    Command   = command,
                    Time      = time,
                    IsEnabled = true
                });
            }
            Bash.Execute($"chown root:wheel {timerFile}", false);
            Bash.Execute($"chown root:wheel {serviceFile}", false);

            Bash.Execute($"ln -s {timerFile} {Parameter.TimerUnitsLinks}/{name}.timer");
            Bash.Execute($"ln -s {serviceFile} {Parameter.TimerUnitsLinks}/{name}.service");

            Bash.Execute("systemctl daemon-reload", false);
        }
예제 #10
0
 /// <summary>
 /// Creates a new instance of the timer
 /// </summary>
 public TimerService()
 {
     this.m_configuration = ApplicationContext.Current.GetService <IConfigurationManager>().GetSection("marc.hi.svc.core.timer") as TimerConfiguration;
 }
예제 #11
0
        private TimerConfiguration GetTimerConfiguration()
        {
            TimerConfiguration ConfigTrans = null;

            switch ((RichTimerType)this.enumComboBox_timerType.SelectedValue)
            {
            case RichTimerType.None:
            {
                return(null);
            }

            case RichTimerType.EverySpan:
            {
                ConfigTrans = new TimerConfiguration();
                ConfigTrans.RichTimerType = RichTimerType.EverySpan;
                ConfigTrans.Hour          = (int)this.numericUpDown_spanhour.Value;
                ConfigTrans.Minute        = (int)this.numericUpDown_spanmins.Value;
                return(ConfigTrans);
            }

            case RichTimerType.PerMonth:
            {
                ConfigTrans = new TimerConfiguration();
                ConfigTrans.RichTimerType = RichTimerType.PerMonth;
                ConfigTrans.Day           = (int)this.numericUpDown_day.Value;
                ConfigTrans.Hour          = (int)this.numericUpDown_hours.Value;
                ConfigTrans.Minute        = (int)this.numericUpDown_mins.Value;
                //ConfigTrans.Second =(int)this.
                return(ConfigTrans);
            }

            case RichTimerType.PerWeek:
            {
                ConfigTrans = new TimerConfiguration();
                ConfigTrans.RichTimerType = RichTimerType.PerWeek;
                ConfigTrans.DayOfWeek     = (int)this.comboBox_week.SelectedIndex;
                ConfigTrans.Hour          = (int)this.numericUpDown_hours.Value;
                ConfigTrans.Minute        = (int)this.numericUpDown_mins.Value;
                return(ConfigTrans);
            }

            case RichTimerType.PerDay:
            {
                ConfigTrans = new TimerConfiguration();
                ConfigTrans.RichTimerType = RichTimerType.PerDay;
                ConfigTrans.Hour          = (int)this.numericUpDown_hours.Value;
                ConfigTrans.Minute        = (int)this.numericUpDown_mins.Value;
                return(ConfigTrans);
            }

            case RichTimerType.PerHour:
            {
                ConfigTrans = new TimerConfiguration();
                ConfigTrans.RichTimerType = RichTimerType.PerHour;
                ConfigTrans.Hour          = (int)this.numericUpDown_hours.Value;
                ConfigTrans.Minute        = (int)this.numericUpDown_mins.Value;
                return(ConfigTrans);
            }

            default:
            {
                return(null);
            }
            }
        }
예제 #12
0
        private void LoadConfig(TimerConfiguration config)
        {
            if (config == null)
            {
                this.enumComboBox_timerType.SelectedValue = RichTimerType.None;
                this.numericUpDown_day.Value      = 1;
                this.comboBox_week.Text           = "天";
                this.numericUpDown_hours.Value    = 0;
                this.numericUpDown_mins.Value     = 0;
                this.numericUpDown_spanhour.Value = 0;
                this.numericUpDown_spanmins.Value = 0;

                this.numericUpDown_day.Enabled      = false;
                this.comboBox_week.Enabled          = false;
                this.numericUpDown_hours.Enabled    = false;
                this.numericUpDown_mins.Enabled     = false;
                this.numericUpDown_spanhour.Enabled = false;
                this.numericUpDown_spanmins.Enabled = false;
                return;
            }

            this.enumComboBox_timerType.SelectedValue = config.RichTimerType;

            switch (config.RichTimerType)
            {
            case RichTimerType.EverySpan:
            {
                this.numericUpDown_day.Value      = 1;
                this.comboBox_week.Text           = "天";
                this.numericUpDown_hours.Value    = 0;
                this.numericUpDown_mins.Value     = 0;
                this.numericUpDown_spanhour.Value = config.Hour;
                this.numericUpDown_spanmins.Value = config.Minute;

                this.numericUpDown_day.Enabled      = false;
                this.comboBox_week.Enabled          = false;
                this.numericUpDown_hours.Enabled    = false;
                this.numericUpDown_mins.Enabled     = false;
                this.numericUpDown_spanhour.Enabled = true;
                this.numericUpDown_spanmins.Enabled = true;
                break;
            }

            case RichTimerType.PerDay:
            {
                this.numericUpDown_day.Enabled      = false;
                this.comboBox_week.Enabled          = false;
                this.numericUpDown_hours.Enabled    = true;
                this.numericUpDown_mins.Enabled     = true;
                this.numericUpDown_spanhour.Enabled = false;
                this.numericUpDown_spanmins.Enabled = false;

                this.numericUpDown_day.Value      = 1;
                this.comboBox_week.Text           = "天";
                this.numericUpDown_hours.Value    = config.Hour;
                this.numericUpDown_mins.Value     = config.Minute;
                this.numericUpDown_spanhour.Value = 0;
                this.numericUpDown_spanmins.Value = 0;
                break;
            }

            case RichTimerType.PerHour:
            {
                this.numericUpDown_day.Enabled      = false;
                this.comboBox_week.Enabled          = false;
                this.numericUpDown_hours.Enabled    = false;
                this.numericUpDown_mins.Enabled     = true;
                this.numericUpDown_spanhour.Enabled = false;
                this.numericUpDown_spanmins.Enabled = false;

                this.numericUpDown_day.Value      = 1;
                this.comboBox_week.Text           = "天";
                this.numericUpDown_hours.Value    = 0;
                this.numericUpDown_mins.Value     = config.Minute;
                this.numericUpDown_spanhour.Value = 0;
                this.numericUpDown_spanmins.Value = 0;
                break;
            }

            case RichTimerType.PerMonth:
            {
                this.numericUpDown_day.Enabled      = true;
                this.comboBox_week.Enabled          = false;
                this.numericUpDown_hours.Enabled    = true;
                this.numericUpDown_mins.Enabled     = true;
                this.numericUpDown_spanhour.Enabled = false;
                this.numericUpDown_spanmins.Enabled = false;

                this.numericUpDown_day.Value      = config.Day;
                this.comboBox_week.Text           = "天";
                this.numericUpDown_hours.Value    = config.Hour;
                this.numericUpDown_mins.Value     = config.Minute;
                this.numericUpDown_spanhour.Value = 0;
                this.numericUpDown_spanmins.Value = 0;
                break;
            }

            case RichTimerType.PerWeek:
            {
                this.numericUpDown_day.Enabled      = false;
                this.comboBox_week.Enabled          = true;
                this.numericUpDown_hours.Enabled    = true;
                this.numericUpDown_mins.Enabled     = true;
                this.numericUpDown_spanhour.Enabled = false;
                this.numericUpDown_spanmins.Enabled = false;

                this.numericUpDown_day.Value      = 1;
                this.comboBox_week.SelectedItem   = config.DayOfWeek;
                this.numericUpDown_hours.Value    = config.Hour;
                this.numericUpDown_mins.Value     = config.Minute;
                this.numericUpDown_spanhour.Value = 0;
                this.numericUpDown_spanmins.Value = 0;
                break;
            }
            }
        }