Skip to content

Commit

Permalink
v1.1.1: LatestRelease <- Development (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmbek authored Jul 22, 2024
2 parents 84dc149 + 5278373 commit 4418c07
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Graceful Go Service (GGService)
GGService is a Go package designed for building robust and gracefully shutdown services. It provides a framework to easily manage service lifecycle, handle interrupts, and ensure smooth operations even during shutdowns. Ideal for applications requiring reliable service management. Contains graceful shutdowns and custom functions.

go version 1.22+ <br>
[![Go report][go_report_img]][go_report_url]

## Features

- **Graceful Shutdowns:** GgService allows services to handle interrupts and shutdowns gracefully, ensuring minimal disruption.
Expand Down Expand Up @@ -33,9 +36,23 @@ func main() {
}

func loadService() {
//alternative //s := new_ssg.NewService("SSG Service", 5*time.Second)
service := ggservice.New(&ggservice.Service{Name: "My Service", GracefulShutdownTime: 5 * time.Second})
err := service.Start(start, run, forcedTimeoutStop)
// creating new service with name and graceful shutdown time duration
service := ggservice.NewService("SSG Service", 5*time.Second)

// we can use Start, Stop and ForceShutdown on the service

// since Start is a blocking operation we would want to put Start, Stop or ForceShutdown into a goroutine
// if we want to stop the service ourselves, otherwise we would also just wait for interrupt
go func() {
time.Sleep(1 * time.Second)
// we can also use service.Stop or service.ForceShutdown, but notice service.Start is a blocking call
// we can put service.Start in a go routine and use waitgroups etc... your choice.
//service.Stop() // waits till all operations are done (not waiting for graceful timer)
//service.ForceShutdown() // force shutdown immediately
}()

// starting the service (please note you can choose to not implement any of these by using nil instead)
err := service.Start(start, run, forceExit) // this is a blocking call
if err != nil {
log.Fatal(err)
}
Expand All @@ -50,23 +67,30 @@ func start() error {
// run loops from the application is started until it is stopped, terminated or ForceShutdown (please use with time.Sleep in between frames)
func run() error {
fmt.Println("start of work")

// service will run the rest of the task if graceful shutdown timer is > 1
time.Sleep(7 * time.Second)
// service will force shutdown if the rest of the task length
// is > 5 (or whatever graceful shutdown timer is set to)
// time.Sleep(8 * time.Second)

time.Sleep(10 * time.Second) // note: if the graceful timer duration is below amount of work needed to be done, it will forceExit
fmt.Println("end of work")
return nil
}

// forcedStop is being run when the application is trying to force a shutdown (non-gracefully)
func forcedTimeoutStop() {
log.Fatal(errors.New("forced stop: timeout"))
// forceExit is being run when the application is trying to force a shutdown (non-gracefully)
func forceExit() {
log.Fatalln(errors.New("forced stop: timeout"))
}


```
## Contributors
Lars M Bek (https://github.com/lmbek)
## License
[`ggservice`][repos_url] is free and open-source software licensed under the MIT License, created and supported by [Lars M Bek].
<!-- Go links -->
[repos_url]: https://github.com/lmbek/ggservice
[go_version_img]: go1.22+
[go_dev_url]: https://pkg.go.dev/github.com/lmbek/ggservice
[go_report_img]: https://goreportcard.com/badge/github.com/lmbek/ggservice
[go_report_url]: https://goreportcard.com/report/github.com/lmbek/ggservice

0 comments on commit 4418c07

Please sign in to comment.