inital code for snapshoting on cron

This commit is contained in:
2022-12-19 07:16:24 -07:00
parent 965af25890
commit 3c0d373e6e
10 changed files with 338 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
package configuration
import (
"os"
"path/filepath"
"github.com/pelletier/go-toml/v2"
"go.uber.org/zap"
)
const (
SNAPZ_CONFIG_ROOT = "/etc/snapz"
SNAPZ_JOBS_FILE = "jobs.toml"
SNAPZ_DATASET_DIRECTORY = "datasets.toml"
)
/*
[[job]]
name = "example"
description = "example job"
cron = "* /1 * * * *"
recursive = true
*/
type JobConfig struct {
Name string `toml:"name"`
Description string `toml:"description"`
Cron string `toml:"cron"`
Dataset string `toml:"dataset"`
Recursive bool `toml:"recusrive"`
}
type JobsConfig struct {
Jobs []JobConfig `toml:"job"`
}
func getConfigPaths(filename string) []string {
global_file_path := filepath.Join(SNAPZ_CONFIG_ROOT, filename)
return []string{global_file_path}
}
func GetJobs() ([]JobConfig, error) {
job_file_paths := getConfigPaths(SNAPZ_JOBS_FILE)
jobs := make([]JobConfig, 0)
for _, file := range job_file_paths {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
var jobsConfig JobsConfig
err = toml.Unmarshal(data, &jobsConfig)
if err != nil {
return nil, err
}
zap.S().Infof("adding %v jobs to queue", len(jobsConfig.Jobs))
jobs = append(jobs, jobsConfig.Jobs...)
}
return jobs, nil
}
+62
View File
@@ -0,0 +1,62 @@
package configuration
import (
"io/ioutil"
"time"
"github.com/go-co-op/gocron"
"gopkg.in/yaml.v3"
)
type Schedule struct {
Every int
Unit string
At []string
}
func (s Schedule) ToGocron() *gocron.Scheduler {
out := gocron.NewScheduler(time.Local)
out = out.Every(s.Every)
switch s.Unit {
case "Day":
out = out.Day()
case "Hour":
out = out.Hour()
default:
return nil
}
for _, at := range s.At {
out = out.At(at)
}
return out
}
type SnapJob struct {
Name string `yaml:"name"`
DatasetPath string `yaml:"dataset_path"`
Description string `yaml:"descriptions"`
Recursive bool `yaml:"recursive"`
Schedules []Schedule `yaml:"schedules"`
Cron string `yaml:"cron"`
}
func LoadSnap(path string) (*SnapJob, error) {
configFile, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var jobInfo SnapJob
err = yaml.Unmarshal(configFile, &jobInfo)
if err != nil {
return nil, err
}
return &jobInfo, nil
}