{"id":37144833,"url":"https://github.com/struct0x/exitplan","last_synced_at":"2026-01-14T16:59:52.479Z","repository":{"id":318878216,"uuid":"1056115963","full_name":"struct0x/exitplan","owner":"struct0x","description":"A Go library for managing the lifecycle of an application with graceful shutdown capabilities.","archived":false,"fork":false,"pushed_at":"2025-10-24T18:25:32.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-24T20:22:34.694Z","etag":null,"topics":["context","go","golang","graceful-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/struct0x.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,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-13T12:23:54.000Z","updated_at":"2025-10-24T18:23:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"2ea947a7-c233-497a-965c-c6889f19bd2a","html_url":"https://github.com/struct0x/exitplan","commit_stats":null,"previous_names":["struct0x/exitplan"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/struct0x/exitplan","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/struct0x%2Fexitplan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/struct0x%2Fexitplan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/struct0x%2Fexitplan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/struct0x%2Fexitplan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/struct0x","download_url":"https://codeload.github.com/struct0x/exitplan/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/struct0x%2Fexitplan/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28427179,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T16:38:47.836Z","status":"ssl_error","status_checked_at":"2026-01-14T16:34:59.695Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["context","go","golang","graceful-shutdown"],"created_at":"2026-01-14T16:59:51.789Z","updated_at":"2026-01-14T16:59:52.470Z","avatar_url":"https://github.com/struct0x.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Exitplan\n[![Go Reference](https://pkg.go.dev/badge/github.com/struct0x/exitplan.svg)](https://pkg.go.dev/github.com/struct0x/exitplan)\n![Coverage](https://img.shields.io/badge/Coverage-83.2%25-brightgreen)\n\nA Go library for managing the lifecycle of an application with graceful shutdown capabilities.\n\n## Overview\n\nThe Exitplan library provides a simple mechanism for managing the lifetime of an application. \nIt helps you handle application startup, running, and shutdown phases with proper resource cleanup. \n\nKey features include:\n\n- Distinct application lifecycle phases (starting, running, teardown)\n- Context-based lifecycle management\n- Graceful shutdown with customizable timeout\n- Flexible callback registration for cleanup operations\n- Signal handling for clean application termination\n- Synchronous and asynchronous shutdown callbacks\n- Error handling during shutdown\n\n## Installation\n\n```bash\ngo get github.com/struct0x/exitplan\n```\n\n## Lifecycle Phases\n\nExitplan splits application lifetime into three phases, each with its own context:\n\n- **Starting**: before `Run()` begins. Use `StartingContext()` for initialization.  \n  It is canceled immediately when `Run()` starts.\n\n- **Running**: active between `Run()` and `Exit()`. Use `Context()` for workers and other long-running tasks.  \n  It is canceled as soon as shutdown begins.\n\n- **Teardown**: after `Exit()` is called. Use `TeardownContext()` in shutdown callbacks.  \n  It is canceled when the global teardown timeout elapses.\n\n### Callback ordering\n\nShutdown callbacks registered with `OnExit*` are executed in **LIFO order** (last registered, first executed).  \nThis mirrors resource lifecycles: if you start DB then HTTP, shutdown runs HTTP then DB.  \nCallbacks marked with `Async` are awaited up to the teardown timeout.\n\n## Usage\n\n### Basic Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"syscall\"\n\t\"time\"\n\n\t\"github.com/struct0x/exitplan\"\n)\n\nfunc main() {\n\t// Create a new Exitplan instance with signal handling for graceful shutdown\n\tex := exitplan.New(\n\t\texitplan.WithSignal(syscall.SIGINT, syscall.SIGTERM),\n\t\texitplan.WithTeardownTimeout(5*time.Second),\n\t)\n\n\t// Register cleanup functions\n\tex.OnExit(func() {\n\t\tfmt.Println(\"Cleaning up resources...\")\n\t\ttime.Sleep(1 * time.Second)\n\t\tfmt.Println(\"Cleanup complete\")\n\t})\n\n\t// Start your application\n\tfmt.Println(\"Application starting...\")\n\n\t// Run the application (blocks until Exit() is called)\n\texitCause := ex.Run()\n\tfmt.Printf(\"Application exited: %v\\n\", exitCause)\n}\n\n```\n\n### Advanced Example with Context\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"syscall\"\n\t\"time\"\n\n\t\"github.com/struct0x/exitplan\"\n)\n\nfunc main() {\n\t// Create a new Exitplan instance with options\n\tex := exitplan.New(\n\t\texitplan.WithSignal(syscall.SIGINT, syscall.SIGTERM),\n\t\texitplan.WithTeardownTimeout(10*time.Second),\n\t\texitplan.WithExitError(func(err error) {\n\t\t\tfmt.Printf(\"Error during shutdown: %v\\n\", err)\n\t\t}),\n\t)\n\n\t// Use the starting context for initialization\n\tstartingCtx := ex.StartingContext()\n\t_ = startingCtx\n\t// Initialize resources with the starting context\n\n\t// For example, pinging a database connection to ensure it is ready, yet it should not freeze the application\n\t// err := db.Ping(startingCtx)\n\n\t// Register cleanup with context awareness\n\tex.OnExitWithContext(func(ctx context.Context) {\n\t\tfmt.Println(\"Starting cleanup...\")\n\n\t\tselect {\n\t\tcase \u003c-time.After(2 * time.Second):\n\t\t\tfmt.Println(\"Cleanup completed successfully\")\n\t\tcase \u003c-ctx.Done():\n\t\t\tfmt.Println(\"Cleanup was interrupted by timeout\")\n\t\t}\n\t})\n\n\t// Register cleanup that might return an error\n\tex.OnExitWithContextError(func(ctx context.Context) error {\n\t\tfmt.Println(\"Closing database connection...\")\n\t\ttime.Sleep(1 * time.Second)\n\t\treturn nil\n\t})\n\n\t// Register an async cleanup task\n\tex.OnExit(func() {\n\t\tfmt.Println(\"Performing async cleanup...\")\n\t\ttime.Sleep(3 * time.Second)\n\t\tfmt.Println(\"Async cleanup complete\")\n\t}, exitplan.Async)\n\n\t// Start your application\n\tfmt.Println(\"Application starting...\")\n\n\t// Get the running context to use in your application\n\trunningCtx := ex.Context()\n\n\t// Start a worker that respects the application lifecycle\n\tworkerDone := make(chan struct{})\n\tgo func() {\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase \u003c-runningCtx.Done():\n\t\t\t\tfmt.Println(\"Worker shutting down...\")\n\t\t\t\ttime.Sleep(100 * time.Millisecond) // Simulate some work\n\t\t\t\tclose(workerDone)\n\t\t\t\treturn\n\t\t\tcase \u003c-time.After(1 * time.Second):\n\t\t\t\tfmt.Println(\"Worker doing work...\")\n\t\t\t}\n\t\t}\n\t}()\n\tex.OnExitWithContext(func(ctx context.Context) {\n\t\tselect {\n\t\tcase \u003c-workerDone:\n\t\t\tfmt.Println(\"Worker shutdown complete\")\n\t\tcase \u003c-ctx.Done():\n\t\t\tfmt.Println(\"Worker shutdown interrupted\")\n\t\t}\n\t})\n\n\t// Run the application (blocks until Exit() is called)\n\texitCause := ex.Run()\n\tfmt.Printf(\"Application exited: %v\\n\", exitCause)\n}\n\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstruct0x%2Fexitplan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstruct0x%2Fexitplan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstruct0x%2Fexitplan/lists"}