GenServer Callbacks: handle_info
Explore the GenServer handle_info callback to handle system messages and implement retry logic for failed operations like email sending. Learn how to use Process.send/2 and Process.send_after/3 for delayed message processing and managing long-running Elixir processes effectively.
The handle_info callback function
Other than using GenServer.cast/2 and GenServer.call/3, we can also send a message to a process using Process.send/2. This generic message triggers the handle_info/2 callback, which works exactly like handle_cast/2 and can return the same set of tuples. Usually, handle_info/2 deals with system messages. Normally, we expose our server API using cast/2 and call/2 and keep send/2 for internal use.
Implement retries for failed emails
Let’s see how we can use handle_info/2. We’ll implement retries for emails that fail to send. To test this, we need to modify sender.ex, so one of the emails returns an error. We replace our ...