This commit is contained in:
parent
ed2e15a3dd
commit
fe02e0ac79
BIN
assets/demo.gif
Normal file
BIN
assets/demo.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 85 KiB |
@ -1,8 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.front.kjuulh.io/kjuulh/orbis/internal/app"
|
"git.front.kjuulh.io/kjuulh/orbis/internal/app"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
@ -17,7 +21,23 @@ func main() {
|
|||||||
|
|
||||||
app := app.NewApp()
|
app := app.NewApp()
|
||||||
|
|
||||||
if err := newRoot(app).Execute(); err != nil {
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-stop
|
||||||
|
|
||||||
|
app.Logger().Info("stop signal received: shutting down orbis")
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
// Start timer for hard stop
|
||||||
|
time.Sleep(time.Second * 10)
|
||||||
|
fmt.Println("orbis failed to stop in time, forced to hard cancel")
|
||||||
|
os.Exit(1)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := newRoot(app).ExecuteContext(ctx); err != nil {
|
||||||
fmt.Printf("%s\n", err)
|
fmt.Printf("%s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -18,10 +18,20 @@ func NewScheduler(logger *slog.Logger) *Scheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Scheduler) Execute(ctx context.Context) error {
|
func (s *Scheduler) Execute(ctx context.Context) error {
|
||||||
|
acquiredLeader, err := s.acquireLeader(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !acquiredLeader {
|
||||||
|
s.logger.Info("gracefully shutting down non-elected scheduler")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
s.logger.Info("gracefully shutting down scheduler")
|
s.logger.Info("gracefully shutting down elected scheduler")
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
if err := s.process(ctx); err != nil {
|
if err := s.process(ctx); err != nil {
|
||||||
@ -31,11 +41,26 @@ func (s *Scheduler) Execute(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Scheduler) acquireLeader(ctx context.Context) (bool, error) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return false, nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Attempt to acquire leader
|
||||||
|
//
|
||||||
|
return true, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Scheduler) process(ctx context.Context) error {
|
func (s *Scheduler) process(ctx context.Context) error {
|
||||||
s.logger.Debug("scheduler processing items")
|
s.logger.Debug("scheduler processing items")
|
||||||
|
|
||||||
// FIXME: simulate work
|
// FIXME: simulate work
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user