{"id":13709574,"url":"https://github.com/jimmysawczuk/worker","last_synced_at":"2025-10-27T12:17:21.691Z","repository":{"id":144200981,"uuid":"14367777","full_name":"jimmysawczuk/worker","owner":"jimmysawczuk","description":"A Go library that takes arbitrary tasks and executes n at a time until done","archived":false,"fork":false,"pushed_at":"2020-10-31T12:31:53.000Z","size":21,"stargazers_count":14,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-26T15:46:37.874Z","etag":null,"topics":["go"],"latest_commit_sha":null,"homepage":null,"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/jimmysawczuk.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}},"created_at":"2013-11-13T15:24:12.000Z","updated_at":"2022-10-21T19:21:12.000Z","dependencies_parsed_at":"2024-05-13T00:11:55.798Z","dependency_job_id":null,"html_url":"https://github.com/jimmysawczuk/worker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmysawczuk%2Fworker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmysawczuk%2Fworker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmysawczuk%2Fworker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmysawczuk%2Fworker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jimmysawczuk","download_url":"https://codeload.github.com/jimmysawczuk/worker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251233923,"owners_count":21556762,"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"],"created_at":"2024-08-02T23:00:41.738Z","updated_at":"2025-10-27T12:17:21.601Z","avatar_url":"https://github.com/jimmysawczuk.png","language":"Go","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"# worker\n\n[ ![travis-ci status for jimmysawczuk/worker](https://travis-ci.org/jimmysawczuk/worker.svg)](https://travis-ci.org/jimmysawczuk/worker) [![GoDoc](https://godoc.org/github.com/jimmysawczuk/worker?status.svg)](https://godoc.org/github.com/jimmysawczuk/worker) [![Go Report Card](https://goreportcard.com/badge/github.com/jimmysawczuk/worker)](https://goreportcard.com/report/github.com/jimmysawczuk/worker)\n\nPackage `worker` is a Go package designed to facilitate the easy parallelization of a number of tasks `N` with up to `n` at a time being computed concurrently.\n\n## Getting started\n\n```bash\n$ go get github.com/jimmysawczuk/worker\n```\n\n## Using in your program\n\n### Design\n\nTo use this package, all you need to do is package your tasks into types that satisfy the following interface:\n\n```go\ntype Job interface {\n\tRun()\n}\n```\n\n### Implementation\n\nFrom there, it's easy to add your task to the queue and start it:\n\n```go\ntype SampleJob struct {\n\tName     string\n\tDuration time.Duration\n}\n\nfunc (s *SampleJob) Run() {\n\n\ttime.Sleep(s.Duration)\n\tlog.Printf(\"Done, slept for %s\\n\", s.Duration)\n\n}\n\n// only do 3 jobs at a time\nworker.MaxJobs = 3\n\nw := worker.NewWorker()\nw.Add(SampleJob{\n\tName: \"sleep 1\",\n\tDuration: 1 * time.Second,\n})\n\nw.Add(SampleJob{\n\tName: \"sleep 2\",\n\tDuration: 2 * time.Second,\n})\n\n// ... and so forth\n\nw.RunUntilDone()\n```\n\nYour `Job`s are packaged internally as `Package`s, which have nice features such as storing a unique-per-worker ID, as well as the return value that is retrieved from the channel. This is mostly used for event handling though; keep in mind that you can store your information in this value or you can simply use your custom `Job` type and store more custom information.\n\n### Events\n\nYou can also listen for events from the `Worker` and react appropriately. Currently, three events are fired: `JobQueued`, `JobStarted`, and `JobFinished`. Add an event handler like so:\n\n```go\nw.On(worker.JobStarted, func(pk *worker.Package, args ...interface{}) {\n\t// You can use type assertion to get back your original job from this:\n\tjob := pk.Job()\n})\n```\n\nCurrently each event emitter only passes one argument, the relevant `Package` that emitted the event. There may be more added later, for other events, but the `Package` will always be the first argument.\n\n## More documentation\n\nYou can find more documentation at [GoDoc][godoc].\n\n## Examples\n\n* [`less-tree`][less-tree], a recursive, per-directory LESS compiler uses `worker`\n\n  [godoc]: http://godoc.org/github.com/jimmysawczuk/worker\n  [less-tree]: http://github.com/jimmysawczuk/less-tree\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimmysawczuk%2Fworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjimmysawczuk%2Fworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimmysawczuk%2Fworker/lists"}