Search⌘ K
AI Features

Limit Concurrency and Inspect the Supervisors

Explore how to enforce concurrency limits in Elixir jobs and inspect supervision trees at runtime. Understand using GenServer callbacks, Registry for process lookup, and count_children to monitor active and supervised processes.

Limit the concurrency of import jobs

We now put our new running_imports/0 to use. Let’s add this check to Jobber.start_job/1:

C++
#file path -> jobber/lib/jobber.ex
def start_job(args) do
if Enum.count(running_imports()) >= 5 do
{:error, :import_quota_reached}
else
DynamicSupervisor.start_child(JobRunner, {JobSupervisor, args})
end
end

Try starting more than five jobs. After the fifth attempt, we should see this error result: {:error, :import_quota_reached}.

This is the executable ...

C++
Jobber.start_job(work: good_job, type: "import")

Here’s the output we get:

iex(1)>
...