{"id":17041685,"url":"https://github.com/xlab/closer","last_synced_at":"2025-04-07T15:09:36.830Z","repository":{"id":33773875,"uuid":"37430950","full_name":"xlab/closer","owner":"xlab","description":"Package closer ensures a clean exit for your Go app.","archived":false,"fork":false,"pushed_at":"2022-10-12T12:30:33.000Z","size":13,"stargazers_count":139,"open_issues_count":1,"forks_count":14,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-31T13:17:49.535Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/xlab.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":"2015-06-14T21:52:49.000Z","updated_at":"2024-11-05T04:49:01.000Z","dependencies_parsed_at":"2022-08-27T21:02:22.607Z","dependency_job_id":null,"html_url":"https://github.com/xlab/closer","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/xlab%2Fcloser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Fcloser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Fcloser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xlab%2Fcloser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xlab","download_url":"https://codeload.github.com/xlab/closer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247675607,"owners_count":20977377,"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":[],"created_at":"2024-10-14T09:13:13.866Z","updated_at":"2025-04-07T15:09:36.809Z","avatar_url":"https://github.com/xlab.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Closer [![Circle CI](https://circleci.com/gh/xlab/closer/tree/master.svg?style=svg)](https://circleci.com/gh/xlab/closer/tree/master) [![GoDoc](https://godoc.org/github.com/xlab/closer?status.svg)](https://godoc.org/github.com/xlab/closer)\n\nThe aim of this package is to provide an universal way to catch the event of application’s exit and perform some actions before it’s too late. `closer` doesn’t care about the way application tries to exit, i.e. was that a panic or just a signal from the OS, it calls the provided methods for cleanup and that’s the whole point.\n\n![demo](https://habrastorage.org/getpro/habr/post_images/f2c/025/0cb/f2c0250cbc4e8519d706b5a35374d40d.png)\n\n### Usage\n\nBe careful, this package is using the singleton pattern (like `net/http` does) and doesn't require any initialisation step. However, there’s an option to provide a custom configuration struct.\n\n```go\n// Init allows user to override the defaults (a set of OS signals to watch for, for example).\nfunc Init(cfg Config)\n\n// Close sends a close request.\n// The app will be terminated by OS as soon as the first close request will be handled by closer, this\n// function will return no sooner. The exit code will always be 0 (success).\nfunc Close()\n\n// Bind will register the cleanup function that will be called when closer will get a close request.\n// All the callbacks will be called in the reverse order they were bound, that's similar to how `defer` works.\nfunc Bind(cleanup func())\n\n// Checked runs the target function and checks for panics and errors it may yield. In case of panic or error, closer\n// will terminate the app with an error code, but either case it will call all the bound callbacks beforehand.\n// One can use this instead of `defer` if you need to care about errors and panics that always may happen.\n// This function optionally can emit log messages via standard `log` package.\nfunc Checked(target func() error, logging bool)\n\n// Hold is a helper that may be used to hold the main from returning,\n// until the closer will do a proper exit via `os.Exit`.\nfunc Hold()\n```\n\nThe the usage examples: [example](/cmd/example/main.go), [example-error](/cmd/example-error/main.go) and [example-panic](/cmd/example-panic/main.go).\n\n### Table of exit codes\n\nAll errors and panics will be logged if the logging option of `closer.Checked` was set true, also the exit code (for `os.Exit`) will be determined accordingly:\n\nEvent         | Default exit code\n------------- | -------------\nerror = nil   | 0 (success)\nerror != nil  | 1 (failure)\npanic         | 1 (failure)\n\n### License\n\n[MIT](/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlab%2Fcloser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxlab%2Fcloser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxlab%2Fcloser/lists"}