{"id":15699954,"url":"https://github.com/tomwright/grace","last_synced_at":"2025-05-12T13:00:32.383Z","repository":{"id":57571626,"uuid":"333538680","full_name":"TomWright/grace","owner":"TomWright","description":"Easy graceful shutdowns in your go applications.","archived":false,"fork":false,"pushed_at":"2021-04-14T11:07:41.000Z","size":21,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-31T22:11:34.506Z","etag":null,"topics":["graceful","graceful-shutdown","gracefully","runner","runners","shutdown"],"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/TomWright.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}},"created_at":"2021-01-27T19:36:15.000Z","updated_at":"2024-01-26T18:20:35.000Z","dependencies_parsed_at":"2022-09-26T20:20:27.122Z","dependency_job_id":null,"html_url":"https://github.com/TomWright/grace","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fgrace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fgrace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fgrace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomWright%2Fgrace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomWright","download_url":"https://codeload.github.com/TomWright/grace/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253745051,"owners_count":21957316,"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":["graceful","graceful-shutdown","gracefully","runner","runners","shutdown"],"created_at":"2024-10-03T19:43:01.494Z","updated_at":"2025-05-12T13:00:32.353Z","avatar_url":"https://github.com/TomWright.png","language":"Go","readme":"[![Go Report Card](https://goreportcard.com/badge/github.com/TomWright/grace)](https://goreportcard.com/report/github.com/TomWright/grace)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/tomwright/grace)](https://pkg.go.dev/github.com/tomwright/grace)\n![Test](https://github.com/TomWright/grace/workflows/Test/badge.svg)\n[![codecov](https://codecov.io/gh/TomWright/grace/branch/main/graph/badge.svg)](https://codecov.io/gh/TomWright/grace)\n![GitHub License](https://img.shields.io/github/license/TomWright/grace)\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/TomWright/grace?label=latest%20release)\n\n# grace\n\nEasy graceful shutdowns in your go applications.\n\n## Table of Contents\n\n- [Usage](#usage)\n- [Quickstart](#quickstart)\n- [Runners](#runners)\n  - [Pre-made Runners](#pre-made-runners)\n- [Triggering Shutdowns](#triggering-shutdowns)\n\n## Usage\n\nFirst import the module:\n\n```bash\ngo get github.com/tomwright/grace\n```\n\n## Quickstart\n\n```go\npackage main\n\nimport (\n    \"context\"\n    \"github.com/tomwright/grace\"\n    \"time\"\n)\n\ntype myRunner struct {\n}\n\nfunc (mr *myRunner) Run(ctx context.Context) error {\n    // Block until we are told to shutdown or until all work is done.\n    \u003c-ctx.Done()\n    return nil\n}\n\nfunc main() {\n    // Initialise grace.\n    g := grace.Init(context.Background())\n\n    // The following runners are equivalent.\n    runnerA := \u0026myRunner{}\n    runnerB := grace.RunnerFunc(func(ctx context.Context) error {\n        // Block until we are told to shutdown or until all work is done.\n        \u003c-ctx.Done()\n        return nil\n    })\n\t\n    // Run both runners.\n    g.Run(runnerA)\n    g.Run(runnerB)\n\n    go func() {\n        // Manually shutdown after 2.5 seconds.\n        // Alternatively leave this out and rely on os signals to shutdown.\n        \u003c-time.After(time.Millisecond * 2500)\n        g.Shutdown()\n    }()\n\n    // Optionally wait for the shutdown signal to be sent.\n    \u003c-g.Context().Done()\n\n    // Wait for all Runners to end.\n    g.Wait()\n}\n```\n\n## Options\n\nGrace allows you to modify the way it is initialised using `InitOption`s.\n\n```go\ng := grace.Init(\n  context.Background(),\n  grace.InitOptionFn(func(g *grace.Grace) {\n    g.LogFn = log.Printf\n    g.ErrHandler = func(err error) bool {\n    \tg.LogFn(\"something terrible went wrong: %s\", err.Error())\n    \treturn true\n    }\n  })\n)\n```\n\n\n## Runners\n\nGrace implements logic to execute and gracefully shutdown any number of `Runner`s.\n\nEach runner must react when the given contexts `Done` channel is closed.\n\nA runner must implement the `Runner` interface. It can do this by implementing the `Run(context.Context) error` func, or by passing a func to `RunnerFunc()`.\n\n### Pre-made Runners\n\nGrace doesn't provide any runners in this module in order to reduce forced dependencies... instead please import the required modules below.\n\nImplemented your own runner that you want to share? Raise a PR!\n\n- [HTTP Server](https://github.com/TomWright/gracehttpserverrunner)\n- [GRPC Server](https://github.com/TomWright/gracegrpcserverrunner)\n- [Sarama Consumer + Producer](https://github.com/TomWright/gracesarama)\n\n## Triggering Shutdowns\n\nShutdowns can be triggered in a number of ways:\n\n1. `SIGKILL` will immediately terminate the application using `os.Exit(1)`.\n2. `SIGINT` or `SIGTERM` will trigger a graceful shutdown. Sending 2 or more of these will immediately exit the application using `os.Exit(1)`.\n3. Calling `Shutdown()` on a grace instance will start a graceful shutdown.\n4. Cancelling the context you gave to grace will start a graceful shutdown.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwright%2Fgrace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomwright%2Fgrace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomwright%2Fgrace/lists"}