Add context

This commit adds and passes context around to (almost) every function.
This allows cancelling (almost) every function call globally.
This commit is contained in:
Tom Wiesing 2022-11-28 13:30:08 +01:00
parent 996ecb9f80
commit 3455f491ca
No known key found for this signature in database
104 changed files with 836 additions and 511 deletions

View file

@ -1,6 +1,8 @@
package locker
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/tkw1536/goprogram/exit"
@ -17,8 +19,8 @@ var Locked = exit.Error{
}
// TryLock attemps to lock this WissKI and returns if it suceeded
func (lock *Locker) TryLock() bool {
table, err := lock.Malt.SQL.QueryTable(true, models.LockTable)
func (lock *Locker) TryLock(ctx context.Context) bool {
table, err := lock.Malt.SQL.QueryTable(ctx, true, models.LockTable)
if err != nil {
return false
}
@ -29,8 +31,8 @@ func (lock *Locker) TryLock() bool {
// TryUnlock attempts to unlock this WissKI and reports if it succeeded.
// An unlock can only
func (lock *Locker) TryUnlock() bool {
table, err := lock.Malt.SQL.QueryTable(true, models.LockTable)
func (lock *Locker) TryUnlock(ctx context.Context) bool {
table, err := lock.Malt.SQL.QueryTable(ctx, true, models.LockTable)
if err != nil {
return false
}
@ -39,6 +41,6 @@ func (lock *Locker) TryUnlock() bool {
}
// Unlock unlocks this WissKI, ignoring any error.
func (lock *Locker) Unlock() {
lock.TryUnlock()
func (lock *Locker) Unlock(ctx context.Context) {
lock.TryUnlock(ctx)
}

View file

@ -1,14 +1,17 @@
package locker
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
)
// Locked checks if this WissKI is currently locked.
func (lock *Locker) Locked() (locked bool) {
table, err := lock.SQL.QueryTable(true, models.LockTable)
// If an error occurs, the instance is considered not locked.
func (lock *Locker) Locked(ctx context.Context) (locked bool) {
table, err := lock.SQL.QueryTable(ctx, true, models.LockTable)
if err != nil {
return false
}
@ -19,6 +22,6 @@ func (lock *Locker) Locked() (locked bool) {
}
func (locker *Locker) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.Locked = locker.Locked()
info.Locked = locker.Locked(flags.Context)
return
}