Hella comments

This commit is contained in:
IamTheFij 2018-08-03 17:30:17 -07:00
parent 3dcc190d3f
commit c840d7b0b2
1 changed files with 16 additions and 2 deletions

18
main.go
View File

@ -10,8 +10,15 @@ import (
"time" "time"
) )
// WatchInterval is the duration we should sleep until polling Docker
var WatchInterval = (5 * time.Second) 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 { type ContainerStartJob struct {
Client *client.Client Client *client.Client
ContainerID string ContainerID string
@ -20,6 +27,8 @@ type ContainerStartJob struct {
Schedule string Schedule string
} }
// Run is executed based on the ContainerStartJob Schedule and starts the
// container
func (job ContainerStartJob) Run() { func (job ContainerStartJob) Run() {
fmt.Println("Starting:", job.Name) fmt.Println("Starting:", job.Name)
err := job.Client.ContainerStart(job.Context, job.ContainerID, types.ContainerStartOptions{}) 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) { func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
fmt.Println("Scanning containers for new schedules...") fmt.Println("Scanning containers for new schedules...")
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true}) 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 { for _, container := range containers {
if val, ok := container.Labels["cron.schedule"]; ok { if val, ok := container.Labels[SchedLabel]; ok {
jobName := strings.Join(container.Names, "/") jobName := strings.Join(container.Names, "/")
jobs = append(jobs, ContainerStartJob{ jobs = append(jobs, ContainerStartJob{
Schedule: val, Schedule: val,
@ -51,6 +62,8 @@ func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
return 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) { func ScheduleJobs(c *cron.Cron, jobs []ContainerStartJob) {
for _, job := range jobs { for _, job := range jobs {
fmt.Printf("Scheduling %s (%s) with schedule '%s'\n", job.Name, job.ContainerID[:10], job.Schedule) 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() { func main() {
// Get a Docker Client
cli, err := client.NewEnvClient() cli, err := client.NewEnvClient()
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Create a cron // Create a Cron
c := cron.New() c := cron.New()
// Start the loop // Start the loop