Hella comments
This commit is contained in:
parent
3dcc190d3f
commit
c840d7b0b2
18
main.go
18
main.go
@ -10,8 +10,15 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// WatchInterval is the duration we should sleep until polling Docker
|
||||
var WatchInterval = (5 * time.Second)
|
||||
|
||||
// SchedLabel is the string label to search for cron expressions
|
||||
var SchedLabel = "cron.schedule"
|
||||
|
||||
// ContainerStartJob represents a scheduled container task
|
||||
// It contains a reference to a client, the schedule to run on, and the
|
||||
// ID of that container that should be started
|
||||
type ContainerStartJob struct {
|
||||
Client *client.Client
|
||||
ContainerID string
|
||||
@ -20,6 +27,8 @@ type ContainerStartJob struct {
|
||||
Schedule string
|
||||
}
|
||||
|
||||
// Run is executed based on the ContainerStartJob Schedule and starts the
|
||||
// container
|
||||
func (job ContainerStartJob) Run() {
|
||||
fmt.Println("Starting:", job.Name)
|
||||
err := job.Client.ContainerStart(job.Context, job.ContainerID, types.ContainerStartOptions{})
|
||||
@ -28,6 +37,8 @@ func (job ContainerStartJob) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
// QueryScheduledJobs queries Docker for all containers with a schedule and
|
||||
// returns a list of ContainerStartJob records to be scheduled
|
||||
func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
|
||||
fmt.Println("Scanning containers for new schedules...")
|
||||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
|
||||
@ -36,7 +47,7 @@ func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
|
||||
}
|
||||
|
||||
for _, container := range containers {
|
||||
if val, ok := container.Labels["cron.schedule"]; ok {
|
||||
if val, ok := container.Labels[SchedLabel]; ok {
|
||||
jobName := strings.Join(container.Names, "/")
|
||||
jobs = append(jobs, ContainerStartJob{
|
||||
Schedule: val,
|
||||
@ -51,6 +62,8 @@ func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
|
||||
return
|
||||
}
|
||||
|
||||
// ScheduleJobs accepts a Cron instance and a list of jobs to schedule.
|
||||
// It then schedules the provided jobs
|
||||
func ScheduleJobs(c *cron.Cron, jobs []ContainerStartJob) {
|
||||
for _, job := range jobs {
|
||||
fmt.Printf("Scheduling %s (%s) with schedule '%s'\n", job.Name, job.ContainerID[:10], job.Schedule)
|
||||
@ -59,12 +72,13 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerStartJob) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Get a Docker Client
|
||||
cli, err := client.NewEnvClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create a cron
|
||||
// Create a Cron
|
||||
c := cron.New()
|
||||
|
||||
// Start the loop
|
||||
|
Loading…
Reference in New Issue
Block a user