{"id":21511980,"url":"https://github.com/strvcom/strv-backend-go-background","last_synced_at":"2025-04-09T18:20:51.140Z","repository":{"id":223280393,"uuid":"759575120","full_name":"strvcom/strv-backend-go-background","owner":"strvcom","description":"Never lose your goroutine again.","archived":false,"fork":false,"pushed_at":"2024-04-29T17:15:20.000Z","size":113,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-23T20:21:54.371Z","etag":null,"topics":["go","goroutines","package"],"latest_commit_sha":null,"homepage":"https://go.strv.io","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/strvcom.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-18T22:48:40.000Z","updated_at":"2024-04-29T05:44:46.000Z","dependencies_parsed_at":"2024-03-12T16:48:04.540Z","dependency_job_id":"ba2ba260-acd7-45a0-9a3e-441e935f3482","html_url":"https://github.com/strvcom/strv-backend-go-background","commit_stats":null,"previous_names":["robertrossmann/background"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-background","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-background/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-background/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-background/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/strvcom","download_url":"https://codeload.github.com/strvcom/strv-backend-go-background/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085388,"owners_count":21045152,"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","goroutines","package"],"created_at":"2024-11-23T22:24:40.925Z","updated_at":"2025-04-09T18:20:51.118Z","avatar_url":"https://github.com/strvcom.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003e\u003ccode\u003ego.strv.io/background\u003c/code\u003e\u003c/h1\u003e\n\n  [![Continuous Integration][badge-ci]][workflow-ci] [![codecov][badge-codecov]][codecov-dashboard]\n\n  \u003e Never lose your goroutine again.\u003cbr /\u003eBuilt with ❤️ at [STRV](https://www.strv.com)\n\u003c/div\u003e\n\n## About\n\nThis package provides mechanism to easily run a task (a function) in a goroutine and provides mechanisms to wait for all tasks to finish (a synchronisation point). Addiionally, you can attach an observer to the background manager and get notified when something interesting happens to your task - like when it finishes, errors or takes too long to complete. This allows you to centralise your error reporting and logging.\n\nThe purpose of the central synchronisation point for all your background goroutines is to make sure that your application does not exit before all goroutines have finished. You can trigger the synchronisation when your application receives a terminating signal, for example, and wait for all tasks to finish before allowing the application to exit.\n\n## Installation\n\n```sh\ngo get go.strv.io/background\n```\n\n## Usage\n\nThere are two types of tasks you can execute:\n\n- one-off: a task that runs once and then finishes\n- looping: a task that runs repeatedly until it is stopped\n\nOne-off tasks are great for triggering a single operation in the background, like sending an email or processing a file. Looping tasks are great for running a background worker that processes a queue of items, like reading from an AWS SQS queue periodically.\n\nAdditionally, each task can define its retry policies, which allows you to automatically retry the task if it fails. For one-off tasks, the task is repeated until its retry policy says it should stop, then the task is considered finished. For looping tasks, the retry policy is applied to each iteration of the task - upon failure, the task is retried using its defined retry policy and when the policy says it should stop, the task continues on to the next iteration and the process repeats.\n\n### Initialising the manager\n\n```go\npackage main\n\nimport (\n  \"context\"\n\n  \"go.strv.io/background\"\n  \"go.strv.io/background/observer\"\n)\n\nfunc main() {\n  manager := background.NewManagerWithOptions(background.Options{\n    // Use one of the provided observers that prints logs to the console using log/slog\n    // Feel free to implement your own.\n    Observer: observer.Slog{},\n  })\n\n  // Share the manager with the rest of your application\n\n  interrupt := make(chan os.Signal, 1)\n\tsignal.Notify(interrupt, os.Interrupt)\n  \u003c-interrupt\n  // Wait for all tasks to finish, then allow the application to exit\n  manager.Close()\n}\n```\n\n### Scheduling tasks\n\n```go\nimport (\n  \"time\"\n\n  \"go.strv.io/background\"\n  \"go.strv.io/background/task\"\n  \"github.com/kamilsk/retry/v5/strategy\"\n)\n\nmaanger := background.NewManagerWithOptions(background.Options{\n  Observer: observer.Slog{},\n})\n\n// Executes a one-off task - the task will run only once (except if it fails and has a retry policy)\noneoff := task.Task{\n  Type: task.TypeOneOff,\n  Fn: func(ctx context.Context) error {\n    // Do something interesting...\n    \u003c-time.After(3 * time.Second)\n    return nil\n  },\n  Retry: task.Retry{\n    strategy.Limit(3),\n  }\n}\n\nmanager.RunTask(context.Background(), oneoff)\n\n// Execute a looping task - the task will run repeatedly until it is stopped\nlooping := task.Task{\n  Type: task.TypeLoop,\n  Fn: func(ctx context.Context) error {\n    // Do something interesting...\n    \u003c-time.After(3 * time.Second)\n    return nil\n  },\n  Retry: task.Retry{\n    strategy.Limit(3),\n  }\n}\n\n// Execute the task to be continuously run in an infinite loop until manager.Close() is called\nmanager.RunTask(context.Background(), looping)\n```\n\n## Examples\n\nYou can find a sample executable in the [examples](examples) folder. To run them, clone the repository and run:\n\n```sh\ngo run examples/slog/slog.go\n```\n\n## License\n\nSee the [LICENSE](LICENSE) file for details.\n\n[badge-ci]: https://github.com/strvcom/strv-backend-go-background/actions/workflows/ci.yaml/badge.svg\n[workflow-ci]: https://github.com/strvcom/strv-backend-go-background/actions/workflows/ci.yaml\n[badge-codecov]: https://codecov.io/gh/strvcom/strv-backend-go-background/graph/badge.svg?token=ST3JD5GCRN\n[codecov-dashboard]: https://codecov.io/gh/strvcom/strv-backend-go-background\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrvcom%2Fstrv-backend-go-background","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrvcom%2Fstrv-backend-go-background","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrvcom%2Fstrv-backend-go-background/lists"}