Skip to content

Commit

Permalink
Build HTTP state update body more like AMQP
Browse files Browse the repository at this point in the history
to hopefully correct inconsistencies!
  • Loading branch information
Dan Buch committed Oct 16, 2017
1 parent 54d1b46 commit bc31e8d
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions http_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ type httpJobPayload struct {
ImageName string `json:"image_name"`
}

type httpJobStateUpdate struct {
CurrentState string `json:"cur"`
NewState string `json:"new"`
Queued *time.Time `json:"queued_at,omitempty"`
Received time.Time `json:"received_at,omitempty"`
Started time.Time `json:"started_at,omitempty"`
Finished time.Time `json:"finished_at,omitempty"`
Meta *httpJobStateUpdateMeta `json:"meta,omitempty"`
}

type httpJobStateUpdateMeta struct {
StateUpdateCount uint `json:"state_update_count,omitempty"`
}

func (j *httpJob) GoString() string {
return fmt.Sprintf("&httpJob{payload: %#v, startAttributes: %#v}",
j.payload, j.startAttributes)
Expand Down Expand Up @@ -174,20 +160,37 @@ func (j *httpJob) Generate(ctx gocontext.Context, job Job) ([]byte, error) {
return script, nil
}

func (j *httpJob) sendStateUpdate(ctx gocontext.Context, curState, newState string) error {
j.stateCount++
payload := &httpJobStateUpdate{
CurrentState: curState,
NewState: newState,
Queued: j.Payload().Job.QueuedAt,
Received: j.received,
Started: j.started,
Finished: j.finished,
Meta: &httpJobStateUpdateMeta{
StateUpdateCount: j.stateCount,
func (j *httpJob) createStateUpdateBody(curState, newState string) map[string]interface{} {
body := map[string]interface{}{
"id": j.Payload().Job.ID,
"state": newState,
"cur": curState,
"new": newState,
"meta": map[string]interface{}{
"state_update_count": j.stateCount,
},
}

if j.Payload().Job.QueuedAt != nil {
body["queued_at"] = j.Payload().Job.QueuedAt.UTC().Format(time.RFC3339)
}
if !j.received.IsZero() {
body["received_at"] = j.received.UTC().Format(time.RFC3339)
}
if !j.started.IsZero() {
body["started_at"] = j.started.UTC().Format(time.RFC3339)
}
if !j.finished.IsZero() {
body["finished_at"] = j.finished.UTC().Format(time.RFC3339)
}

return body
}

func (j *httpJob) sendStateUpdate(ctx gocontext.Context, curState, newState string) error {
j.stateCount++
payload := j.createStateUpdateBody(curState, newState)

encodedPayload, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "error encoding json")
Expand Down

0 comments on commit bc31e8d

Please sign in to comment.