118 lines
2.1 KiB
Go
118 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
inass "show-rss/src/asses"
|
|
"show-rss/src/cmd/asses"
|
|
"show-rss/src/cmd/fetch"
|
|
"show-rss/src/cmd/server"
|
|
"show-rss/src/pool"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func Main(ctx context.Context, args []string) error {
|
|
ctx, can := context.WithCancel(ctx)
|
|
defer can()
|
|
|
|
ctx, can, flags, err := Config(ctx, args)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to inject: %w", err)
|
|
}
|
|
defer can()
|
|
|
|
switch flags.Entrypoint {
|
|
case Defacto:
|
|
foos := map[string]func(context.Context) error{
|
|
"server": server.Main,
|
|
"fetch": fetch.Main,
|
|
"asses": asses.Main,
|
|
}
|
|
p := pool.New(len(foos))
|
|
defer p.Wait(ctx)
|
|
|
|
for k, foo := range foos {
|
|
if err := p.Go(ctx, k, runner(ctx, k, foo)); err != nil {
|
|
return fmt.Errorf("failed to go %s: %v", k, err)
|
|
}
|
|
}
|
|
|
|
return p.Wait(ctx)
|
|
case DeportAss:
|
|
for _, pos := range flags.Pos {
|
|
if err := inass.Entrypoint(ctx, pos); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
case BestAssToSRT:
|
|
for _, pos := range flags.Pos {
|
|
if err := inass.BestAssToSRT(ctx, pos); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
default:
|
|
panic(flags.Entrypoint.String())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Entrypoint int
|
|
|
|
const (
|
|
Defacto Entrypoint = iota
|
|
DeportAss
|
|
BestAssToSRT
|
|
)
|
|
|
|
func (e *Entrypoint) Set(s string) error {
|
|
switch s {
|
|
case Defacto.String():
|
|
*e = Defacto
|
|
case DeportAss.String():
|
|
*e = DeportAss
|
|
case BestAssToSRT.String():
|
|
*e = BestAssToSRT
|
|
default:
|
|
return fmt.Errorf("%s nin (%s)", s, strings.Join([]string{
|
|
Defacto.String(),
|
|
DeportAss.String(),
|
|
BestAssToSRT.String(),
|
|
}, ", "))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e Entrypoint) String() string {
|
|
switch e {
|
|
case Defacto:
|
|
return ""
|
|
case DeportAss:
|
|
return "deport-ass"
|
|
case BestAssToSRT:
|
|
return "best-ass-to-srt"
|
|
}
|
|
panic("cannot serialize entrypoint")
|
|
}
|
|
|
|
func runner(ctx context.Context, k string, foo func(context.Context) error) func() error {
|
|
return func() error {
|
|
var err error
|
|
for {
|
|
err = foo(ctx)
|
|
if ctx.Err() == nil {
|
|
log.Printf("%s failed; restarting: %v", k, err)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
break
|
|
case <-time.After(time.Second):
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
}
|