Skip to content

Commit

Permalink
feat: automatically restart miner (#213)
Browse files Browse the repository at this point in the history
Fixes #212
  • Loading branch information
agaffney authored Jul 9, 2024
1 parent 691632a commit 841a298
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package miner

import (
"fmt"
"sync"
"sync/atomic"
"time"
Expand All @@ -26,6 +27,7 @@ import (

const (
hashRateLogInterval = 60 * time.Second
restartTimeout = 2 * time.Minute
)

type Manager struct {
Expand All @@ -38,6 +40,8 @@ type Manager struct {
hashCounter *atomic.Uint64
hashLogTimer *time.Timer
hashLogLastCount uint64
restartTimer *time.Timer
lastBlockData any
}

var globalManager = &Manager{}
Expand All @@ -49,6 +53,7 @@ func (m *Manager) Reset() {
}

func (m *Manager) Stop() {
logger := logging.GetLogger()
m.stopMutex.Lock()
defer m.stopMutex.Unlock()
if !m.started {
Expand All @@ -61,7 +66,17 @@ func (m *Manager) Stop() {
m.workerWaitGroup.Wait()
close(m.resultChan)
m.started = false
logging.GetLogger().Infof("stopped workers")
logger.Infof("stopped workers")
// Start timer to restart miner
m.restartTimer = time.AfterFunc(
restartTimeout,
func() {
logger.Warn(
fmt.Sprintf("restarting miner automatically after %s timeout", restartTimeout),
)
m.Start(m.lastBlockData)
},
)
}

func (m *Manager) Start(blockData any) {
Expand All @@ -70,6 +85,11 @@ func (m *Manager) Start(blockData any) {
if m.started {
return
}
m.lastBlockData = blockData
// Cancel any restart timer
if m.restartTimer != nil {
m.restartTimer.Stop()
}
cfg := config.GetConfig()
logger := logging.GetLogger()
// Start hash rate log timer
Expand Down

0 comments on commit 841a298

Please sign in to comment.