Skip to content

Commit

Permalink
+on start & on stop callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
lnashier committed May 7, 2024
1 parent 61790c7 commit 61dd96c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 30 deletions.
58 changes: 51 additions & 7 deletions boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,68 @@ import (
// syscall.SIGABRT
//
// It exits with a non-zero status code if an error occurs during either the startup or shutdown process.
func Up(s Service) {
func Up(s Service, opt ...BootOpt) {
opts := defaultBootOpts
opts.apply(opt...)

var ch chan error

go func(s Service) {
sig := make(chan os.Signal, 1)
// e.g. kill -SIGQUIT <pid>
// Notify the channel for the specified signals
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGABRT)
// Block until a signal is received
<-sig
ch = make(chan error)
// Attempt to stop the service
err := s.Stop()
// If an error occurs during service stop, exit with a non-zero status code
ch <- s.Stop()
}(s)

// Start the service
err := s.Start()
opts.onStart(err)
if ch != nil {
opts.onStop(<-ch)
}
}

type BootOpt func(*bootOpts)

type bootOpts struct {
onStart func(error)
onStop func(error)
}

func (b *bootOpts) apply(opt ...BootOpt) {
for _, o := range opt {
o(b)
}
}

var defaultBootOpts = bootOpts{
// If an error occurs during startup, exit with a non-zero status code
onStart: func(err error) {
if err != nil {
os.Exit(1)
}
}(s)
},
// If an error occurs during service stop, exit with a non-zero status code
onStop: func(err error) {
if err != nil {
os.Exit(1)
}
},
}

func OnStart(f func(error)) BootOpt {
return func(b *bootOpts) {
b.onStart = f
}
}

// Start the service, and if an error occurs during startup, exit with a non-zero status code
if err := s.Start(); err != nil {
os.Exit(1)
func OnStop(f func(error)) BootOpt {
return func(b *bootOpts) {
b.onStop = f
}
}
46 changes: 27 additions & 19 deletions examples/byofunction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,34 @@ import (
func main() {
ctx, cancel := context.WithCancel(context.Background())

goarc.Up(goarc.ServiceFunc(
// The same function is invoked for both starting and stopping the service.
// It's important to note that Start and Stop represent two separate executions
// of the same provided function.
// Any local variables won't persist across these executions as expected.
func(start bool) error {
if !start {
fmt.Println("Stopping service")
cancel()
return nil
}
goarc.Up(
goarc.ServiceFunc(
// The same function is invoked for both starting and stopping the service.
// It's important to note that Start and Stop represent two separate executions
// of the same provided function.
// Any local variables won't persist across these executions as expected.
func(start bool) error {
if !start {
fmt.Println("Stopping service")
cancel()
return nil
}

fmt.Println("Starting service")
defer fmt.Println("Service done!")
fmt.Println("Starting service")
defer fmt.Println("Service done!")

fmt.Println("Doing some random work")
xtime.SleepWithContext(ctx, time.Duration(10)*time.Second)
fmt.Println("Done with random work")
fmt.Println("Doing some random work for 10 seconds")
xtime.SleepWithContext(ctx, time.Duration(10)*time.Second)
fmt.Println("Done with random work")

return nil
},
))
return nil
},
),
goarc.OnStart(func(err error) {
fmt.Println("On Start Err: ", err)
}),
goarc.OnStop(func(err error) {
fmt.Println("On Stop Err: ", err)
}),
)
}
9 changes: 8 additions & 1 deletion examples/httpservice/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"github.com/lnashier/goarc"
goarchttp "github.com/lnashier/goarc/http"
"github.com/lnashier/goarc/x/health"
Expand All @@ -15,7 +16,7 @@ func main() {
goarchttp.NewService(
goarchttp.ServiceName("httpservice"),
goarchttp.ServicePort(8080),
goarchttp.ServiceShutdownGracetime(time.Duration(10)*time.Second),
goarchttp.ServiceShutdownGracetime(time.Duration(1)*time.Second),
goarchttp.App(
app.App,
func(srv *goarchttp.Service) error {
Expand All @@ -40,5 +41,11 @@ func main() {
},
),
),
goarc.OnStart(func(err error) {
fmt.Println("Start Err: ", err)
}),
goarc.OnStop(func(err error) {
fmt.Println("Stop Err: ", err)
}),
)
}
2 changes: 1 addition & 1 deletion http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Service struct {

func NewService(opt ...ServiceOpt) *Service {
opts := defaultServiceOpts
opts.apply(opt)
opts.apply(opt...)

preempt := negroni.New()

Expand Down
4 changes: 2 additions & 2 deletions http/serviceopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type serviceOpts struct {
apps []func(*Service) error
}

func (s *serviceOpts) apply(opts []ServiceOpt) {
for _, o := range opts {
func (s *serviceOpts) apply(opt ...ServiceOpt) {
for _, o := range opt {
o(s)
}
}
Expand Down

0 comments on commit 61dd96c

Please sign in to comment.