-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quick stab at making watchtower tasks generic
- Loading branch information
Showing
3 changed files
with
138 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package task | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log/slog" | ||
"sync" | ||
|
||
"github.com/rocket-pool/smartnode/v2/rocketpool-daemon/common/state" | ||
) | ||
|
||
var ( | ||
// ErrAlreadyRunning is returned when a background task is kicked off, but it is already in progress. | ||
ErrAlreadyRunning = errors.New("task is already running") | ||
) | ||
|
||
// TaskContext is passed to the Task's Callback function when the invoker wishes the task | ||
// to be kicked off. | ||
// | ||
// Its fields are things that are variable and may change between invokations of a task. | ||
type BackgroundTaskContext struct { | ||
// A context provided by the invoker of this task. | ||
// May be nil, and cancellations should be respected. | ||
Ctx context.Context | ||
// Whether or not the node is on the oDAO at the time the task was invoked | ||
IsOnOdao bool | ||
// A recent network state so each task need not query it redundantly | ||
State *state.NetworkState | ||
} | ||
|
||
type BackgroundTask interface { | ||
// Returns a function to call that starts the task in the background | ||
Run(*BackgroundTaskContext) error | ||
// A function that tasks must call when all async portions are completed | ||
Done() | ||
} | ||
|
||
type LockingBackgroundTask struct { | ||
logger *slog.Logger | ||
description string | ||
run func(*BackgroundTaskContext) error | ||
|
||
lock sync.Mutex | ||
isRunning bool | ||
} | ||
|
||
func NewLockingBackgroundTask(logger *slog.Logger, description string, f func(*BackgroundTaskContext) error) *LockingBackgroundTask { | ||
return &LockingBackgroundTask{ | ||
description: description, | ||
logger: logger, | ||
run: f, | ||
} | ||
} | ||
|
||
func (lbt *LockingBackgroundTask) Run(taskContext *BackgroundTaskContext) error { | ||
lbt.lock.Lock() | ||
defer lbt.lock.Unlock() | ||
|
||
lbt.logger.Info("Starting task", "description", lbt.description) | ||
if lbt.isRunning { | ||
lbt.logger.Info("Task is already running", "description", lbt.description) | ||
return ErrAlreadyRunning | ||
} | ||
|
||
lbt.isRunning = true | ||
err := lbt.run(taskContext) | ||
if err != nil { | ||
lbt.Done() | ||
} | ||
return err | ||
} | ||
|
||
func (lbt *LockingBackgroundTask) Done() { | ||
lbt.lock.Lock() | ||
defer lbt.lock.Unlock() | ||
lbt.isRunning = false | ||
} |
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