A concurrent job runner
The problem
Build a program that reads a list of jobs, runs them with a pool of worker goroutines, collects the results through channels and prints a report in the order of the jobs, including the jobs that failed.
Requirements
- A job struct and a result struct that carries the job's position and an error
- A pool of worker goroutines reading from a jobs channel
- A sync.WaitGroup and closed channels so that nothing waits for ever
- A final report in input order with a count of successes and failures
Technologies
- Go
- Goroutines
- Channels
- sync.WaitGroup
- Errors
Step by step
- Define the job and result types and write one worker function.
- Start the workers, send the jobs and close the jobs channel.
- Collect all results and store each in its position.
- Print the ordered report and the totals.
Expected result
A program that uses several goroutines and still prints the same report on every run.
How it is evaluated
- No deadlock and no lost result
- The output order does not depend on which worker was faster
- Failed jobs are reported with their error
- Channels are closed by the sender
Ways to improve it
- Make the number of workers a command from the input
- Add a limit on how many jobs may fail before the run stops
- Protect a shared counter of finished jobs with a Mutex