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 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
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
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
nseaSeb
AcmeScript — Writing JS hooks as if I were still using Elixir I’ve been having fun building a little something over the last few days: Ac...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews