예제 #1
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
                        });
                    }
                }
            }
        }
예제 #2
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);
            };
        }
예제 #3
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);
        }