Skip to content

Commit

Permalink
Fix new jobs showing as failed in dashboard in western timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
yvanoers committed Aug 2, 2019
1 parent 4c5fbf4 commit 294a09d
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 253 deletions.
288 changes: 144 additions & 144 deletions dkron/assets/assets_vfsdata.go

Large diffs are not rendered by default.

41 changes: 25 additions & 16 deletions dkron/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/dgraph-io/badger"
"github.com/distribworks/dkron/cron"
"github.com/distribworks/dkron/ntime"
"github.com/distribworks/dkron/proto"
"github.com/golang/protobuf/ptypes"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -74,10 +75,10 @@ type Job struct {
ErrorCount int `json:"error_count"`

// Last time this job executed succesful.
LastSuccess time.Time `json:"last_success"`
LastSuccess ntime.NullableTime `json:"last_success"`

// Last time this job failed.
LastError time.Time `json:"last_error"`
LastError ntime.NullableTime `json:"last_error"`

// Is this job disabled?
Disabled bool `json:"disabled"`
Expand Down Expand Up @@ -123,10 +124,8 @@ type Job struct {

// NewJobFromProto create a new Job from a PB Job struct
func NewJobFromProto(in *proto.Job) *Job {
lastSuccess, _ := ptypes.Timestamp(in.GetLastSuccess())
lastError, _ := ptypes.Timestamp(in.GetLastError())
next, _ := ptypes.Timestamp(in.GetNext())
return &Job{
job := &Job{
Name: in.Name,
DisplayName: in.Displayname,
Timezone: in.Timezone,
Expand All @@ -145,16 +144,33 @@ func NewJobFromProto(in *proto.Job) *Job {
ExecutorConfig: in.ExecutorConfig,
Status: in.Status,
Metadata: in.Metadata,
LastSuccess: lastSuccess,
LastError: lastError,
Next: next,
}
if in.GetLastSuccess().GetHasValue() {
t, _ := ptypes.Timestamp(in.GetLastSuccess().GetTime())
job.LastSuccess.Set(t)
}
if in.GetLastError().GetHasValue() {
t, _ := ptypes.Timestamp(in.GetLastError().GetTime())
job.LastError.Set(t)
}
return job
}

// ToProto return the corresponding representation of this Job in proto struct
func (j *Job) ToProto() *proto.Job {
lastSuccess, _ := ptypes.TimestampProto(j.LastSuccess)
lastError, _ := ptypes.TimestampProto(j.LastError)
lastSuccess := &proto.Job_NullableTime{
HasValue: j.LastSuccess.HasValue(),
}
if j.LastSuccess.HasValue() {
lastSuccess.Time, _ = ptypes.TimestampProto(j.LastSuccess.Get())
}
lastError := &proto.Job_NullableTime{
HasValue: j.LastError.HasValue(),
}
if j.LastError.HasValue() {
lastError.Time, _ = ptypes.TimestampProto(j.LastError.Get())
}
next, _ := ptypes.TimestampProto(j.Next)
return &proto.Job{
Name: j.Name,
Expand Down Expand Up @@ -281,13 +297,6 @@ func (j *Job) GetParent() (*Job, error) {
return parentJob, nil
}

func (j *Job) getLast() time.Time {
if j.LastSuccess.After(j.LastError) {
return j.LastSuccess
}
return j.LastError
}

// GetNext returns the job's next schedule from now
func (j *Job) GetNext() (time.Time, error) {
if j.Schedule != "" {
Expand Down
6 changes: 4 additions & 2 deletions dkron/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,12 @@ func (s *Store) SetExecutionDone(execution *Execution) (bool, error) {
}

if pbe.Success {
pbj.LastSuccess = pbe.FinishedAt
pbj.LastSuccess.HasValue = true
pbj.LastSuccess.Time = pbe.FinishedAt
pbj.SuccessCount++
} else {
pbj.LastError = pbe.FinishedAt
pbj.LastError.HasValue = true
pbj.LastError.Time = pbe.FinishedAt
pbj.ErrorCount++
}

Expand Down
16 changes: 8 additions & 8 deletions dkron/templates/templates_vfsdata.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c h1:964Od4U6p2jUkFxvCydnIczKteheJEzHRToSGK3Bnlw=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 h1:zLTLjkaOFEFIOxY5BWLFLwh+cL8vOBW4XJ2aqLE/Tf0=
Expand Down
61 changes: 61 additions & 0 deletions ntime/nullabletime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ntime

import (
"encoding/json"
"time"
)

// NullableTime represents a Time from the time package, but can have no value set
type NullableTime struct {
hasValue bool
time time.Time
}

// HasValue returns whether a time has been set
func (t *NullableTime) HasValue() bool {
return t.hasValue
}

// Set a time. This is the equivalent of an assignment
func (t *NullableTime) Set(newTime time.Time) {
t.hasValue = true
t.time = newTime
}

// Unset sets the value to nothing. This is the equivalent of assigning nil.
func (t *NullableTime) Unset() {
t.hasValue = false
}

// Get returns the contained value. Panics if no value is set.
func (t *NullableTime) Get() time.Time {
if t.hasValue {
return t.time
}
panic("runtime error: attempt to get value of NullableTime set to nil.")
}

// After determines whether one time is after another.
func (t *NullableTime) After(u NullableTime) bool {
// nil after u? No value is ever after anything else
if !t.hasValue {
return false
}

// t after nil? Always.
if !u.hasValue {
return true
}

// t after u?
return t.time.After(u.time)
}

// MarshalJSON serializer this struct to JSON
// Implements json.Marshaler interface
func (t *NullableTime) MarshalJSON() ([]byte, error) {
if t.hasValue {
return json.Marshal(t.time)
}
return json.Marshal(nil)
}
Loading

0 comments on commit 294a09d

Please sign in to comment.