This commit adds and passes context around to (almost) every function. This allows cancelling (almost) every function call globally.
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package info
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/FAU-CDI/wisski-distillery/internal/status"
|
|
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
|
|
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
|
|
"github.com/FAU-CDI/wisski-distillery/pkg/lazy"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
type Info struct {
|
|
ingredient.Base
|
|
|
|
PHP *php.PHP
|
|
Fetchers []ingredient.WissKIFetcher
|
|
|
|
Analytics *lazy.PoolAnalytics
|
|
}
|
|
|
|
// Information fetches information about this WissKI.
|
|
// TODO: Rework this to be able to determine what kind of information is available.
|
|
func (wisski *Info) Information(ctx context.Context, quick bool) (info status.WissKI, err error) {
|
|
// setup flags
|
|
flags := ingredient.FetcherFlags{
|
|
Quick: quick,
|
|
Context: ctx,
|
|
}
|
|
|
|
// potentially setup a new server
|
|
if !flags.Quick {
|
|
flags.Server = wisski.PHP.NewServer()
|
|
if err == nil {
|
|
defer flags.Server.Close()
|
|
}
|
|
}
|
|
|
|
// 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) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) error {
|
|
info.Time = time.Now().UTC()
|
|
info.Slug = wisski.Slug
|
|
info.URL = wisski.URL().String()
|
|
return nil
|
|
}
|