Skip to content

Commit

Permalink
Update child jobs when parent is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
yvanoers committed Jul 9, 2019
1 parent c6da283 commit 4974894
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 14 additions & 0 deletions dkron/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ func (s *Store) GetJob(name string, options *JobOptions) (*Job, error) {
return job, err
}

// DeleteJob deletes the given job from the store, along with
// all its executions and references to it.
func (s *Store) DeleteJob(name string) (*Job, error) {
var job *Job
err := s.db.Update(func(txn *badger.Txn) error {
Expand All @@ -383,6 +385,18 @@ func (s *Store) DeleteJob(name string) (*Job, error) {
}
}

// Remove the parent from any children
for _, djn := range j.DependentJobs {
child, err := s.GetJob(djn, nil)
if err != nil {
return err
}
child.ParentJob = ""
if err := s.SetJob(child, false); err != nil {
return err
}
}

if err := s.DeleteExecutions(name); err != nil {
if err != nil {
return err
Expand Down
14 changes: 13 additions & 1 deletion dkron/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ func TestStore_JobBecomesIndependentJob(t *testing.T) {
assert.Equal(t, 0, len(parent.DependentJobs))
}

func TestStore_ChildIsUpdatedAfterDeletingParentJob(t *testing.T) {
s, dir := setupStore(t)
defer cleanupStore(dir, s)

storeJob(t, s, "parent1")
storeChildJob(t, s, "child1", "parent1")
deleteJob(t, s, "parent1")
orphan := loadJob(t, s, "child1")

assert.Equal(t, "", orphan.ParentJob)
}

func TestStore_GetLastExecutionGroup(t *testing.T) {
// This can not use time.Now() because that will include monotonic information
// that will cause the unmarshalled execution to differ from our generated version
Expand Down Expand Up @@ -319,7 +331,7 @@ func setupStore(t *testing.T) (*Store, string) {
dir, err := ioutil.TempDir("", "dkron-test")
require.NoError(t, err)

a := NewAgent(nil, nil)
a := NewAgent(nil)
s, err := NewStore(a, dir)
require.NoError(t, err)
a.Store = s
Expand Down

0 comments on commit 4974894

Please sign in to comment.