{"id":22496997,"url":"https://github.com/morebec/gorman","last_synced_at":"2025-10-28T10:03:49.307Z","repository":{"id":150453755,"uuid":"621044825","full_name":"Morebec/gorman","owner":"Morebec","description":"A Goroutine Management and Monitoring library","archived":false,"fork":false,"pushed_at":"2023-04-01T08:30:09.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-28T10:03:17.209Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Morebec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-03-29T22:06:41.000Z","updated_at":"2023-03-29T22:06:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"87ef2621-fb98-40ea-87e5-6e66f31f1991","html_url":"https://github.com/Morebec/gorman","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Morebec/gorman","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Fgorman","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Fgorman/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Fgorman/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Fgorman/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Morebec","download_url":"https://codeload.github.com/Morebec/gorman/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Morebec%2Fgorman/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281418054,"owners_count":26497723,"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","status":"online","status_checked_at":"2025-10-28T02:00:06.022Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2024-12-06T20:15:21.681Z","updated_at":"2025-10-28T10:03:49.288Z","avatar_url":"https://github.com/Morebec.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gorman\nGorman is a lightweight Go package for managing and monitoring the execution \nof goroutines. \n\nIt provides two core concepts:\n- `Goroutine`: Struct wrapping a goroutine and allowing to control its execution using Start/Stop semantics.\n- `Manager`: Allows controlling multiple `Goroutines` at once and managing their lifecycle as a group.\n\nThe `Manager` can be helpful to create Goroutine supervisors with a HTTP or CLI implementation\nto start and stop specific Goroutines.\n\n## Installation\nTo install Gorman, use go get:\n\n```shell\ngo get -u github.com/morebec/gorman\n```\n\n## Usage\n### Manager\nTo use Gorman, you first need to import it in your Go project:\n\n```go\nimport \"github.com/morebec/gorman\"\n```\n\nNext, you can create a new instance of a Manager:\n\n```go\nman := gorman.NewManager(gorman.Options{\n    Logger: slog.New(tint.Options{\n        Level:      slog.LevelDebug,\n        TimeFormat: time.TimeOnly,\n    }.NewHandler(os.Stdout)),\n})\n```\n\n#### Registering a Goroutine\nYou can then register a new Goroutine with the manager:\n\n```go\nman.Add(\"MyGoroutine\", func(ctx context.Context) error {\n    // Goroutine logic goes here\n\tselect {\n\tcase \u003c-ctx.Done():\n            return nil\n    }\n}, gorman.NeverRestart())\n```\n\nThe last parameter allows specifying the restart policy of the Goroutine.\nOut of the box the following Restart Policies are implemented:\n- `NeverRestart()`: Never restarts the Goroutine once it has stopped.\n- `AlwaysRestart()`: Always restarts the Goroutine.\n- `RestartOnError()`: Restart the Goroutine when it stops with an error.\n- `RestartPolicyFunc`: Allows specifying the restart logic using a function.\n\nSee the `RestartPolicy` interface for more information.\n\n#### Running the Manager\nThe `Run` method, allows running the manager and starting all the goroutines that were added to it:\n```go\nman.Run(context.Background())\n```\n\nThis method will run until the context is canceled, or the `Shutdown` method is called.\n\n#### Stopping a Goroutine\nTo stop a running Goroutine, you can call `Stop`:\n\n```go\nerr := manager.Stop(\"MyGoroutine\")\nif err != nil {\n    // handle error\n}\n```\n\n\u003e Note: For goroutines to be stoppable they should correctly listen to the ctx.Done() channel.\n\u003e Go routines will also stop whenever they return.\n\n#### Starting a Goroutine\nIt is possible to start a goroutine that was previously stopped using the `Start` method:\n\n```go\nerr := man.Start(context.Background(), \"mygoroutine\")\n```\n\n#### Getting the current state of goroutines\nThe manager exposes the Status method which returns a slice of `GoroutineState` \nwhich represents the current state of Goroutines.\n\n### Goroutine\nGoroutines are the building blocks of Gorman and wrap the execution of a go routine\nto allow start/stop semantics. They can be used independently of the manager to control\nspecific goroutines in isolation.\n\n#### Creating a Goroutine\n```go\ng := gorman.NewGoroutine(\"name\", func (ctx context.Context) error {\n\t// Perform work here.\n})\n```\n### Starting a Goroutine\n```go\ng.Start(context.Background())\n```\n\n### Stopping a Goroutine\nThe `Stop` method will cancel the goroutine's context and will wait for it\nto be stopped.\n```go\nerr := g.Stop()\n```\n\nAlternatively a goroutine can be stopped through its context.\n```go\nctx, cancel := context.WithCancel(context.Background())\ng.Start(ctx)\ncancel()\n```\n\n### Listening to Goroutine events\nGoroutines have an internal broadcasting system that allows subscribers to listen to Goroutine events\nsuch as  `GoroutineStartedEvent` and `GoroutineEndedEvent`.\n\nHere's an example:\n```go\ng := gorman.NewGoroutine(\"mygoroutine\", func(ctx context.Context) error {\n\t// do something\n\treturn nil\n})\neventChan := g.Listen()\n\ng.Start(context.Background())\n\nfor event := range eventChan {\n\tswitch event.(type) {\n\tcase gorman.GoroutineStartedEvent:\n\t\tfmt.Printf(\"Goroutine %s started\\n\", g.State.Name)\n\tcase gorman.GoroutineStoppedEvent:\n\t\te := event.(GoroutineStoppedEvent)\n\t\tif e.Error != nil {\n\t\t\tfmt.Printf(\"Goroutine %s stopped with error: %s\\n\", event.Name, event.Error.Error())\n\t\t} else {\n\t\t\tfmt.Printf(\"Goroutine %s stopped\\n\", event.Name)\n\t\t}\n\t}\n}\n\ng.Unlisten(eventChan)\n```\n\nThis mechanism can be useful to react to a goroutine's execution. For instance\nthe `Manager` uses this mechanism to monitor the lifecycle of the Goroutines.\n\n\u003e Note: Every call to the Listen() method will return a new channel. When done with using the\n\u003e channel, it should be released using the `Unlisten` method.\n\n## Examples\nFor more usage examples, check the `examples` directory.\n\n## License\nGorman is released under the MIT License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorebec%2Fgorman","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorebec%2Fgorman","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorebec%2Fgorman/lists"}