main
Bel LaPointe 2025-04-24 21:12:46 -06:00
parent 0e0ade420a
commit a174e6834d
5 changed files with 46 additions and 25 deletions

View File

@ -1,10 +0,0 @@
package cmd
import (
"context"
"io"
)
func cron(ctx context.Context) error {
return io.EOF
}

10
src/cmd/cron/main.go Normal file
View File

@ -0,0 +1,10 @@
package cron
import (
"context"
"io"
)
func Main(ctx context.Context) error {
return io.EOF
}

View File

@ -3,26 +3,47 @@ package cmd
import (
"context"
"fmt"
"log"
"show-rss/src/cmd/cron"
"show-rss/src/cmd/server"
"show-rss/src/pool"
"time"
)
func Main(ctx context.Context) error {
ctx, can := context.WithCancel(ctx)
defer can()
foos := map[string]func() error{
"server": func() error { return server(ctx) },
"cron": func() error { return cron(ctx) },
foos := map[string]func(context.Context) error{
"server": server.Main,
"cron": cron.Main,
}
p := pool.New(len(foos))
defer p.Wait(ctx)
for k, foo := range foos {
foo := foo
if err := p.Go(ctx, k, foo); err != nil {
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)
}
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
}
}

View File

@ -1,10 +0,0 @@
package cmd
import (
"context"
"io"
)
func server(ctx context.Context) error {
return io.EOF
}

10
src/cmd/server/main.go Normal file
View File

@ -0,0 +1,10 @@
package server
import (
"context"
"io"
)
func Main(ctx context.Context) error {
return io.EOF
}