Skip to content

Commit

Permalink
fix: send on closed channel
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 14, 2022
1 parent 08a001f commit 9e5ef97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
19 changes: 7 additions & 12 deletions internal/op/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,14 @@ func EnableStorage(ctx context.Context, id uint) error {
if !storage.Disabled {
return errors.Errorf("this storage have enabled")
}
err = LoadStorage(ctx, *storage)
if err != nil {
return errors.WithMessage(err, "failed load storage")
}
// re-get storage from db, because it maybe hava updated
storage, err = db.GetStorageById(id)
if err != nil {
return errors.WithMessage(err, "failed re-get storage again")
}
storage.Disabled = false
err = db.UpdateStorage(storage)
if err != nil {
return errors.WithMessage(err, "failed update storage in db, but have load in memory. you can try restart")
return errors.WithMessage(err, "failed update storage in db")
}
err = LoadStorage(ctx, *storage)
if err != nil {
return errors.WithMessage(err, "failed load storage")
}
return nil
}
Expand All @@ -128,12 +123,12 @@ func DisableStorage(ctx context.Context, id uint) error {
return errors.Wrapf(err, "failed drop storage")
}
// delete the storage in the memory
storagesMap.Delete(storage.MountPath)
storage.Disabled = true
err = db.UpdateStorage(storage)
if err != nil {
return errors.WithMessage(err, "failed update storage in db, but have drop in memory. you can try restart")
return errors.WithMessage(err, "failed update storage in db")
}
storagesMap.Delete(storage.MountPath)
return nil
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (c *Cron) Do(f func()) {
}

func (c *Cron) Stop() {
c.ch <- struct{}{}
close(c.ch)
select {
case _, _ = <-c.ch:
default:
c.ch <- struct{}{}
close(c.ch)
}
}
3 changes: 2 additions & 1 deletion pkg/cron/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func TestCron(t *testing.T) {
c.Do(func() {
t.Logf("cron log")
})
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 3)
c.Stop()
c.Stop()
}

0 comments on commit 9e5ef97

Please sign in to comment.