Add a client interface so I can build unit tests
This commit is contained in:
parent
6e324795d4
commit
e9e555e5a2
2
Makefile
2
Makefile
@ -13,7 +13,7 @@ vendor:
|
|||||||
# Runs the application, useful while developing
|
# Runs the application, useful while developing
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run:
|
run:
|
||||||
go run *.go
|
go run .
|
||||||
|
|
||||||
# Output target
|
# Output target
|
||||||
dockron:
|
dockron:
|
||||||
|
24
main.go
24
main.go
@ -3,8 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/docker/docker/api/types"
|
dockerTypes "github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/client"
|
dockerCient "github.com/docker/docker/client"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"log"
|
"log"
|
||||||
@ -24,11 +24,17 @@ var (
|
|||||||
version = "dev"
|
version = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ContainerClient provides an interface for interracting with Docker
|
||||||
|
type ContainerClient interface {
|
||||||
|
ContainerStart(context context.Context, containerID string, options dockerTypes.ContainerStartOptions) error
|
||||||
|
ContainerList(context context.Context, options dockerTypes.ContainerListOptions) ([]dockerTypes.Container, error)
|
||||||
|
}
|
||||||
|
|
||||||
// ContainerStartJob represents a scheduled container task
|
// ContainerStartJob represents a scheduled container task
|
||||||
// It contains a reference to a client, the schedule to run on, and the
|
// It contains a reference to a client, the schedule to run on, and the
|
||||||
// ID of that container that should be started
|
// ID of that container that should be started
|
||||||
type ContainerStartJob struct {
|
type ContainerStartJob struct {
|
||||||
Client *client.Client
|
Client ContainerClient
|
||||||
ContainerID string
|
ContainerID string
|
||||||
Context context.Context
|
Context context.Context
|
||||||
Name string
|
Name string
|
||||||
@ -39,7 +45,7 @@ type ContainerStartJob struct {
|
|||||||
// container
|
// container
|
||||||
func (job ContainerStartJob) Run() {
|
func (job ContainerStartJob) Run() {
|
||||||
log.Println("Starting:", job.Name)
|
log.Println("Starting:", job.Name)
|
||||||
err := job.Client.ContainerStart(job.Context, job.ContainerID, types.ContainerStartOptions{})
|
err := job.Client.ContainerStart(job.Context, job.ContainerID, dockerTypes.ContainerStartOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -47,9 +53,9 @@ func (job ContainerStartJob) Run() {
|
|||||||
|
|
||||||
// QueryScheduledJobs queries Docker for all containers with a schedule and
|
// QueryScheduledJobs queries Docker for all containers with a schedule and
|
||||||
// returns a list of ContainerStartJob records to be scheduled
|
// returns a list of ContainerStartJob records to be scheduled
|
||||||
func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
|
func QueryScheduledJobs(client ContainerClient) (jobs []ContainerStartJob) {
|
||||||
log.Println("Scanning containers for new schedules...")
|
log.Println("Scanning containers for new schedules...")
|
||||||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
|
containers, err := client.ContainerList(context.Background(), dockerTypes.ContainerListOptions{All: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -59,7 +65,7 @@ func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
|
|||||||
jobName := strings.Join(container.Names, "/")
|
jobName := strings.Join(container.Names, "/")
|
||||||
jobs = append(jobs, ContainerStartJob{
|
jobs = append(jobs, ContainerStartJob{
|
||||||
Schedule: val,
|
Schedule: val,
|
||||||
Client: cli,
|
Client: client,
|
||||||
ContainerID: container.ID,
|
ContainerID: container.ID,
|
||||||
Context: context.Background(),
|
Context: context.Background(),
|
||||||
Name: jobName,
|
Name: jobName,
|
||||||
@ -87,7 +93,7 @@ func ScheduleJobs(c *cron.Cron, jobs []ContainerStartJob) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Get a Docker Client
|
// Get a Docker Client
|
||||||
cli, err := client.NewEnvClient()
|
client, err := dockerClient.NewEnvClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -118,7 +124,7 @@ func main() {
|
|||||||
c = cron.New()
|
c = cron.New()
|
||||||
|
|
||||||
// Schedule jobs again
|
// Schedule jobs again
|
||||||
jobs := QueryScheduledJobs(cli)
|
jobs := QueryScheduledJobs(client)
|
||||||
ScheduleJobs(c, jobs)
|
ScheduleJobs(c, jobs)
|
||||||
c.Start()
|
c.Start()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user