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:
parent
996ecb9f80
commit
3455f491ca
104 changed files with 836 additions and 511 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
|
@ -49,7 +50,7 @@ type BackupDescription struct {
|
|||
}
|
||||
|
||||
// New create a new Backup
|
||||
func (exporter *Exporter) NewBackup(io stream.IOStream, description BackupDescription) (backup Backup) {
|
||||
func (exporter *Exporter) NewBackup(ctx context.Context, io stream.IOStream, description BackupDescription) (backup Backup) {
|
||||
backup.Description = description
|
||||
|
||||
// catch anything critical that happened during the snapshot
|
||||
|
|
@ -60,7 +61,7 @@ func (exporter *Exporter) NewBackup(io stream.IOStream, description BackupDescri
|
|||
// do the create keeping track of time!
|
||||
logging.LogOperation(func() error {
|
||||
backup.StartTime = time.Now().UTC()
|
||||
backup.run(io, exporter)
|
||||
backup.run(ctx, io, exporter)
|
||||
backup.EndTime = time.Now().UTC()
|
||||
|
||||
return nil
|
||||
|
|
@ -69,7 +70,7 @@ func (exporter *Exporter) NewBackup(io stream.IOStream, description BackupDescri
|
|||
return
|
||||
}
|
||||
|
||||
func (backup *Backup) run(ios stream.IOStream, exporter *Exporter) {
|
||||
func (backup *Backup) run(ctx context.Context, ios stream.IOStream, exporter *Exporter) {
|
||||
// create a manifest
|
||||
manifest, done := backup.handleManifest(backup.Description.Dest)
|
||||
defer done()
|
||||
|
|
@ -93,6 +94,7 @@ func (backup *Backup) run(ios stream.IOStream, exporter *Exporter) {
|
|||
Handler: func(bc component.Backupable, index int, writer io.Writer) error {
|
||||
return bc.Backup(
|
||||
component.NewStagingContext(
|
||||
ctx,
|
||||
exporter.Environment,
|
||||
stream.NewIOStream(writer, writer, nil, 0),
|
||||
filepath.Join(backup.Description.Dest, bc.BackupName()),
|
||||
|
|
@ -124,7 +126,7 @@ func (backup *Backup) run(ios stream.IOStream, exporter *Exporter) {
|
|||
}
|
||||
|
||||
// list all instances
|
||||
wissKIs, err := exporter.Instances.All()
|
||||
wissKIs, err := exporter.Instances.All(ctx)
|
||||
if err != nil {
|
||||
backup.InstanceListErr = err
|
||||
return nil
|
||||
|
|
@ -147,7 +149,7 @@ func (backup *Backup) run(ios stream.IOStream, exporter *Exporter) {
|
|||
|
||||
manifest <- dir
|
||||
|
||||
return exporter.NewSnapshot(instance, stream.NewIOStream(writer, writer, nil, 0), SnapshotDescription{
|
||||
return exporter.NewSnapshot(ctx, instance, stream.NewIOStream(writer, writer, nil, 0), SnapshotDescription{
|
||||
Dest: dir,
|
||||
})
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
|
|
@ -19,8 +20,8 @@ func (Bookkeeping) SnapshotNeedsRunning() bool { return false }
|
|||
func (Bookkeeping) SnapshotName() string { return "bookkeeping.txt" }
|
||||
|
||||
// Snapshot creates a snapshot of this instance
|
||||
func (*Bookkeeping) Snapshot(wisski models.Instance, context component.StagingContext) error {
|
||||
return context.AddFile(".", func(file io.Writer) error {
|
||||
func (*Bookkeeping) Snapshot(wisski models.Instance, scontext component.StagingContext) error {
|
||||
return scontext.AddFile(".", func(ctx context.Context, file io.Writer) error {
|
||||
_, err := fmt.Fprintf(file, "%#v\n", wisski)
|
||||
return err
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
|
|
@ -15,13 +16,13 @@ func (*Config) BackupName() string {
|
|||
return "config"
|
||||
}
|
||||
|
||||
func (control *Config) Backup(context component.StagingContext) error {
|
||||
func (control *Config) Backup(scontext component.StagingContext) error {
|
||||
files := control.backupFiles()
|
||||
|
||||
return context.AddDirectory("", func() error {
|
||||
return scontext.AddDirectory("", func(ctx context.Context) error {
|
||||
for _, src := range files {
|
||||
name := filepath.Base(src)
|
||||
if err := context.CopyFile(name, src); err != nil {
|
||||
if err := scontext.CopyFile(name, src); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
|
|
@ -17,15 +18,15 @@ func (Pathbuilders) SnapshotNeedsRunning() bool { return true }
|
|||
|
||||
func (Pathbuilders) SnapshotName() string { return "pathbuilders" }
|
||||
|
||||
func (pbs *Pathbuilders) Snapshot(wisski models.Instance, context component.StagingContext) error {
|
||||
return context.AddDirectory(".", func() error {
|
||||
builders, err := pbs.Instances.Instance(wisski).Pathbuilder().GetAll(nil)
|
||||
func (pbs *Pathbuilders) Snapshot(wisski models.Instance, scontext component.StagingContext) error {
|
||||
return scontext.AddDirectory(".", func(ctx context.Context) error {
|
||||
builders, err := pbs.Instances.Instance(ctx, wisski).Pathbuilder().GetAll(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for name, bytes := range builders {
|
||||
if err := context.AddFile(name+".xml", func(file io.Writer) error {
|
||||
if err := scontext.AddFile(name+".xml", func(ctx context.Context, file io.Writer) error {
|
||||
_, err := file.Write([]byte(bytes))
|
||||
return err
|
||||
}); err != nil {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ type export interface {
|
|||
|
||||
// MakeExport performs an export task as described by flags.
|
||||
// Output is directed to the provided io.
|
||||
func (exporter *Exporter) MakeExport(io stream.IOStream, task ExportTask) (err error) {
|
||||
func (exporter *Exporter) MakeExport(ctx context.Context, io stream.IOStream, task ExportTask) (err error) {
|
||||
// extract parameters
|
||||
Title := "Backup"
|
||||
Slug := ""
|
||||
|
|
@ -95,11 +96,11 @@ func (exporter *Exporter) MakeExport(io stream.IOStream, task ExportTask) (err e
|
|||
var sl export
|
||||
if task.Instance == nil {
|
||||
task.BackupDescription.Dest = stagingDir
|
||||
backup := exporter.NewBackup(io, task.BackupDescription)
|
||||
backup := exporter.NewBackup(ctx, io, task.BackupDescription)
|
||||
sl = &backup
|
||||
} else {
|
||||
task.SnapshotDescription.Dest = stagingDir
|
||||
snapshot := exporter.NewSnapshot(task.Instance, io, task.SnapshotDescription)
|
||||
snapshot := exporter.NewSnapshot(ctx, task.Instance, io, task.SnapshotDescription)
|
||||
sl = &snapshot
|
||||
}
|
||||
|
||||
|
|
@ -131,7 +132,7 @@ func (exporter *Exporter) MakeExport(io stream.IOStream, task ExportTask) (err e
|
|||
// write out the log entry
|
||||
entry.Path = stagingDir
|
||||
entry.Packed = false
|
||||
exporter.ExporterLogger.Add(entry)
|
||||
exporter.ExporterLogger.Add(ctx, entry)
|
||||
|
||||
io.Printf("Wrote %s\n", stagingDir)
|
||||
return nil
|
||||
|
|
@ -159,7 +160,7 @@ func (exporter *Exporter) MakeExport(io stream.IOStream, task ExportTask) (err e
|
|||
logging.LogMessage(io, "Writing Log Entry")
|
||||
entry.Path = archivePath
|
||||
entry.Packed = true
|
||||
exporter.ExporterLogger.Add(entry)
|
||||
exporter.ExporterLogger.Add(ctx, entry)
|
||||
|
||||
// and we're done!
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
|
|
@ -19,8 +21,8 @@ type Logger struct {
|
|||
// For retrieves (and prunes) the ExportLog.
|
||||
// Slug determines if entries for Backups (empty slug)
|
||||
// or a specific Instance (non-empty slug) are returned.
|
||||
func (log *Logger) For(slug string) (exports []models.Export, err error) {
|
||||
exports, err = log.Log()
|
||||
func (log *Logger) For(ctx context.Context, slug string) (exports []models.Export, err error) {
|
||||
exports, err = log.Log(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -31,9 +33,9 @@ func (log *Logger) For(slug string) (exports []models.Export, err error) {
|
|||
}
|
||||
|
||||
// Log retrieves (and prunes) all entries in the snapshot log.
|
||||
func (log *Logger) Log() ([]models.Export, error) {
|
||||
func (log *Logger) Log(ctx context.Context) ([]models.Export, error) {
|
||||
// query the table!
|
||||
table, err := log.SQL.QueryTable(false, models.ExportTable)
|
||||
table, err := log.SQL.QueryTable(ctx, false, models.ExportTable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -63,9 +65,9 @@ func (log *Logger) Log() ([]models.Export, error) {
|
|||
}
|
||||
|
||||
// AddToExportLog adds the provided export to the log.
|
||||
func (log *Logger) Add(export models.Export) error {
|
||||
func (log *Logger) Add(ctx context.Context, export models.Export) error {
|
||||
// find the table
|
||||
table, err := log.SQL.QueryTable(false, models.ExportTable)
|
||||
table, err := log.SQL.QueryTable(ctx, false, models.ExportTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -79,7 +81,7 @@ func (log *Logger) Add(export models.Export) error {
|
|||
}
|
||||
|
||||
// Fetch writes the SnapshotLog into the given observation
|
||||
func (logger *Logger) Fetch(flags component.FetcherFlags, target *status.Distillery) (err error) {
|
||||
target.Backups, err = logger.For("")
|
||||
func (logger *Logger) Fetch(ctx context.Context, flags component.FetcherFlags, target *status.Distillery) (err error) {
|
||||
target.Backups, err = logger.For(ctx, "")
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
|
|
@ -14,7 +15,7 @@ func (exporter *Exporter) ShouldPrune(modtime time.Time) bool {
|
|||
}
|
||||
|
||||
// Prune prunes all old exports
|
||||
func (exporter *Exporter) PruneExports(io stream.IOStream) error {
|
||||
func (exporter *Exporter) PruneExports(ctx context.Context, io stream.IOStream) error {
|
||||
sPath := exporter.ArchivePath()
|
||||
|
||||
// list all the files
|
||||
|
|
@ -50,6 +51,6 @@ func (exporter *Exporter) PruneExports(io stream.IOStream) error {
|
|||
}
|
||||
|
||||
// prune the snapshot log!
|
||||
_, err = exporter.ExporterLogger.Log()
|
||||
_, err = exporter.ExporterLogger.Log(ctx)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
|
@ -44,10 +45,10 @@ type Snapshot struct {
|
|||
}
|
||||
|
||||
// Snapshot creates a new snapshot of this instance into dest
|
||||
func (snapshots *Exporter) NewSnapshot(instance *wisski.WissKI, io stream.IOStream, desc SnapshotDescription) (snapshot Snapshot) {
|
||||
func (snapshots *Exporter) NewSnapshot(ctx context.Context, instance *wisski.WissKI, io stream.IOStream, desc SnapshotDescription) (snapshot Snapshot) {
|
||||
|
||||
logging.LogMessage(io, "Locking instance")
|
||||
if !instance.Locker().TryLock() {
|
||||
if !instance.Locker().TryLock(ctx) {
|
||||
err := locker.Locked
|
||||
io.EPrintln(err)
|
||||
logging.LogMessage(io, "Aborting snapshot creation")
|
||||
|
|
@ -58,7 +59,7 @@ func (snapshots *Exporter) NewSnapshot(instance *wisski.WissKI, io stream.IOStre
|
|||
}
|
||||
defer func() {
|
||||
logging.LogMessage(io, "Unlocking instance")
|
||||
instance.Locker().Unlock()
|
||||
instance.Locker().Unlock(ctx)
|
||||
}()
|
||||
|
||||
// setup the snapshot
|
||||
|
|
@ -74,8 +75,8 @@ func (snapshots *Exporter) NewSnapshot(instance *wisski.WissKI, io stream.IOStre
|
|||
logging.LogOperation(func() error {
|
||||
snapshot.StartTime = time.Now().UTC()
|
||||
|
||||
snapshot.ErrWhitebox = snapshot.makeParts(io, snapshots, instance, false)
|
||||
snapshot.ErrBlackbox = snapshot.makeParts(io, snapshots, instance, true)
|
||||
snapshot.ErrWhitebox = snapshot.makeParts(ctx, io, snapshots, instance, false)
|
||||
snapshot.ErrBlackbox = snapshot.makeParts(ctx, io, snapshots, instance, true)
|
||||
|
||||
snapshot.EndTime = time.Now().UTC()
|
||||
return nil
|
||||
|
|
@ -85,16 +86,16 @@ func (snapshots *Exporter) NewSnapshot(instance *wisski.WissKI, io stream.IOStre
|
|||
return
|
||||
}
|
||||
|
||||
func (snapshot *Snapshot) makeParts(ios stream.IOStream, snapshots *Exporter, instance *wisski.WissKI, needsRunning bool) map[string]error {
|
||||
func (snapshot *Snapshot) makeParts(ctx context.Context, ios stream.IOStream, snapshots *Exporter, instance *wisski.WissKI, needsRunning bool) map[string]error {
|
||||
if !needsRunning && !snapshot.Description.Keepalive {
|
||||
stack := instance.Barrel().Stack()
|
||||
|
||||
logging.LogMessage(ios, "Stopping instance")
|
||||
snapshot.ErrStop = stack.Down(ios)
|
||||
snapshot.ErrStop = stack.Down(ctx, ios)
|
||||
|
||||
defer func() {
|
||||
logging.LogMessage(ios, "Starting instance")
|
||||
snapshot.ErrStart = stack.Up(ios)
|
||||
snapshot.ErrStart = stack.Up(ctx, ios)
|
||||
}()
|
||||
}
|
||||
// handle writing the manifest!
|
||||
|
|
@ -123,6 +124,7 @@ func (snapshot *Snapshot) makeParts(ios stream.IOStream, snapshots *Exporter, in
|
|||
return sc.Snapshot(
|
||||
instance.Instance,
|
||||
component.NewStagingContext(
|
||||
ctx,
|
||||
snapshots.Environment,
|
||||
stream.NewIOStream(writer, writer, nil, 0),
|
||||
filepath.Join(snapshot.Description.Dest, sc.SnapshotName()),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue