Limit Concurrency and Inspect the Supervisors
Learn to limit the number of jobs we can run concurrently and examine the count_children function of their supervisor.
We'll cover the following...
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
:
Press + to interact
#file path -> jobber/lib/jobber.exdef start_job(args) doif Enum.count(running_imports()) >= 5 do{:error, :import_quota_reached}elseDynamicSupervisor.start_child(JobRunner, {JobSupervisor, args})endend
Try starting more than five jobs. After the fifth attempt, we should see this error result: {:error, :import_quota_reached}
.
This is the executable command:
Press + to interact
Jobber.start_job(work: good_job, type: "import")
Here’s the output we get:
iex(1)>
...