Skip to content

Commit

Permalink
refactor: optimize code for mrunners
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeguo committed Jul 30, 2024
1 parent 422913b commit 1fd02e5
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 47 deletions.
33 changes: 33 additions & 0 deletions internal/mrunners/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mrunners

import (
"github.com/yankeguo/minit/internal/mexec"
"github.com/yankeguo/minit/internal/mlog"
"github.com/yankeguo/minit/internal/munit"
)

type RunnerOptions struct {
Unit munit.Unit
Exec mexec.Manager
Logger mlog.ProcLogger
}

func (ro RunnerOptions) Print(message string) {
ro.Logger.Print("minit: " + ro.Unit.Kind + "/" + ro.Unit.Name + ": " + message)
}

func (ro RunnerOptions) Printf(layout string, items ...any) {
ro.Logger.Printf("minit: "+ro.Unit.Kind+"/"+ro.Unit.Name+": "+layout, items...)
}

func (ro RunnerOptions) Error(message string) {
ro.Logger.Error("minit: " + ro.Unit.Kind + "/" + ro.Unit.Name + ": " + message)
}

func (ro RunnerOptions) Errorf(layout string, items ...any) {
ro.Logger.Errorf("minit: "+ro.Unit.Kind+"/"+ro.Unit.Name+": "+layout, items...)
}

func (ro RunnerOptions) Execute() error {
return ro.Exec.Execute(ro.Unit.ExecuteOptions(ro.Logger))
}
23 changes: 5 additions & 18 deletions internal/mrunners/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"context"
"errors"
"sync"

"github.com/yankeguo/minit/internal/mexec"
"github.com/yankeguo/minit/internal/mlog"
"github.com/yankeguo/minit/internal/munit"
)

// RunnerAction is the interface of runner action
type RunnerAction interface {
Do(ctx context.Context) (err error)
}

// Runner is the struct of runner
type Runner struct {
Order int
Long bool
Expand All @@ -25,29 +23,18 @@ var (
factoriesLock sync.Locker = &sync.Mutex{}
)

type RunnerOptions struct {
Unit munit.Unit
Exec mexec.Manager
Logger mlog.ProcLogger
}

func (ro RunnerOptions) Print(message string) {
ro.Logger.Print("minit: " + ro.Unit.Kind + "/" + ro.Unit.Name + ": " + message)
}

func (ro RunnerOptions) Error(message string) {
ro.Logger.Error("minit: " + ro.Unit.Kind + "/" + ro.Unit.Name + ": " + message)
}

// RunnerFactory is the type of runner factory
type RunnerFactory = func(opts RunnerOptions) (Runner, error)

// Register registers a runner factory
func Register(name string, factory RunnerFactory) {
factoriesLock.Lock()
defer factoriesLock.Unlock()

factories[name] = factory
}

// Create creates a runner from options
func Create(opts RunnerOptions) (Runner, error) {
factoriesLock.Lock()
defer factoriesLock.Unlock()
Expand Down
10 changes: 5 additions & 5 deletions internal/mrunners/runner_cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ func init() {

runner.Order = 30
runner.Long = true
runner.Action = &runnerCron{RunnerOptions: opts}
runner.Action = &actionCron{RunnerOptions: opts}
return
})
}

type runnerCron struct {
type actionCron struct {
RunnerOptions
}

func (r *runnerCron) Do(ctx context.Context) (err error) {
func (r *actionCron) Do(ctx context.Context) (err error) {
r.Print("controller started")
defer r.Print("controller exited")

if r.Unit.Immediate {
if err = r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
if err = r.Execute(); err != nil {
r.Error("failed executing: " + err.Error())
if r.Unit.Critical {
return
Expand All @@ -55,7 +55,7 @@ func (r *runnerCron) Do(ctx context.Context) (err error) {

if _, err = cr.AddFunc(r.Unit.Cron, func() {
r.Print("triggered")
if err := r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
if err := r.Execute(); err != nil {
r.Error("failed executing: " + err.Error())
if chErr != nil {
select {
Expand Down
8 changes: 4 additions & 4 deletions internal/mrunners/runner_cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func TestRunnerCron(t *testing.T) {

buf := &bytes.Buffer{}

r := &runnerCron{
r := &actionCron{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindCron,
Name: "test",
Cron: "@every 1s",
Cron: "@every 2s",
Immediate: true,
Command: []string{
"echo", "hhhlll",
Expand Down Expand Up @@ -56,15 +56,15 @@ func TestRunnerCron(t *testing.T) {

wg.Wait()

require.Equal(t, 3, strings.Count(buf.String(), "hhhlll\n"))
require.Equal(t, 2, strings.Count(buf.String(), "hhhlll\n"))
}

func TestRunnerCronCritical(t *testing.T) {
exem := mexec.NewManager()

buf := &bytes.Buffer{}

r := &runnerCron{
r := &actionCron{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindCron,
Expand Down
8 changes: 4 additions & 4 deletions internal/mrunners/runner_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ func init() {

runner.Order = 40
runner.Long = true
runner.Action = &runnerDaemon{RunnerOptions: opts}
runner.Action = &actionDaemon{RunnerOptions: opts}
return
})
}

type runnerDaemon struct {
type actionDaemon struct {
RunnerOptions
}

func (r *runnerDaemon) Do(ctx context.Context) (err error) {
func (r *actionDaemon) Do(ctx context.Context) (err error) {
r.Print("controller started")
defer r.Print("controller exited")

Expand All @@ -34,7 +34,7 @@ forLoop:
break forLoop
}

if err = r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
if err = r.Execute(); err != nil {
r.Error("failed executing:" + err.Error())

if r.Unit.Critical {
Expand Down
4 changes: 2 additions & 2 deletions internal/mrunners/runner_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRunnerDaemon(t *testing.T) {

buf := &bytes.Buffer{}

r := &runnerDaemon{
r := &actionDaemon{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindDaemon,
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestRunnerDaemonCritical(t *testing.T) {

buf := &bytes.Buffer{}

r := &runnerDaemon{
r := &actionDaemon{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindDaemon,
Expand Down
10 changes: 5 additions & 5 deletions internal/mrunners/runner_once.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ func init() {
}

runner.Order = 20
runner.Action = &runnerOnce{RunnerOptions: opts}
runner.Action = &actionOnce{RunnerOptions: opts}
return
})
}

type runnerOnce struct {
type actionOnce struct {
RunnerOptions
}

func (r *runnerOnce) Do(ctx context.Context) (err error) {
func (r *actionOnce) Do(ctx context.Context) (err error) {
r.Print("controller started")
defer r.Print("controller exited")

if r.Unit.Blocking != nil && !*r.Unit.Blocking {
go func() {
if err := r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
if err := r.Execute(); err != nil {
r.Error("failed executing (non-blocking): " + err.Error())
return
}
}()
return
}

if err = r.Exec.Execute(r.Unit.ExecuteOptions(r.Logger)); err != nil {
if err = r.Execute(); err != nil {
r.Error("failed executing: " + err.Error())
if r.Unit.Critical {
return
Expand Down
6 changes: 3 additions & 3 deletions internal/mrunners/runner_once_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestRunnerOnce(t *testing.T) {

buf := &bytes.Buffer{}

r := &runnerOnce{
r := &actionOnce{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindOnce,
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestRunnerOnceCritical(t *testing.T) {

buf := &bytes.Buffer{}

r := &runnerOnce{
r := &actionOnce{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindOnce,
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestRunnerOnceCriticalNonBlocking(t *testing.T) {

blocking := false

r := &runnerOnce{
r := &actionOnce{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindOnce,
Expand Down
11 changes: 6 additions & 5 deletions internal/mrunners/runner_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ func init() {
}

runner.Order = 10
runner.Action = &runnerRender{RunnerOptions: opts}
runner.Action = &actionRender{RunnerOptions: opts}
return
})
}

type runnerRender struct {
type actionRender struct {
RunnerOptions
}

func (r *runnerRender) doFile(ctx context.Context, name string, env map[string]string) (err error) {
func (r *actionRender) doFile(ctx context.Context, name string, env map[string]string) (err error) {
var buf []byte
if buf, err = os.ReadFile(name); err != nil {
err = fmt.Errorf("failed reading %s: %s", name, err.Error())
Expand All @@ -52,7 +52,7 @@ func (r *runnerRender) doFile(ctx context.Context, name string, env map[string]s
return
}

func (r *runnerRender) Do(ctx context.Context) (err error) {
func (r *actionRender) Do(ctx context.Context) (err error) {
r.Print("controller started")
defer r.Print("controller exited")

Expand All @@ -69,7 +69,7 @@ func (r *runnerRender) Do(ctx context.Context) (err error) {
var names []string

if names, err = filepath.Glob(filePattern); err != nil {
r.Error(fmt.Sprintf("failed globbing: %s: %s", filePattern, err.Error()))
r.Errorf("failed globbing: %s: %s", filePattern, err.Error())

if r.Unit.Critical {
return
Expand Down Expand Up @@ -104,6 +104,7 @@ func (r *runnerRender) Do(ctx context.Context) (err error) {
return
}

// sanitizeLines removes empty lines and trailing spaces
func sanitizeLines(s []byte) []byte {
var out [][]byte
for _, line := range bytes.Split(s, []byte("\n")) {
Expand Down
2 changes: 1 addition & 1 deletion internal/mrunners/runner_render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestRunnerRender(t *testing.T) {

buf := &bytes.Buffer{}

r := &runnerRender{
r := &actionRender{
RunnerOptions: RunnerOptions{
Unit: munit.Unit{
Kind: munit.KindRender,
Expand Down

0 comments on commit 1fd02e5

Please sign in to comment.