JEG2

JEG2

Author of Designing Elixir Systems with OTP

I am collecting examples of uses for Agent. I would love to hear uses you can share. Thanks in advance!

Showing Posts 1 to 10

hubertlepicki

hubertlepicki

Oh I’d love to hear some good use cases too. I sort of feel guilty not using Agent at all. I usually grab GenServer instead without much thinking.

OvermindDL1

OvermindDL1

Yeah, same here, I always have ‘some’ functionality associated with shared state like that, so a GenServer has always seemed better, otherwise for shared state without functionality then I always use ETS/Mnesia instead… ^.^;

JEG2

JEG2 OP

Author of Designing Elixir Systems with OTP

I have noticed one place that I tend to use it. I’ll share that later, so as not to bias the answers of others just yet…

JEG2

JEG2 OP

Author of Designing Elixir Systems with OTP

I find it interesting that not many folks are chiming in with answers yet. I wonder if that signifies that they don’t get a ton of usage.

For myself, I have found them useful in testing other processes. I’ve rigged processes to pull pre-arranged values from them and stored values representing work happening inside of a process that I could later write assertions against.

The one time I tried to use them in an application, I grew to regret it and later replaced them with GenServer processes, for the functionality reasons noted by @OvermindDL1.

JEG2

JEG2 OP

Author of Designing Elixir Systems with OTP

Oops, I did forget another decent usage I found for them!

I recorded the events of a simulation as they happened, then fetched and cleared all changes in the drawing code (using get_and_update()). That worked fine.

jeramyRR

jeramyRR

I’ve been wondering the same thing myself. When do people actually use agent?

brightball

brightball

I’d thought about using them as a low TTL query cache

michalmuskala

michalmuskala

I don’t think I ever used Agent in one of my programs. That said, the use cases in the wild, I saw, are:

sasajuric

sasajuric

Author of Elixir In Action

I also don’t use Agents for the production runtime code. I did use them on few occasions in tests.

StefanHoutzager

StefanHoutzager

I’m using an agent like KV.bucket: http://elixir-lang.org/getting-started/mix-otp/agent.html. In the following function I’m using an agent as “accumulator”. If there’s a handier solution (I’m not as experienced as most posters) I would like to hear. :wink:

 @doc """
    Validate the input `body` in the taskform, task identified with `element_id', `socket` is third param
  """
  @spec validate_task(map, String.t, Socket.t) :: :ok
  def validate_task(body, element_id, socket) do
    Agent.start_link(fn -> [] end, name: :error_accumulator)

    traverse = fn(data, trav) ->
      case data do
        [_|_] ->
          Enum.map(data, &(trav.(&1,trav)))
        %{"key" => name, "validate" => validation, "label" => label} ->
          result = validate_item(body |> Map.fetch!(name),
                                 label,
                                 validation)
          if result != "", do: Agent.update(:error_accumulator, &List.insert_at(&1, 0, result))
        %{"components" => components} ->
           trav.(components,trav)
        %{"columns" => columns} ->
           trav.(columns,trav)
        %{"rows" => rows} ->
           trav.(rows, trav)
        _ ->
           data
      end
    end

    WorkflowAgent.get_user_tasks(socket.assigns.agent_pid)
    |> Stream.filter(fn task -> Map.fetch!(task, :id) === element_id end)
    |> Enum.at(0)
    |> Map.fetch!(:formfields)
    |> Enum.at(0)
    |> Map.fetch!("components")
    |> traverse.(traverse)

    errors = Agent.get(:error_accumulator, &(&1))

    Agent.stop(:error_accumulator, :normal)

    case errors do
      [] -> push socket, "cleanupFormIO", %{}
            :ok
      _  -> push socket, "alert",  %{message: "<br>" <> (errors |> List.foldl(" ", fn(x, acc) -> x <> acc end)), type: "error"}
            :errors_found
    end
  end

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews