hernancuy

hernancuy

Hello everybody, I’m creating an app service aggregator, I created some modules, my idea it’s one module create tasks and inside other module create the await and select only one task, after you select one task you will be get a mutex, but this is another history. So I’m using this modules, one is “App” and another is “Driver”, when you add the name of app, It goes to create some Task async, the names that I’m gonna use, are created by a random name creation line. Until now it’s working well, the issue comes, when I try to execute the async from another module, says that I’m sending two arguments, I modified the function to get two params but not works, how can I get this async from other modules ? =D thanks my friends

defmodule Tarea2 do

  defmodule App do

    @app Apps

    #### Start agent
    def start do
      Agent.start_link(fn -> %{} end,  name: @app)
    end

    #### Start tasks from app
    def task(app) do
      for x <- 0..4 do
        name = Enum.take_random(alphabet = Enum.to_list(?a..?e), 5)
        :timer.sleep(1000)
        name = Task.async(fn ->
              pid = self()
              ms = Enum.random(1..1000)
              info = "Codigo #{name} : Hernan Cuy, lugar x lugar y, 20000"
              Agent.update(@app, fn state -> Map.put(state, x, info) end)
              :timer.sleep(120000)
              IO.puts "#{inspect(pid)} finished in #{ms}ms"
              Agent.update(@app, fn state -> Map.delete(state, x) end)
          end)
      end
    end

    #### getNotifications
    def getNotifications do
      Agent.get(@app, fn state -> Map.values(state) end)
    end

    def taskReceiver(name) do
      Task.await(name)
    end

  end

  defmodule Central do

    @name __MODULE__

    #### Start the principal agent
    def start do
      Agent.start_link(fn -> {} end,  name: @name)
    end

    #### Add apps and start tasks
    def aggregator(app) do
      Agent.update(@name, fn state ->
        Tuple.append(state, app)
      end)

      Tarea2.App.task(app)
    end

    #### Get all apps
    def getApps do
      Agent.get(@name, fn state -> state end )
    end

  end

  defmodule Driver do
    @mut MutexRegion

    defstruct [name: "John", matricula: 28]

    def aggregator(app) do
      Tarea2.Central.start
      Tarea2.App.start
      Tarea2.Central.aggregator(app)
    end

    def taskReceiver do
      msg = Tarea2.App.getNotifications
    end

    def selectTask do
      taskSelected = String.trim(IO.gets("Ingrese codigo de solicitud a atender: "))
      Tarea2.App.taskReceiver(taskSelected)
      #Mutex.await(@defstruct, mutex2_id)
      #Process.sleep(1000)
      #Mutex.release(@defstruct, mutex1_id)
    end

  end

end

Here the error

iex(4)> Tarea2.Dirver.selectTask  

Ingrese codigo de solicitud a atender: bcdae
** (FunctionClauseError) no function clause matching in Task.await/2    

    The following arguments were given to Task.await/2:

        # 1
        "bcdae"

        # 2
        5000

    Attempted function clauses (showing 1 out of 1):

        def await(-%Task{ref: ref, owner: owner} = task-, timeout) when -timeout == :infinity- or is_integer(timeout) and timeout >= 0

    (elixir 1.13.2) lib/task.ex:794: Task.await/2

Showing Posts 1 to 4

kartheek

kartheek

Task.await expects task to be passed - you are passing string. You may have to map name to a corresponding task.

hernancuy

hernancuy OP


 name = Enum.take_random(alphabet = Enum.to_list(?a..?e), 5)
        :timer.sleep(1000)
        name = Task.async(fn ->
              pid = self()

I don’t know if I’m doing wrong this name creation so, because the idea here is create a name random, and this name gonna be the name task. This is the string that I’m passing in the code that you see.

Do I need to pass the name like an atom ?

Thank you Kartheek :smiley:

kartheek

kartheek

You are welcome @hernancuy .

I don’t think naming a task is possible and your code does not attempt it. It generated name and that name was overwritten by task returned by Task.async.

This is a naive implementation - i only addressed the name to task mapping part of it. Store name against corresponding task which was created in Agent state. Retrieve task for a given name and check if process is alive and await it if it is alive.

defmodule TestTask.App do

  @app Apps

  #### Start agent
  def start do
    Agent.start_link(fn -> %{tasks: %{}, data: %{}} end,  name: @app)
  end

  #### Start tasks from app
  def task(app) do
    for x <- 0..4 do
      name = Enum.take_random(alphabet = Enum.to_list(?a..?e), 5)
      task = 
        Task.async(fn -> 
          pid = self()
          ms = Enum.random(1..1000)
          info = "Codigo #{name} : Hernan Cuy, lugar x lugar y, 20000"
          Agent.update(@app, fn state -> 
            data = Map.put(state.data, x, info) 
            Map.put(state, :data, data)
          end)
          :timer.sleep(120000)
          Agent.update(@app, fn state -> 
            data = Map.delete(state.data, x) 
            tasks = Map.delete(state.tasks, name) 
            state
            |> Map.put( :data, data)
            |> Map.put( :tasks, tasks)
          end)
        end)
      Agent.update(@app, fn state -> 
        tasks = Map.put(state.tasks, name, task)
        Map.put(state, :tasks, tasks) 
      end)
      task
    end
  end

  def get() do
    Agent.get(@app, &(&1))
  end

  #### getNotifications
  def getNotifications do
    Agent.get(@app, fn state -> Map.values(state) end)
  end

  def taskReceiver(name) do
    task = Agent.get(@app, fn state -> Map.get(state.tasks, name) end)
    if task != nil and Process.alive?(task.pid) do
      Task.await(task)
    end
  end
end
iex(1)> alias TestTask.App
TestTask.App
iex(2)> App.start()
{:ok, #PID<0.472.0>}
iex(3)> App.get()
%{data: %{}, tasks: %{}}
iex(4)> App.task(nil)
iex(5)> App.get()
iex(6)> App.taskReceiver(<name>)
hernancuy

hernancuy OP

Oh, I see!!!, thank you my friend, you give me the light and the idea to do that. Thank you !!!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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