70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
|
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...")
|
||
|
}
|
||
|
}
|