Skip to content

Commit

Permalink
update start restart struct
Browse files Browse the repository at this point in the history
  • Loading branch information
justlorain committed Apr 5, 2024
1 parent b634498 commit a95524a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
8 changes: 4 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
"github.com/B1NARY-GR0UP/openalysis/storage"
)

func Start(ctx context.Context) {
cron.Start(ctx)
func Start(ctx context.Context) error {
return cron.Start(ctx)
}

func Restart(ctx context.Context) {
cron.Restart(ctx)
func Restart(ctx context.Context) error {
return cron.Restart(ctx)
}

func ReadInConfig(path string) error {
Expand Down
4 changes: 3 additions & 1 deletion cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ e.g. oa restart -c "cron-spec" path2config.yaml`,
if err := api.Init(); err != nil {
cobra.CheckErr(err)
}
api.Restart(context.Background())
if err := api.Restart(context.Background()); err != nil {
cobra.CheckErr(err)
}
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ e.g. oa start -t "your-token" path2config.yaml`,
if err := api.Init(); err != nil {
cobra.CheckErr(err)
}
api.Start(context.Background())
if err := api.Start(context.Background()); err != nil {
cobra.CheckErr(err)
}
},
}

Expand Down
47 changes: 25 additions & 22 deletions cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ var ErrReachedRetryTimes = errors.New("error reached retry times")

var GlobalCleaner *cleaner.Cleaner

func Start(ctx context.Context) {
func Start(ctx context.Context) error {
slog.Info("openalysis service started")

// 1. add strategies error
// 2. init task error
// 3. cron add func error
errC := make(chan error, 3)
errC := make(chan error, 1)

GlobalCleaner = cleaner.New()
if err := GlobalCleaner.AddStrategies(config.GlobalConfig.Cleaner...); err != nil {
errC <- err
return err
}

c := cron.New()
if err := AddCronTask(ctx, c, errC); err != nil {
return err
}

slog.Info("init task starts now")
Expand All @@ -61,47 +63,49 @@ func Start(ctx context.Context) {
// if init failed, stop service
if err := InitTask(ctx, tx); err != nil {
tx.Rollback()
errC <- err
} else {
tx.Commit()
slog.Info("init task completed", "time", time.Since(startInit).String())
return err
}
tx.Commit()
slog.Info("init task completed", "time", time.Since(startInit).String())

c := cron.New()
StartCron(ctx, c, errC)
c.Start()
defer c.Stop()

if err := util.WaitSignal(errC); err != nil {
slog.Error("receive error signal", "err", err.Error())
}

slog.Info("openalysis service stopped")
return nil
}

func Restart(ctx context.Context) {
func Restart(ctx context.Context) error {
slog.Info("openalysis service restarted")

// 1. add strategies error
// 2. cron add func error
errC := make(chan error, 2)
errC := make(chan error, 1)

GlobalCleaner = cleaner.New()
if err := GlobalCleaner.AddStrategies(config.GlobalConfig.Cleaner...); err != nil {
errC <- err
return err
}

c := cron.New()
StartCron(ctx, c, errC)
if err := AddCronTask(ctx, c, errC); err != nil {
return err
}

c.Start()
defer c.Stop()

if err := util.WaitSignal(errC); err != nil {
slog.Error("receive error signal", "err", err.Error())
}

slog.Info("openalysis service stopped")
return nil
}

func StartCron(ctx context.Context, c *cron.Cron, errC chan error) {
func AddCronTask(ctx context.Context, c *cron.Cron, errC chan error) error {
if _, err := c.AddFunc(config.GlobalConfig.Backend.Cron, func() {
slog.Info("update task starts now")
startUpdate := time.Now()
Expand All @@ -124,10 +128,9 @@ func StartCron(ctx context.Context, c *cron.Cron, errC chan error) {
}
slog.Info("update task completed", "time", time.Since(startUpdate).String())
}); err != nil {
slog.Error("error add cron func", "err", err)
errC <- err
return err
}
c.Start()
return nil
}

type Count struct {
Expand Down

0 comments on commit a95524a

Please sign in to comment.