Search⌘ K
AI Features

Process Restart Values and Frequency

Explore how to control GenServer process restart behaviors using :transient settings and understand supervisor restart frequency with max_restarts and max_seconds. This lesson helps you configure supervisors to handle job failures gracefully and maintain system reliability in concurrent Elixir applications.

Process restart values revisited

By default, GenServer processes are always restarted by their supervisor, which is the :permanent setting. In our case, we intentionally shut down the process. However, JobRunner thinks something must have gone wrong, so it keeps restarting the process forever. We can easily fix this by using the :transient restart option, which tells the supervisor not to restart the process if it is exiting normally.

We have modified the use GenServer statement in job.ex file for the new setting:

C++
#file path -> jobber/lib/jobber/job.ex
use GenServer, restart: :transient

We can try these commands in the IEx mode in the playground widget.

This is the executable command:

Scala
Jobber.start_job(work: good_job)
Scala
Jobber.start_job(work: bad_job)

Here’s the output we get:

iex(1)> Jobber.start_job(work: good_job)
{:ok, #PID<0.172.0>}
iex(2)> 
08:26:07.918 [info]  Job completed y0t51mM
 
08:26:07.922 [info]  Job exiting y0t51mM
 
nil
iex(3)> Jobber.start_job(work: bad_job)
{:ok, #PID<0.176.0>}
iex(4)> 
08:26:20.368 [warn]  Job errored
...