WIP - Actually kinda works!

This commit is contained in:
IamTheFij 2018-08-01 08:56:13 -07:00
parent 2fc174ad96
commit 2318a12ff2

69
main.go Normal file
View File

@ -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...")
}
}