{"id":36443356,"url":"https://github.com/lftk/lcfx","last_synced_at":"2026-01-11T22:01:22.566Z","repository":{"id":325238242,"uuid":"1073670947","full_name":"lftk/lcfx","owner":"lftk","description":"A go.uber.org/fx module for gracefully managing long-running services within the application lifecycle.","archived":false,"fork":false,"pushed_at":"2025-10-12T05:31:06.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-11-20T10:04:03.979Z","etag":null,"topics":["asynchronous","background-services","lifecycle","long-running-tasks","uber-fx"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/lftk/lcfx","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/lftk.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-10-10T12:58:17.000Z","updated_at":"2025-10-12T05:31:09.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/lftk/lcfx","commit_stats":null,"previous_names":["lftk/lcfx"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/lftk/lcfx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lftk%2Flcfx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lftk%2Flcfx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lftk%2Flcfx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lftk%2Flcfx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lftk","download_url":"https://codeload.github.com/lftk/lcfx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lftk%2Flcfx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28324835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T18:42:50.174Z","status":"ssl_error","status_checked_at":"2026-01-11T18:39:13.842Z","response_time":60,"last_error":"SSL_read: 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":["asynchronous","background-services","lifecycle","long-running-tasks","uber-fx"],"created_at":"2026-01-11T22:01:21.390Z","updated_at":"2026-01-11T22:01:22.559Z","avatar_url":"https://github.com/lftk.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lcfx\n\n`lcfx` is a small helper library for the [go.uber.org/fx](https://github.com/uber-go/fx) dependency injection framework. It simplifies the management of long-running, asynchronous tasks (like web servers, message queue consumers, or other background services) within the `fx` application lifecycle.\n\n## The Problem\n\nWhen using `fx`, `OnStart` hooks are expected to complete quickly. If a hook blocks for too long (by default, 15 seconds), the application will time out during startup and exit with an error. Starting a long-running service like an HTTP server directly in an `OnStart` hook will cause this timeout.\n\nThe common workaround is to launch the service in a separate goroutine. However, this requires manual wiring for graceful shutdown and a way to signal startup errors back to the main application, which can be complex.\n\n## The Solution: `lcfx`\n\n`lcfx` provides a custom `Lifecycle` wrapper with an `AppendAsync` method. This allows you to write your long-running task logic in a simple, synchronous style, while `lcfx` handles the complexity of running it in the background and ensuring graceful shutdown.\n\nTo use it, you need to:\n\n1.  Add `lcfx.Module` to your `fx.New()` call. This provides the custom lifecycle to the dependency graph.\n2.  Request `lcfx.Lifecycle` in your constructor instead of `fx.Lifecycle`.\n3.  Use the `lc.AppendAsync` method to register your long-running service.\n\nHere is a complete example:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/lftk/lcfx\" // Import lcfx\n\t\"go.uber.org/fx\"\n)\n\nfunc main() {\n\tapp := fx.New(\n\t\t// 1. Add the lcfx.Module.\n\t\tlcfx.Module,\n\n\t\tfx.Provide(NewMux, NewHandler),\n\t\tfx.Invoke(Register),\n\n\t\t// Disable fx logging for clarity in this example.\n\t\tfx.NopLogger,\n\t)\n\tapp.Run()\n}\n\n// 2. Request lcfx.Lifecycle in the constructor.\nfunc NewMux(lc lcfx.Lifecycle) *http.ServeMux {\n\tmux := http.NewServeMux()\n\tserver := \u0026http.Server{\n\t\tAddr:    \"127.0.0.1:8080\",\n\t\tHandler: mux,\n\t}\n\n\t// 3. Use lc.AppendAsync for the long-running task.\n\tlc.AppendAsync(fx.Hook{\n\t\tOnStart: func(context.Context) error {\n\t\t\tfmt.Println(\"Starting HTTP server at\", server.Addr)\n\t\t\t// Write blocking code directly. lcfx handles the goroutine.\n\t\t\t// If this returns an error, lcfx will shut down the app.\n\t\t\terr := server.ListenAndServe()\n\t\t\tif errors.Is(err, http.ErrServerClosed) {\n\t\t\t\tfmt.Println(\"HTTP server shut down gracefully.\")\n\t\t\t\treturn nil // Expected error on graceful shutdown.\n\t\t\t}\n\t\t\treturn err\n\t\t},\n\t\tOnStop: func(ctx context.Context) error {\n\t\t\tfmt.Println(\"Stopping HTTP server.\")\n\t\t\treturn server.Shutdown(ctx)\n\t\t},\n\t})\n\treturn mux\n}\n\nfunc NewHandler() http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tfmt.Println(\"Got a request.\")\n\t\tfmt.Fprintln(w, \"Hello, world!\")\n\t})\n}\n\nfunc Register(mux *http.ServeMux, h http.Handler) {\n\tmux.Handle(\"/\", h)\n}\n```\n\n## Installation\n\n```sh\ngo get github.com/lftk/lcfx\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flftk%2Flcfx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flftk%2Flcfx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flftk%2Flcfx/lists"}