Skip to content

Commit

Permalink
CORE-9075: fixed a bunch of linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
slr71 committed Aug 3, 2018
1 parent 6bcda8c commit c075428
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
21 changes: 11 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ 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,
}

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
}

Expand All @@ -56,14 +56,15 @@ 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
}

log.Errorf("failed to publish job status update again - giving up: %s", err)
return nil, err
}

// MessagePost describes the structure of the job status update request body.
type MessagePost struct {
Hostname string
Message string
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down
11 changes: 10 additions & 1 deletion publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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()

Expand All @@ -59,6 +67,7 @@ func (c *DefaultJobUpdatePublisher) Reconnect() error {
return nil
}

// Close closes the messaging client connection.
func (c *DefaultJobUpdatePublisher) Close() {
c.client.Close()
}

0 comments on commit c075428

Please sign in to comment.