Skip to content

Commit

Permalink
Merge pull request #278 from s29papi/feature/fixed-temp-logging
Browse files Browse the repository at this point in the history
Feature/fixed temp logging
  • Loading branch information
tremarkley authored Dec 3, 2024
2 parents 7f98695 + f267db4 commit 4f95706
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Anvil struct {

stopped atomic.Bool
stoppedCh chan struct{}

cleanupTasks []func()
}

func New(log log.Logger, closeApp context.CancelCauseFunc, cfg *config.ChainConfig) *Anvil {
Expand Down Expand Up @@ -97,7 +99,6 @@ func (a *Anvil) Start(ctx context.Context) error {

if len(a.cfg.GenesisJSON) > 0 && a.cfg.ForkConfig == nil {
tempFile, err := os.CreateTemp("", "genesis-*.json")
defer a.removeFile(tempFile)

if err != nil {
return fmt.Errorf("error creating temporary genesis file: %w", err)
Expand All @@ -106,6 +107,10 @@ func (a *Anvil) Start(ctx context.Context) error {
return fmt.Errorf("error writing to genesis file: %w", err)
}
args = append(args, "--init", tempFile.Name())

a.registerCleanupTask(func() {
a.removeFile(tempFile)
})
}
if a.cfg.ForkConfig != nil {
args = append(args,
Expand Down Expand Up @@ -136,9 +141,10 @@ func (a *Anvil) Start(ctx context.Context) error {
}

logFile = tempLogFile
// Clean up the temp log file
// TODO (https://github.com/ethereum-optimism/supersim/issues/205) This results in the temp file being deleted right away instead of after shutdown.
defer a.removeFile(logFile)

a.registerCleanupTask(func() {
a.removeFile(logFile)
})
} else {
// Expand the path to the log file
absFilePath, err := filepath.Abs(fmt.Sprintf("%s/anvil-%d.log", a.cfg.LogsDirectory, a.cfg.ChainID))
Expand Down Expand Up @@ -241,6 +247,7 @@ func (a *Anvil) Stop(_ context.Context) error {
}

a.resourceCancel()
a.executeCleanup()
<-a.stoppedCh
return nil
}
Expand Down Expand Up @@ -339,3 +346,13 @@ func (a *Anvil) removeFile(file *os.File) {
a.log.Warn("failed to remove temp genesis file", "file.path", file.Name(), "err", err)
}
}

func (a *Anvil) executeCleanup() {
for _, task := range a.cleanupTasks {
task()
}
}

func (a *Anvil) registerCleanupTask(task func()) {
a.cleanupTasks = append(a.cleanupTasks, task)
}

0 comments on commit 4f95706

Please sign in to comment.