-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20274 from ashley-cui/cleanup
Machine: Teardown on init failure
- Loading branch information
Showing
7 changed files
with
276 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package machine | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type CleanupCallback struct { | ||
Funcs []func() error | ||
mu sync.Mutex | ||
} | ||
|
||
func (c *CleanupCallback) CleanIfErr(err *error) { | ||
// Do not remove created files if the init is successful | ||
if *err == nil { | ||
return | ||
} | ||
c.clean() | ||
} | ||
|
||
func (c *CleanupCallback) CleanOnSignal() { | ||
ch := make(chan os.Signal, 1) | ||
signal.Notify(ch, os.Interrupt, syscall.SIGTERM) | ||
|
||
_, ok := <-ch | ||
if !ok { | ||
return | ||
} | ||
|
||
c.clean() | ||
os.Exit(1) | ||
} | ||
|
||
func (c *CleanupCallback) clean() { | ||
c.mu.Lock() | ||
// Claim exclusive usage by copy and resetting to nil | ||
funcs := c.Funcs | ||
c.Funcs = nil | ||
c.mu.Unlock() | ||
|
||
// Already claimed or none set | ||
if funcs == nil { | ||
return | ||
} | ||
|
||
// Cleanup functions can now exclusively be run | ||
for _, cleanfunc := range funcs { | ||
if err := cleanfunc(); err != nil { | ||
logrus.Error(err) | ||
} | ||
} | ||
} | ||
|
||
func InitCleanup() CleanupCallback { | ||
return CleanupCallback{ | ||
Funcs: []func() error{}, | ||
} | ||
} | ||
|
||
func (c *CleanupCallback) Add(anotherfunc func() error) { | ||
c.mu.Lock() | ||
c.Funcs = append(c.Funcs, anotherfunc) | ||
c.mu.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.