...

/

Process Restart Values and Frequency

Process Restart Values and Frequency

Learn about process restart values and restart frequency to control processes' shut downs and restarts.

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:

Press + to interact
#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:

Press + to interact
Jobber.start_job(work: good_job)
Press + to interact
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,
...