{"id":13786652,"url":"https://github.com/ztrue/shutdown","last_synced_at":"2025-08-18T19:32:41.049Z","repository":{"id":33387711,"uuid":"158010290","full_name":"ztrue/shutdown","owner":"ztrue","description":"Golang app shutdown hooks.","archived":false,"fork":false,"pushed_at":"2022-01-15T22:23:00.000Z","size":13,"stargazers_count":59,"open_issues_count":0,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-17T22:36:26.742Z","etag":null,"topics":["go","golang","hooks","shutdown","signal"],"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/ztrue.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-11-17T17:56:03.000Z","updated_at":"2024-11-08T11:22:23.000Z","dependencies_parsed_at":"2022-08-07T21:01:24.941Z","dependency_job_id":null,"html_url":"https://github.com/ztrue/shutdown","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/ztrue%2Fshutdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Fshutdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Fshutdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ztrue%2Fshutdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ztrue","download_url":"https://codeload.github.com/ztrue/shutdown/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230268679,"owners_count":18199808,"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","hooks","shutdown","signal"],"created_at":"2024-08-03T19:01:27.168Z","updated_at":"2024-12-18T12:19:44.615Z","avatar_url":"https://github.com/ztrue.png","language":"Go","readme":"# Golang App Shutdown Hooks\n\n[![GoDoc](https://godoc.org/github.com/ztrue/shutdown?status.svg)](https://godoc.org/github.com/ztrue/shutdown)\n[![Report](https://goreportcard.com/badge/github.com/ztrue/shutdown)](https://goreportcard.com/report/github.com/ztrue/shutdown)\n[![Coverage Status](https://coveralls.io/repos/github/ztrue/shutdown/badge.svg?branch=master)](https://coveralls.io/github/ztrue/shutdown?branch=master)\n[![Build Status](https://travis-ci.com/ztrue/shutdown.svg?branch=master)](https://travis-ci.com/ztrue/shutdown)\n\nThis package provides convenient interface for working with `os.Signal`.\n\nMultiple hooks can be applied, they will be called simultaneously on app shutdown.\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/ztrue/shutdown\"\n)\n\nfunc main() {\n\tshutdown.Add(func() {\n\t\t// Write log.\n\t\t// Stop writing files.\n\t\t// Close connections.\n\t\t// Etc.\n\t\tlog.Println(\"Stopping...\")\n\t\tlog.Println(\"3\")\n\t\ttime.Sleep(time.Second)\n\t\tlog.Println(\"2\")\n\t\ttime.Sleep(time.Second)\n\t\tlog.Println(\"1\")\n\t\ttime.Sleep(time.Second)\n\t\tlog.Println(\"0, stopped\")\n\t})\n\n\t// App emulation.\n\tgo func() {\n\t\tlog.Println(\"App running, press CTRL + C to stop\")\n\t\tselect {}\n\t}()\n\n\tshutdown.Listen()\n}\n```\n\nFind more executable examples in [examples](examples) dir.\n\n## How to Use\n\n### Import\n\n```go\nimport \"github.com/ztrue/shutdown\"\n```\n\n### Add Shutdown Hook\n\n```go\nshutdown.Add(func() {\n\tlog.Println(\"Stopping\")\n})\n```\n\n### Remove Hook\n\n```go\nkey := shutdown.Add(func() {\n\tlog.Println(\"Stopping\")\n})\n\nshutdown.Remove(key)\n```\n\n### Hook With Custom Key\n\n```go\nshutdown.AddWithKey(\"mykey\", func() {\n\tlog.Println(\"Stopping\")\n})\n\nshutdown.Remove(\"mykey\")\n```\n\n### Hook With Signal Parameter\n\n```go\nshutdown.AddWithParam(func(os.Signal) {\n\tlog.Println(\"Stopping because of\", os.Signal)\n})\n```\n\n### Listen for Specific Signals\n\n```go\nshutdown.Listen(syscall.SIGINT, syscall.SIGTERM)\n```\n\n### Reload\nReload service on `SIGHUP`\n```go\n\tshutdown.AddWithParam(func(sig os.Signal) {\n\t\tif sig == syscall.SIGHUP {\n\t\t\tlog.Println( \"Reload conf...\")\n\t\t\treloadConf()\n\t\t\t// listen again signals to avoid shutdown\n\t\t\tshutdown.Listen(syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)\n\t\t} else { // SIGINT, SIGTERM\n\t\t\tlog.Println(\"Stopping...\")\n\t\t}\n\t})\n\tshutdown.Listen(syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)\n```\n","funding_links":[],"categories":["Utilities","公用事业公司","工具库`可以提升效率的通用代码库和工具`","Utility","工具库"],"sub_categories":["Utility/Miscellaneous","实用程序/Miscellaneous","HTTP Clients","查询语","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fztrue%2Fshutdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fztrue%2Fshutdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fztrue%2Fshutdown/lists"}