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 barrel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
|
|
@ -15,40 +16,40 @@ import (
|
|||
// Build builds or rebuilds the barel connected to this instance.
|
||||
//
|
||||
// It also logs the current time into the metadata belonging to this instance.
|
||||
func (barrel *Barrel) Build(stream stream.IOStream, start bool) error {
|
||||
if !barrel.Locker.TryLock() {
|
||||
func (barrel *Barrel) Build(ctx context.Context, stream stream.IOStream, start bool) error {
|
||||
if !barrel.Locker.TryLock(ctx) {
|
||||
err := locker.Locked
|
||||
return err
|
||||
}
|
||||
defer barrel.Locker.Unlock()
|
||||
defer barrel.Locker.Unlock(ctx)
|
||||
|
||||
stack := barrel.Stack()
|
||||
|
||||
var context component.InstallationContext
|
||||
|
||||
{
|
||||
err := stack.Install(stream, context)
|
||||
err := stack.Install(ctx, stream, context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
err := stack.Update(stream, start)
|
||||
err := stack.Update(ctx, stream, start)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// store the current last rebuild
|
||||
return barrel.setLastRebuild()
|
||||
return barrel.setLastRebuild(ctx)
|
||||
}
|
||||
|
||||
// TODO: Move this to time.Time
|
||||
var lastRebuild = mstore.For[int64]("lastRebuild")
|
||||
|
||||
func (barrel Barrel) LastRebuild() (t time.Time, err error) {
|
||||
epoch, err := lastRebuild.Get(barrel.MStore)
|
||||
func (barrel Barrel) LastRebuild(ctx context.Context) (t time.Time, err error) {
|
||||
epoch, err := lastRebuild.Get(ctx, barrel.MStore)
|
||||
if err == meta.ErrMetadatumNotSet {
|
||||
return t, nil
|
||||
}
|
||||
|
|
@ -60,8 +61,8 @@ func (barrel Barrel) LastRebuild() (t time.Time, err error) {
|
|||
return time.Unix(epoch, 0), nil
|
||||
}
|
||||
|
||||
func (barrel *Barrel) setLastRebuild() error {
|
||||
return lastRebuild.Set(barrel.MStore, time.Now().Unix())
|
||||
func (barrel *Barrel) setLastRebuild(ctx context.Context) error {
|
||||
return lastRebuild.Set(ctx, barrel.MStore, time.Now().Unix())
|
||||
}
|
||||
|
||||
type LastRebuildFetcher struct {
|
||||
|
|
@ -70,7 +71,7 @@ type LastRebuildFetcher struct {
|
|||
Barrel *Barrel
|
||||
}
|
||||
|
||||
func (lbr *LastRebuildFetcher) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
|
||||
info.LastRebuild, _ = lbr.Barrel.LastRebuild()
|
||||
func (lbr *LastRebuildFetcher) Fetch(ctx context.Context, flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
|
||||
info.LastRebuild, _ = lbr.Barrel.LastRebuild(ctx)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package drush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
|
||||
|
|
@ -15,8 +16,8 @@ var errCronFailed = exit.Error{
|
|||
ExitCode: exit.ExitGeneric,
|
||||
}
|
||||
|
||||
func (drush *Drush) Cron(io stream.IOStream) error {
|
||||
code, err := drush.Barrel.Shell(io, "/runtime/cron.sh")
|
||||
func (drush *Drush) Cron(ctx context.Context, io stream.IOStream) error {
|
||||
code, err := drush.Barrel.Shell(ctx, io, "/runtime/cron.sh")
|
||||
if err != nil {
|
||||
io.EPrintln(err)
|
||||
}
|
||||
|
|
@ -29,9 +30,9 @@ func (drush *Drush) Cron(io stream.IOStream) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (drush *Drush) LastCron(server *phpx.Server) (t time.Time, err error) {
|
||||
func (drush *Drush) LastCron(ctx context.Context, server *phpx.Server) (t time.Time, err error) {
|
||||
var timestamp int64
|
||||
err = drush.PHP.EvalCode(server, ×tamp, `$val = \Drupal::state()->get('system.cron_last'); return $val; `)
|
||||
err = drush.PHP.EvalCode(ctx, server, ×tamp, `$val = \Drupal::state()->get('system.cron_last'); return $val; `)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -49,6 +50,6 @@ func (lbr *LastCronFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Wi
|
|||
return
|
||||
}
|
||||
|
||||
info.LastRebuild, _ = lbr.Drush.LastCron(flags.Server)
|
||||
info.LastRebuild, _ = lbr.Drush.LastCron(flags.Context, flags.Server)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package drush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta"
|
||||
|
|
@ -18,8 +19,8 @@ var errBlindUpdateFailed = exit.Error{
|
|||
}
|
||||
|
||||
// Update performs a blind drush update
|
||||
func (drush *Drush) Update(io stream.IOStream) error {
|
||||
code, err := drush.Barrel.Shell(io, "/runtime/blind_update.sh")
|
||||
func (drush *Drush) Update(ctx context.Context, io stream.IOStream) error {
|
||||
code, err := drush.Barrel.Shell(ctx, io, "/runtime/blind_update.sh")
|
||||
if err != nil {
|
||||
return errBlindUpdateFailed.WithMessageF(drush.Slug, environment.ExecCommandError)
|
||||
}
|
||||
|
|
@ -27,13 +28,13 @@ func (drush *Drush) Update(io stream.IOStream) error {
|
|||
return errBlindUpdateFailed.WithMessageF(drush.Slug, code)
|
||||
}
|
||||
|
||||
return drush.setLastUpdate()
|
||||
return drush.setLastUpdate(ctx)
|
||||
}
|
||||
|
||||
const lastUpdate = mstore.For[int64]("lastUpdate")
|
||||
|
||||
func (drush *Drush) LastUpdate() (t time.Time, err error) {
|
||||
epoch, err := lastUpdate.Get(drush.MStore)
|
||||
func (drush *Drush) LastUpdate(ctx context.Context) (t time.Time, err error) {
|
||||
epoch, err := lastUpdate.Get(ctx, drush.MStore)
|
||||
if err == meta.ErrMetadatumNotSet {
|
||||
return t, nil
|
||||
}
|
||||
|
|
@ -45,8 +46,8 @@ func (drush *Drush) LastUpdate() (t time.Time, err error) {
|
|||
return time.Unix(epoch, 0), nil
|
||||
}
|
||||
|
||||
func (drush *Drush) setLastUpdate() error {
|
||||
return lastUpdate.Set(drush.MStore, time.Now().Unix())
|
||||
func (drush *Drush) setLastUpdate(ctx context.Context) error {
|
||||
return lastUpdate.Set(ctx, drush.MStore, time.Now().Unix())
|
||||
}
|
||||
|
||||
type LastUpdateFetcher struct {
|
||||
|
|
@ -56,6 +57,6 @@ type LastUpdateFetcher struct {
|
|||
}
|
||||
|
||||
func (lbr *LastUpdateFetcher) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
|
||||
info.LastUpdate, err = lbr.Drush.LastUpdate()
|
||||
info.LastUpdate, err = lbr.Drush.LastUpdate(flags.Context)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package provisioner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
|
|
@ -19,10 +20,10 @@ type Provisioner struct {
|
|||
}
|
||||
|
||||
// Provision provisions an instance, assuming that the required databases already exist.
|
||||
func (provision *Provisioner) Provision(io stream.IOStream) error {
|
||||
func (provision *Provisioner) Provision(ctx context.Context, io stream.IOStream) error {
|
||||
|
||||
// build the container
|
||||
if err := provision.Barrel.Build(io, false); err != nil {
|
||||
if err := provision.Barrel.Build(ctx, io, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +54,7 @@ func (provision *Provisioner) Provision(io stream.IOStream) error {
|
|||
// TODO: Move the provision script into the control plane!
|
||||
provisionScript := "sudo PATH=$PATH -u www-data /bin/bash /provision_container.sh " + strings.Join(provisionParams, " ")
|
||||
|
||||
code, err := provision.Barrel.Stack().Run(io, true, "barrel", "/bin/bash", "-c", provisionScript)
|
||||
code, err := provision.Barrel.Stack().Run(ctx, io, true, "barrel", "/bin/bash", "-c", provisionScript)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,16 @@
|
|||
package barrel
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
// Running checks if this WissKI is currently running.
|
||||
func (barrel *Barrel) Running() (bool, error) {
|
||||
ps, err := barrel.Stack().Ps(stream.FromNil())
|
||||
func (barrel *Barrel) Running(ctx context.Context) (bool, error) {
|
||||
ps, err := barrel.Stack().Ps(ctx, stream.FromNil())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
@ -22,6 +24,6 @@ type RunningFetcher struct {
|
|||
}
|
||||
|
||||
func (rf *RunningFetcher) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
|
||||
info.Running, err = rf.Barrel.Running()
|
||||
info.Running, err = rf.Barrel.Running(flags.Context)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
package barrel
|
||||
|
||||
import "github.com/tkw1536/goprogram/stream"
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
// Shell executes a shell command inside the instance.
|
||||
func (barrel *Barrel) Shell(io stream.IOStream, argv ...string) (int, error) {
|
||||
return barrel.Stack().Exec(io, "barrel", "/bin/sh", append([]string{"/user_shell.sh"}, argv...)...)
|
||||
func (barrel *Barrel) Shell(ctx context.Context, io stream.IOStream, argv ...string) (int, error) {
|
||||
return barrel.Stack().Exec(ctx, io, "barrel", "/bin/sh", append([]string{"/user_shell.sh"}, argv...)...)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue