Stabilize packaging and scheduler storage
This commit is contained in:
+58
-29
@@ -36,6 +36,10 @@ func OpenStore() (*Store, []Job, error) {
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
normalizeJobs(jobs)
|
||||
if err := store.SaveJobs(jobs); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return store, jobs, nil
|
||||
}
|
||||
|
||||
@@ -93,6 +97,7 @@ func loadOrCreateConfig(paths Paths) (Config, error) {
|
||||
func loadOrCreateJobs(path string) ([]Job, error) {
|
||||
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
||||
jobs := defaultJobs()
|
||||
normalizeJobs(jobs)
|
||||
return jobs, writeYAML(path, JobsFile{Jobs: jobs})
|
||||
}
|
||||
|
||||
@@ -107,6 +112,42 @@ func loadOrCreateJobs(path string) ([]Job, error) {
|
||||
return file.Jobs, nil
|
||||
}
|
||||
|
||||
func normalizeJobs(jobs []Job) {
|
||||
next := 1
|
||||
for index := range jobs {
|
||||
job := &jobs[index]
|
||||
if job.ID <= 0 {
|
||||
job.ID = next
|
||||
}
|
||||
if job.ID >= next {
|
||||
next = job.ID + 1
|
||||
}
|
||||
if strings.TrimSpace(job.Name) == "" {
|
||||
job.Name = "Untitled job"
|
||||
}
|
||||
if strings.TrimSpace(job.Schedule) == "" {
|
||||
job.Schedule = "@every 1m"
|
||||
}
|
||||
if strings.TrimSpace(job.Command) == "" {
|
||||
job.Command = echoCommand("PySentry job ran")
|
||||
}
|
||||
if job.LastRun == "" {
|
||||
job.LastRun = "Never"
|
||||
}
|
||||
if job.Output == "" {
|
||||
job.Output = "No command output captured yet."
|
||||
}
|
||||
if job.Enabled {
|
||||
job.LastState = "Ready"
|
||||
job.NextRun = "After start"
|
||||
} else {
|
||||
job.LastState = "Paused"
|
||||
job.NextRun = "Paused"
|
||||
}
|
||||
job.Logs = nil
|
||||
}
|
||||
}
|
||||
|
||||
func resolveJobsDir(appDir string, jobsDir string) string {
|
||||
return resolveConfiguredDir(appDir, jobsDir)
|
||||
}
|
||||
@@ -138,39 +179,27 @@ func writeYAML(path string, value any) error {
|
||||
func defaultJobs() []Job {
|
||||
return []Job{
|
||||
{
|
||||
ID: 1,
|
||||
Name: "Hello scheduler",
|
||||
Folder: "Examples",
|
||||
Schedule: "@every 10s",
|
||||
Command: echoCommand("PySentry test job: scheduler is alive"),
|
||||
Enabled: true,
|
||||
LastRun: "Never",
|
||||
NextRun: "After start",
|
||||
LastState: "Ready",
|
||||
Output: "No command output captured yet.",
|
||||
ID: 1,
|
||||
Name: "Hello scheduler",
|
||||
Folder: "Examples",
|
||||
Schedule: "@every 10s",
|
||||
Command: echoCommand("PySentry test job: scheduler is alive"),
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
Name: "Write timestamp",
|
||||
Folder: "Examples",
|
||||
Schedule: "@every 15s",
|
||||
Command: echoCommand("PySentry test job: timestamp command ran"),
|
||||
Enabled: true,
|
||||
LastRun: "Never",
|
||||
NextRun: "After start",
|
||||
LastState: "Ready",
|
||||
Output: "No command output captured yet.",
|
||||
ID: 2,
|
||||
Name: "Write timestamp",
|
||||
Folder: "Examples",
|
||||
Schedule: "*/1 * * * *",
|
||||
Command: echoCommand("PySentry test job: timestamp command ran"),
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: 3,
|
||||
Name: "Paused sample",
|
||||
Schedule: "@every 1m",
|
||||
Command: echoCommand("This paused sample should not run until enabled"),
|
||||
Enabled: false,
|
||||
LastRun: "Never",
|
||||
NextRun: "Paused",
|
||||
LastState: "Paused",
|
||||
Output: "No command output captured yet.",
|
||||
ID: 3,
|
||||
Name: "Paused sample",
|
||||
Schedule: "@every 1m",
|
||||
Command: echoCommand("This paused sample should not run until enabled"),
|
||||
Enabled: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user