ingredient/info: Add Fetcher concept

This commit is contained in:
Tom Wiesing 2022-10-19 10:50:40 +02:00
parent a6501b42c7
commit 52559e4d68
No known key found for this signature in database
22 changed files with 447 additions and 328 deletions

View file

@ -3,7 +3,6 @@ package info
import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/barrel"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/barrel/drush"
@ -16,7 +15,9 @@ import (
type Info struct {
ingredient.Base
PHP *php.PHP
PHP *php.PHP
Fetchers []ingredient.Fetcher
Barrel *barrel.Barrel
Locker *locker.Locker
Drush *drush.Drush
@ -24,98 +25,41 @@ type Info struct {
Pathbuilder *extras.Pathbuilder
}
// WissKIInfo represents information about this WissKI Instance.
type WissKIInfo struct {
Time time.Time // Time this info was built
// TODO: Use the information struct globally
type WissKIInfo = ingredient.Information
// Generic Information
Slug string // slug
URL string // complete URL, including http(s)
Locked bool // Is this instance currently locked?
// Information about the running instance
Running bool
LastRebuild time.Time
LastUpdate time.Time
LastCron time.Time
// List of backups made
Snapshots []models.Export
// WissKI content information
NoPrefixes bool // TODO: Move this into the database
Prefixes []string // list of prefixes
Pathbuilders map[string]string // all the pathbuilders
}
// Fetch fetches information about this WissKI.
// Information fetches information about this WissKI.
// TODO: Rework this to be able to determine what kind of information is available.
func (wisski *Info) Fetch(quick bool) (info WissKIInfo, err error) {
var group errgroup.Group
wisski.infoQuick(&info, &group)
func (wisski *Info) Information(quick bool) (info WissKIInfo, err error) {
// setup flags
flags := ingredient.FetchFlags{
Quick: quick,
}
if !quick {
// potentially setup a new server
if !flags.Quick {
server, err := wisski.PHP.NewServer()
if err == nil {
defer server.Close()
}
wisski.infoSlow(&info, server, &group)
}
// run all the fetchers!
var group errgroup.Group
for _, fetcher := range wisski.Fetchers {
fetcher := fetcher
group.Go(func() error {
return fetcher.Fetch(flags, &info)
})
}
err = group.Wait()
return
}
func (wisski *Info) infoQuick(info *WissKIInfo, group *errgroup.Group) {
func (wisski *Info) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) error {
info.Time = time.Now().UTC()
info.Slug = wisski.Slug
info.URL = wisski.URL().String()
group.Go(func() (err error) {
info.Running, err = wisski.Barrel.Running()
return
})
group.Go(func() (err error) {
info.Locked = wisski.Locker.Locked()
return
})
group.Go(func() (err error) {
info.LastRebuild, _ = wisski.Barrel.LastRebuild()
return
})
group.Go(func() (err error) {
info.LastUpdate, _ = wisski.Drush.LastUpdate()
return
})
group.Go(func() (err error) {
info.NoPrefixes = wisski.Prefixes.NoPrefix()
return
})
}
func (wisski *Info) infoSlow(info *WissKIInfo, server *php.Server, group *errgroup.Group) {
group.Go(func() (err error) {
info.Prefixes, _ = wisski.Prefixes.All(server)
return nil
})
group.Go(func() (err error) {
info.Snapshots, _ = wisski.Snapshots()
return nil
})
group.Go(func() (err error) {
info.Pathbuilders, _ = wisski.Pathbuilder.GetAll(server)
return nil
})
group.Go(func() (err error) {
info.LastCron, _ = wisski.Drush.LastCron(server)
return
})
return nil
}

View file

@ -0,0 +1,18 @@
package info
import "github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
type SnapshotsFetcher struct {
ingredient.Base
Info *Info
}
func (lbr *SnapshotsFetcher) Fetch(flags ingredient.FetchFlags, info *ingredient.Information) (err error) {
if flags.Quick {
return
}
info.Snapshots, _ = lbr.Snapshots()
return
}