Skip to content

Commit

Permalink
Merge pull request #33 from buildboxhq/refactor-logging
Browse files Browse the repository at this point in the history
Refactor logging
  • Loading branch information
keithpitt committed Jun 13, 2014
2 parents 4a095c0 + 47acca0 commit 6e9b99b
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 102 deletions.
11 changes: 8 additions & 3 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"os"
"fmt"
"log"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/buildboxhq/buildbox-agent/buildbox"
)
Expand Down Expand Up @@ -124,6 +124,11 @@ func main() {
}

func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent {
// Init debugging
if c.Bool("debug") {
buildbox.LoggerInitDebug()
}

agentAccessToken := c.String("access-token")

// Should we look to the environment for the agent access token?
Expand Down Expand Up @@ -151,24 +156,20 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent {

// Set the agent options
var agent buildbox.Agent;
agent.Debug = c.Bool("debug")
agent.BootstrapScript = bootstrapScript

// Client specific options
agent.Client.AgentAccessToken = agentAccessToken
agent.Client.URL = c.String("url")
agent.Client.Debug = agent.Debug

// Tell the user that debug mode has been enabled
if agent.Debug {
log.Printf("Debug mode enabled")
}

// Setup the agent
agent.Setup()

// A nice welcome message
log.Printf("Started buildbox-agent `%s` (version %s)\n", agent.Name, buildbox.Version)
buildbox.Logger.WithFields(logrus.Fields{
"pid": os.Getpid(),
"version": buildbox.Version,
}).Infof("Started buildbox-agent `%s`", agent.Name)

return &agent
}
23 changes: 10 additions & 13 deletions artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"os"
"fmt"
"log"
"github.com/codegangsta/cli"
"github.com/buildboxhq/buildbox-agent/buildbox"
)
Expand Down Expand Up @@ -75,6 +74,11 @@ func main() {
cli.BoolFlag{"debug", "Enable debug mode"},
},
Action: func(c *cli.Context) {
// Init debugging
if c.Bool("debug") {
buildbox.LoggerInitDebug()
}

agentAccessToken := c.String("agent-access-token")

// Should we look to the environment for the agent access token?
Expand Down Expand Up @@ -114,41 +118,34 @@ func main() {

// Set the agent options
var agent buildbox.Agent;
agent.Debug = c.Bool("debug")

// Client specific options
agent.Client.AgentAccessToken = agentAccessToken
agent.Client.URL = c.String("url")
agent.Client.Debug = agent.Debug

// Tell the user that debug mode has been enabled
if agent.Debug {
log.Printf("Debug mode enabled")
}

// Setup the agent
agent.Setup()

// Find the actual job now
job, err := agent.Client.JobFind(jobId)
if err != nil {
log.Fatalf("Could not find job: %s", jobId)
buildbox.Logger.Fatalf("Could not find job: %s", jobId)
}

// Create artifact structs for all the files we need to upload
artifacts, err := buildbox.CollectArtifacts(job, paths)
if err != nil {
log.Fatalf("Failed to collect artifacts: %s", err)
buildbox.Logger.Fatalf("Failed to collect artifacts: %s", err)
}

if len(artifacts) == 0 {
log.Printf("No files matched paths: %s", paths)
buildbox.Logger.Infof("No files matched paths: %s", paths)
} else {
log.Printf("Uploading %d files that match \"%s\"", len(artifacts), paths)
buildbox.Logger.Infof("Found %d files that match \"%s\"", len(artifacts), paths)

err := buildbox.UploadArtifacts(agent.Client, job, artifacts, destination)
if err != nil {
log.Fatalf("Failed to upload artifacts: %s", err)
buildbox.Logger.Fatalf("Failed to upload artifacts: %s", err)
}
}
},
Expand Down
22 changes: 9 additions & 13 deletions buildbox/agent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package buildbox

import (
"log"
"fmt"
"time"
"strings"
Expand All @@ -22,9 +21,6 @@ type Agent struct {
// The hostname of the agent
Hostname string `json:"hostname,omitempty"`

// Whether to run the agent in Debug mode
Debug bool

// The boostrap script to run
BootstrapScript string

Expand All @@ -51,7 +47,7 @@ func (a *Agent) Setup() {
// Figure out the hostname of the current machine
hostname, err := exec.Command("hostname").Output()
if err != nil {
log.Fatal(err)
Logger.Fatal(err)
}

// Set the hostname
Expand All @@ -61,7 +57,7 @@ func (a *Agent) Setup() {
// current agent struct with data.
err = a.Client.AgentUpdate(a)
if err != nil {
log.Fatal(err)
Logger.Fatal(err)
}
}

Expand All @@ -82,11 +78,11 @@ func (a *Agent) MonitorSignals() {
// This will block until a signal is sent
sig := <-signals

log.Printf("Received signal `%s`", sig.String())
Logger.Debugf("Received signal `%s`", sig.String())

// Only monitor certain signals
if sig != syscall.SIGINT && sig != syscall.SIGUSR2 {
log.Printf("Ignoring signal `%s`", sig.String())
Logger.Debugf("Ignoring signal `%s`", sig.String())

// Start monitoring signals again
a.MonitorSignals()
Expand All @@ -96,7 +92,7 @@ func (a *Agent) MonitorSignals() {

// If the agent isn't running a job, exit right away
if a.Job == nil {
log.Printf("No jobs running. Exiting...")
Logger.Info("No jobs running. Exiting...")
os.Exit(1)
}

Expand All @@ -112,7 +108,7 @@ func (a *Agent) MonitorSignals() {
// Die time.
os.Exit(1)
} else {
log.Print("Exiting... Waiting for job to finish before stopping. Send signal again to exit immediately.")
Logger.Info("Exiting... Waiting for job to finish before stopping. Send signal again to exit immediately.")

a.stopping = true
}
Expand All @@ -133,7 +129,7 @@ func (a *Agent) Start() {
for {
job, err := a.Client.JobNext()
if err != nil {
log.Printf("Failed to get job (%s)", err)
Logger.Errorf("Failed to get job (%s)", err)
break
}

Expand Down Expand Up @@ -162,11 +158,11 @@ func (a *Agent) Run(id string) {
job, err := a.Client.JobFindAndAssign(id)

if err != nil {
log.Fatal(err)
Logger.Fatal(err)
}

if job.State != "scheduled" {
log.Fatalf("The agent can only run scheduled jobs. Current state is `%s`", job.State)
Logger.Fatalf("The agent can only run scheduled jobs. Current state is `%s`", job.State)
}

// Run the paticular job
Expand Down
14 changes: 9 additions & 5 deletions buildbox/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"strings"
"log"
"errors"
"path/filepath"
"mime"
Expand Down Expand Up @@ -97,6 +96,8 @@ func CollectArtifacts(job *Job, artifactPaths string) (artifacts []*Artifact, er
glob = strings.TrimSpace(glob)

if glob != "" {
Logger.Debugf("Globbing %s for %s", workingDirectory, glob)

files, err := Glob(workingDirectory, glob)
if err != nil {
return nil, err
Expand Down Expand Up @@ -181,6 +182,8 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination
var routines []chan string
var concurrency int = 10

Logger.Debugf("Spinning up %d concurrent threads for uploads", concurrency)

count := 0
for _, artifact := range createdArtifacts {
// Create a channel and apend it to the routines array. Once we've hit our
Expand All @@ -192,7 +195,8 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination
routines = append(routines, wait)

if count >= concurrency {
// fmt.Printf("Maxiumum concurrent threads running. Waiting.\n")
Logger.Debug("Maxiumum concurrent threads running. Waiting.")

// Wait for all the routines to finish, then reset
waitForRoutines(routines)
count = 0
Expand All @@ -208,22 +212,22 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination

func uploadRoutine(quit chan string, client Client, job *Job, artifact Artifact, uploader Uploader) {
// Show a nice message that we're starting to upload the file
log.Printf("Uploading %s\n", artifact.Path)
Logger.Infof("Uploading %s (%d bytes)", artifact.Path, artifact.FileSize)

// Upload the artifact and then set the state depending on whether or not
// it passed.
err := uploader.Upload(&artifact)
if err != nil {
artifact.State = "error"
log.Printf("Error uploading artifact %s (%s)", artifact.Path, err)
Logger.Errorf("Error uploading artifact %s (%s)", artifact.Path, err)
} else {
artifact.State = "finished"
}

// Update the state of the artifact on Buildbox
_, err = client.ArtifactUpdate(job, artifact)
if err != nil {
log.Printf("Error marking artifact %s as uploaded (%s)", artifact.Path, err)
Logger.Errorf("Error marking artifact %s as uploaded (%s)", artifact.Path, err)
}

// We can notify the channel that this routine has finished now
Expand Down
20 changes: 1 addition & 19 deletions buildbox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package buildbox

import (
"net/http"
"net/http/httputil"
_ "crypto/sha512" // import sha512 to make sha512 ssl certs work
"encoding/json"
"runtime"
"strings"
"io"
"errors"
"log"
"os"
"bytes"
)

Expand All @@ -27,9 +24,6 @@ type Client struct {
// The access token of the agent being used to make API requests
AgentAccessToken string

// Debug mode can be used to dump the full request and response to stdout.
Debug bool

// UserAgent to be provided in API requests. Set to DefaultUserAgent if not
// specified.
UserAgent string
Expand Down Expand Up @@ -112,9 +106,7 @@ func (c *Client) NewRequest(method string, path string, body interface{}) (*http
// Submits an HTTP request, checks its response, and deserializes
// the response into v.
func (c *Client) DoReq(req *http.Request, v interface{}) error {
if c.Debug {
log.Printf("%s %s\n", req.Method, req.URL)
}
Logger.Debugf("%s %s", req.Method, req.URL)

res, err := http.DefaultClient.Do(req)
if err != nil {
Expand All @@ -125,16 +117,6 @@ func (c *Client) DoReq(req *http.Request, v interface{}) error {
// this function
defer res.Body.Close()

if c.Debug {
dump, err := httputil.DumpResponse(res, true)
if err != nil {
log.Println(err)
} else {
os.Stderr.Write(dump)
os.Stderr.Write([]byte{'\n'})
}
}

// Check the response of the response
if err = checkResp(res); err != nil {
return err
Expand Down
Loading

0 comments on commit 6e9b99b

Please sign in to comment.