diff --git a/main.go b/main.go index 7d48ac1..ee9595f 100644 --- a/main.go +++ b/main.go @@ -28,9 +28,9 @@ var ( cfg *viper.Viper ) -func update(publisher JobUpdatePublisher, state messaging.JobState, jobId string, hostname string, msg string) (*messaging.UpdateMessage, error) { +func update(publisher JobUpdatePublisher, state messaging.JobState, jobID string, hostname string, msg string) (*messaging.UpdateMessage, error) { updateMessage := &messaging.UpdateMessage{ - Job: messaging.JobDetails{InvocationID: jobId}, + Job: messaging.JobDetails{InvocationID: jobID}, State: state, Message: msg, Sender: hostname, @@ -38,7 +38,7 @@ func update(publisher JobUpdatePublisher, state messaging.JobState, jobId string err := publisher.PublishJobUpdate(updateMessage) if err == nil { - log.Infof("%s (%s) [%s]: %s", jobId, state, hostname, msg) + log.Infof("%s (%s) [%s]: %s", jobID, state, hostname, msg) return updateMessage, nil } @@ -56,7 +56,7 @@ func update(publisher JobUpdatePublisher, state messaging.JobState, jobId string // Attempt to record the message one more time. err = publisher.PublishJobUpdate(updateMessage) if err == nil { - log.Infof("%s (%s) [%s]: %s", jobId, state, hostname, msg) + log.Infof("%s (%s) [%s]: %s", jobID, state, hostname, msg) return updateMessage, nil } @@ -64,6 +64,7 @@ func update(publisher JobUpdatePublisher, state messaging.JobState, jobId string return nil, err } +// MessagePost describes the structure of the job status update request body. type MessagePost struct { Hostname string Message string @@ -94,13 +95,13 @@ func postUpdate(publisher JobUpdatePublisher, w http.ResponseWriter, r *http.Req var updateMessage MessagePost vars := mux.Vars(r) - jobId := vars["uuid"] + jobID := vars["uuid"] err := json.NewDecoder(r.Body).Decode(&updateMessage) if err != nil { w.WriteHeader(http.StatusBadRequest) log.Error(err) - out.Encode(map[string]string{ + _ = out.Encode(map[string]string{ "error": err.Error(), }) return @@ -110,22 +111,22 @@ func postUpdate(publisher JobUpdatePublisher, w http.ResponseWriter, r *http.Req if err != nil { w.WriteHeader(http.StatusBadRequest) log.Error(err) - out.Encode(map[string]string{ + _ = out.Encode(map[string]string{ "error": err.Error(), }) return } - msg, err := update(publisher, state, jobId, updateMessage.Hostname, updateMessage.Message) + msg, err := update(publisher, state, jobID, updateMessage.Hostname, updateMessage.Message) if err != nil { w.WriteHeader(http.StatusBadRequest) log.Error(err) - out.Encode(map[string]string{ + _ = out.Encode(map[string]string{ "error": err.Error(), }) log.Fatal("failed to record a valid job status update - aborting") } - out.Encode(msg) + _ = out.Encode(msg) } func init() { diff --git a/publisher.go b/publisher.go index 2e2a1af..7997322 100644 --- a/publisher.go +++ b/publisher.go @@ -12,6 +12,8 @@ type JobUpdatePublisher interface { Close() } +// DefaultJobUpdatePublisher provides a wrapper around messaging.Client that adds support for +// reestablishing stale connections. type DefaultJobUpdatePublisher struct { uri string exchange string @@ -24,11 +26,15 @@ func newMessagingClient(uri, exchange string, reconnect bool) (*messaging.Client return nil, err } - client.SetupPublishing(exchange) + err = client.SetupPublishing(exchange) + if err != nil { + return nil, err + } return client, nil } +// NewDefaultJobUpdatePublisher returns a new instance of DefaultJobUpdatePublisher. func NewDefaultJobUpdatePublisher(uri, exchange string) (*DefaultJobUpdatePublisher, error) { client, err := newMessagingClient(uri, exchange, true) if err != nil { @@ -43,10 +49,12 @@ func NewDefaultJobUpdatePublisher(uri, exchange string) (*DefaultJobUpdatePublis return publisher, nil } +// PublishJobUpdate simply forwards the function call to messaging.Client.PublishJobUpdate. func (c *DefaultJobUpdatePublisher) PublishJobUpdate(m *messaging.UpdateMessage) error { return c.client.PublishJobUpdate(m) } +// Reconnect closes the existing messaging client connection and establishes a new one. func (c *DefaultJobUpdatePublisher) Reconnect() error { c.client.Close() @@ -59,6 +67,7 @@ func (c *DefaultJobUpdatePublisher) Reconnect() error { return nil } +// Close closes the messaging client connection. func (c *DefaultJobUpdatePublisher) Close() { c.client.Close() }