{"id":20157573,"url":"https://github.com/g4s8/go-lifecycle","last_synced_at":"2025-03-03T02:14:31.582Z","repository":{"id":87504861,"uuid":"606836915","full_name":"g4s8/go-lifecycle","owner":"g4s8","description":"Simple and flexible Go lifecycle manager library.","archived":false,"fork":false,"pushed_at":"2024-04-19T12:42:01.000Z","size":84,"stargazers_count":1,"open_issues_count":8,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-13T12:50:36.908Z","etag":null,"topics":["go","golang","graceful-shutdown","healthcheck","lifecycle","lifecycle-management"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/g4s8.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-02-26T17:58:54.000Z","updated_at":"2023-10-10T10:05:57.000Z","dependencies_parsed_at":"2023-10-16T22:17:09.820Z","dependency_job_id":"4ebec66f-fe94-4540-a982-65a42db87ff7","html_url":"https://github.com/g4s8/go-lifecycle","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g4s8%2Fgo-lifecycle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g4s8%2Fgo-lifecycle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g4s8%2Fgo-lifecycle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g4s8%2Fgo-lifecycle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/g4s8","download_url":"https://codeload.github.com/g4s8/go-lifecycle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241596276,"owners_count":19988044,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","graceful-shutdown","healthcheck","lifecycle","lifecycle-management"],"created_at":"2024-11-13T23:46:55.629Z","updated_at":"2025-03-03T02:14:31.562Z","avatar_url":"https://github.com/g4s8.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Introducing a powerful and versatile Go library for managing the lifecycle of your applications!\n\nIt is designed to be simple, yet flexible, giving you complete control over configuring and running multiple services\nwithin a single application. With built-in tools for monitoring service state, implementing graceful shutdowns,\nand conducting health checks, this library provides everything you need to ensure your application runs smoothly.\n\n[![CI](https://github.com/g4s8/go-lifecycle/actions/workflows/go.yml/badge.svg)](https://github.com/g4s8/go-lifecycle/actions/workflows/go.yml)\n[![GoDoc](https://godoc.org/github.com/g4s8/go-lifecycle?status.svg)](https://godoc.org/github.com/g4s8/go-lifecycle)\n\n## Key Features\n\n  - Simple and flexible Go lifecycle manager library.\n  - Components to manage application lifecycle, handle system signals for graceful shutdowns, and built-in health-check service.\n  - Adapters for common scenarios, such as HTTP servers.\n  - No reflection or magic behind the scenes, just static types.\n  - Flexible configuration options for both the lifecycle manager and each individual service.\n\n## How go-lifecycle Compares to Others\n\nWhile our library is similar in some aspects to the `github.com/uber-go/fx` library, there are several main differences:\n  - This library is not a DI container, only a lifecycle manager.\n  - This library uses only static types, without any reflection or magic behind the scenes.\n  - This library offers more flexibility in configuration options.\n\n## Get Started\n\nSee [Usage](#Usage) examples below and\ncheck out the `/examples` directory to see it in action!\n\n## Install\n\nInstall using `go get`:\n```bash\ngo get github.com/g4s8/go-lifecycle@latest\n```\n\n## Usage\n\n### Register startup and shutdown hooks:\n\nIn this example:\n - `lifecycle.New` - creates new lifecycle manager.\n - `RegisterStartupHook` - registers func as startup hook and calls it during startup phase.\n - `RegisterShutdownHook` - registers func as shutdown hook and calls it on shutdown.\n - `lf.Start` - starts all services and call startup hooks.\n\n```go\nlf := lifecycle.New(lifecycle.DefaultConfig)\nlf.RegisterStartupHook(\"my-service\", func(ctx context.Context, errCh chan\u003c- error) error {\n        fmt.Println(\"My service started\")\n        return nil\n})\nlf.RegisterShutdownHook(\"cleanup-hook\", func(ctx context.Context) error {\n        return os.Remove(\"/tmp/cache-file\")\n})\nlf.Start()\n```\n\n### Shutdown on SIGTERM:\n```go\nsig := lifecycle.NewSignalHandler(lf, nil)\nsig.Start(lifecycle.DefaultShutdownConfig) // start it async\nsig.Wait() // wait for signal\n```\n\nThe lifecycle will shut down, on SIGTERM or interrup signals.\n\n### Configure lifecycle behavior\n\n```go\nlf := lifecycle.New(lifecycle.Config{\n        StartupTimeout: 3*time.Second, // 3 seconds timout for startup, otherwise fail\n        ShutdownTimeout: 4*time.Second, // 4 seconds timeout for shutdown, otherwise fail\n        StartStrategy: StartStrategyFailFast | StartStrategyRollbackOnError\n})\n```\nThere are 3 startup strategies for lifecycle manager:\n - `StartStrategyFailFast` - lifecycle manager will fail on `Start` if any service fails.\n - `StartStrategyStartAll` - lifecycle manager will continue on `Start` if one or many services fails.\n - `StartStrategyRollbackOnError` - lifecycle manager will stop all started services in case of start error.\n\n### Report service errors\n\nThe channnel `errCh` should be used to report service errors, depens on configuration the server could be restarted:\n```go\nlf.RegisterStartupHook(\"web-service\", func(ctx context.Context, errCh chan\u003c- error) error {\n\tln, err := net.Listen(\"tcp\", \":80\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"listen tcp: %w\", err)\n\t}\n\tgo func() {\n\t\tif err := srv.Serve(ln); err != nil {\n\t\t\tif err != http.ErrServerClosed {\n\t\t\t\terrCh \u003c- errors.Wrap(err, \"serve\")\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}()\n\treturn nil\n})\n```\n\n### Configure service\n```go\nlf.RegisterService(types.ServiceConfig{\n        Name:         \"my-service\",\n        StartupHook:  service.Start,\n        ShutdownHook: service.Stop,\n        RestartPolicy: types.ServiceRestartPolicy{\n                RestartOnFailure: true,\n                RestartCount:     3,\n                RestartDelay:     time.Millisecond * 200,\n        },\n})\n```\nHere:\n - `Name` is a name of the service\n - `StartupHook` - method to call on startup\n - `ShutdownHook` - method to call on shutdown\n - `RestartPolicy` - service restart rules:\n  - `RestartOnFailure` - restart service in case of runtime errors reported to `errCh`.\n  - `RestartCount` - number of restart attemts until lifecycle manager gives up.\n  - `RestartDelay` - min time interval between restart attempts.\n\n### Run HTTP web service\n\nThe package `github.com/g4s8/go-lifecycle/pkg/adaptors` contains adaptors for common services, e.g. web server:\n```go\nweb := http.NewServeMux()\nweb.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n        w.WriteHeader(http.StatusOK)\n        w.Write([]byte(\"OK\"))\n})\nsvc := adaptors.NewHTTPService(\u0026http.Server{\n        Addr:    \":8080\",\n        Handler: web,\n})\nsvc.RegisterLifecycle(\"web\", lf)\n```\n\n### Add healthcheck service\n\nThe package `github.com/g4s8/go-lifecycle/pkg/health` has healthcheck http service\nwhich can monitor service statuses in lifecycle manager:\n```go\nhs := health.NewService(\":9999\", lf)\nhs.RegisterLifecycle(lf)\n```\n\n## Contributing\n\n - Commit changes and create pull request.\n - Describe your changes in PR title and description.\n - Use (Conventional Commits)[https://www.conventionalcommits.org/en/v1.0.0/] specification.\n - Ensure CI checks are green.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fg4s8%2Fgo-lifecycle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fg4s8%2Fgo-lifecycle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fg4s8%2Fgo-lifecycle/lists"}