From 7e85ffffc32834b70c952e37e3142bb3009dc8b5 Mon Sep 17 00:00:00 2001 From: Lars Morten Bek Date: Mon, 22 Jul 2024 13:47:41 +0200 Subject: [PATCH 1/2] Updated README.md with example --- README.md | 53 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index be56e07..84b23c5 100644 --- a/README.md +++ b/README.md @@ -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+
+[![Go report][go_report_img]][go_report_url] + ## Features - **Graceful Shutdowns:** GgService allows services to handle interrupts and shutdowns gracefully, ensuring minimal disruption. @@ -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) } @@ -50,23 +67,33 @@ 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][repo_license_url], created and supported by [Lars M Bek]. + + + +[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 +[repo_license_img]: https://goreportcard.com/badge/github.com/lmbek/ggservice +[repo_license_url]: https://goreportcard.com/report/github.com/lmbek/ggservice + From 5278373dd2ca295da8e836627abc6d426440a419 Mon Sep 17 00:00:00 2001 From: Lars Morten Bek Date: Mon, 22 Jul 2024 13:49:54 +0200 Subject: [PATCH 2/2] Updated README.md --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 84b23c5..6db3e00 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Lars M Bek (https://github.com/lmbek) ## License -[`ggservice`][repos_url] is free and open-source software licensed under the [MIT License][repo_license_url], created and supported by [Lars M Bek]. +[`ggservice`][repos_url] is free and open-source software licensed under the MIT License, created and supported by [Lars M Bek]. @@ -93,7 +93,4 @@ Lars M Bek (https://github.com/lmbek) [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 -[repo_license_img]: https://goreportcard.com/badge/github.com/lmbek/ggservice -[repo_license_url]: https://goreportcard.com/report/github.com/lmbek/ggservice - +[go_report_url]: https://goreportcard.com/report/github.com/lmbek/ggservice \ No newline at end of file