shuffle
parent
0e0ade420a
commit
a174e6834d
|
|
@ -1,10 +0,0 @@
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
func cron(ctx context.Context) error {
|
|
||||||
return io.EOF
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cron
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Main(ctx context.Context) error {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
|
@ -3,26 +3,47 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"show-rss/src/cmd/cron"
|
||||||
|
"show-rss/src/cmd/server"
|
||||||
"show-rss/src/pool"
|
"show-rss/src/pool"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main(ctx context.Context) error {
|
func Main(ctx context.Context) error {
|
||||||
ctx, can := context.WithCancel(ctx)
|
ctx, can := context.WithCancel(ctx)
|
||||||
defer can()
|
defer can()
|
||||||
|
|
||||||
foos := map[string]func() error{
|
foos := map[string]func(context.Context) error{
|
||||||
"server": func() error { return server(ctx) },
|
"server": server.Main,
|
||||||
"cron": func() error { return cron(ctx) },
|
"cron": cron.Main,
|
||||||
}
|
}
|
||||||
p := pool.New(len(foos))
|
p := pool.New(len(foos))
|
||||||
defer p.Wait(ctx)
|
defer p.Wait(ctx)
|
||||||
|
|
||||||
for k, foo := range foos {
|
for k, foo := range foos {
|
||||||
foo := foo
|
if err := p.Go(ctx, k, runner(ctx, k, foo)); err != nil {
|
||||||
if err := p.Go(ctx, k, foo); err != nil {
|
|
||||||
return fmt.Errorf("failed to go %s: %v", k, err)
|
return fmt.Errorf("failed to go %s: %v", k, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.Wait(ctx)
|
return p.Wait(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
func server(ctx context.Context) error {
|
|
||||||
return io.EOF
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Main(ctx context.Context) error {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue