From 2318a12ff228fa9584d6776feb4a819895c8deb3 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Wed, 1 Aug 2018 08:56:13 -0700 Subject: [PATCH] WIP - Actually kinda works! --- main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..d78a2ac --- /dev/null +++ b/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" + "github.com/robfig/cron" + "golang.org/x/net/context" + "io" + "log" + "os" + "strings" + "time" +) + +type ContainerStartJob struct { + Client *client.Client + ContainerID string + Context context.Context + Name string + Schedule string +} + +func (job ContainerStartJob) Run() { + fmt.Println("Starting: ", job.Name) + err := job.Client.ContainerStart(job.Context, job.ContainerID, types.ContainerStartOptions{}) + if err != nil { + panic(err) + } +} + +func main() { + cli, err := client.NewEnvClient() + if err != nil { + panic(err) + } + + containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true}) + if err != nil { + panic(err) + } + + c := cron.New() + + jobs := []ContainerStartJob{} + + for _, container := range containers { + if val, ok := container.Labels["cron.schedule"]; ok { + jobName := strings.Join(container.Names, "/") + fmt.Println("Scheduling ", jobName, "(", val, ")") + c.AddJob(val, ContainerStartJob{ + Schedule: val, + Client: cli, + ContainerID: container.ID, + Context: context.Background(), + Name: jobName, + }) + } + } + + // Start the cron job threads + c.Start() + + // Start the loop + for { + time.Sleep(5 * time.Second) + fmt.Println("Tick...") + } +}