I am working on an application that receives "jobs" to work on, and could only run 3 jobs in parallel (say 3 is a configurable number).
Here's how the flow looks like:
- A job is received somehow (not really relevant to the point)
- A task is running that watches a queue of jobs and picks up the next one 2.a. The job is passed over to a ConcurrentExecutor class that we have created that can block the creation of an additional Task (which runs the "job" itself) using a semaphore, until the other tasks (if any) have finished.
At this point, the task that's being created in 2.a is an abandoned task, meaning that we are losing any exceptions that occur in that "job" task that we have spawned.
I am trying to find an elegant solution to managing the spawned tasks. I have thought of a simple list that I can add the Task to, after which I can remove the task from it once it's done (same location as when I release the semaphore one the task is done).
But then we have hit a roadblock, in the sense of how would we wait for the tasks to finish (to monitor their exceptions), AND in the same time wait for new jobs to pick up. Yep, we could create another thread to monitor these, but then we would have to Join to it as some point as well.
We have thought about only picking up the "failed" jobs using ContinueWith, and should there be anything in the list - that's an exception from the task, but that's also not very elegant.
I am hunting for an idea that would allow me to: a. monitor the queue and spawn new jobs b. monitor the current running jobs and catch their exceptions
Thanks!