Nafets94

Nafets94 OP

I have the following simple Agent

defmodule ListAdd do
  use Agent

  def addToList(agent, value) do
    Agent.update(agent, fn list -> [value | list] end)
  end
end

How can I crash it so it gets restarted by the supervisor? I’ve been searching around and looks like you can simulate this using it’s stop function [stop(agent, reason \\ :normal, timeout \\ :infinity)](Agent — Elixir v1.20.2) found in the documentation?

First 2 of 2 Posts Switch mode

cmkarlsson

cmkarlsson

You can use Process.exit/2 to kill a process.

Process.exit(agent, :somereason)
Process.exit(agent, :kill) # kills process even if trapping exits

The following is from the built-in help.


                             def exit(pid, reason)

Sends an exit signal with the given reason to the pid.

The following behaviour applies if reason is any term except :normal or :kill:

  1. If pid is not trapping exits, pid will exit with the given reason.
  2. If pid is trapping exits, the exit signal is transformed into a
     message {:EXIT, from, reason} and delivered to the message queue of pid.

If reason is the atom :normal, pid will not exit (unless pid is the calling
process, in which case it will exit with the reason :normal). If it is trapping
exits, the exit signal is transformed into a message {:EXIT, from, :normal} and
delivered to its message queue.

If reason is the atom :kill, that is if exit(pid, :kill) is called, an
untrappable exit signal is sent to pid which will unconditionally exit with
reason :killed.

Inlined by the compiler.

## Examples

    Process.exit(pid, :kill)
lpil

lpil

Creator of Gleam

I would say that “let it crash” is a statement about how unexpected errors should be handled, so a more realistic way to simulate a crash would be to call the update with a function with a bug that causes it to crash . For example you supply give a function that pattern matches on the incorrect data structure.

Why do you want your agent to crash? :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement