Code Monkey home page Code Monkey logo

Comments (7)

josevalim avatar josevalim commented on May 25, 2024 1

@michelson when in doubt, always add a catch all and see what it looks like:

receive do
  whatever -> IO.inspect whatever
end

:)

from phoenix_pubsub.

josevalim avatar josevalim commented on May 25, 2024

Can you add {:ok, _} = Code.ensure_loaded(Enumerable.MapSet) to def start/2 in your application callback (usually in "lib/my_app.ex") and let us know if the error disappears?

Also please double check you are running in production environment.

This is not a Phoenix bug per se. We have had reports of something similar happening but we could not yet reproduce it.

from phoenix_pubsub.

michelson avatar michelson commented on May 25, 2024

Hello @josevalim ,

thanks for your quick reply, I've tunned some sysctl settings on my machine and this error disappeared among other errors.
Also note that I'm using this plugin with a mix application, not a phoenix application. it seems This kinds of errors are raised when process reaches system limits on file descriptors

I'm trying to implement a long polling application which each request is tracked with Tracker and each req starts a PubSub subscriber, if subscriber got message the response is released to the client. (i'm using task async/yield which blocks requests until got response)
I would like to know if this is the correct way to do it, it will scale well ?

  get "/api/process/:key" do
    key2 = key |> String.replace(".json", "")
    Phoenix.Tracker.track(Notify.Tracker, self(), "devices", key2, %{stat: "CONNECTED"})
    proc = self()
    Phoenix.PubSub.subscribe Notify.PubSub, key2

    task = Task.Supervisor.async_nolink(NotifyWeb.TaskSupervisor, fn ->
      try do
        check_process(proc)
      catch
        :exit, _ -> {:error}
      end
    end)


    conn = put_resp_header(conn, "content-type", "application/json")

    # await for task to return, will also return exit process
    case Task.yield(task, 60000) do
      [] -> send_resp(conn, 200, "[]")
      {:ok, res3}   -> 
        case res3 do
          {:ok, [actions: str]} -> deliver_actions(key2, conn, 200, str)
          {:ok, _} -> send_resp(conn, 200, "[]")
        end
          
      {:exit, _}    -> send_resp(conn, 200, "[]")
      {:error, _}   -> send_resp(conn, 200, "[]")
      nil           -> send_resp(conn, 200, "[]")
      _             -> send_resp(conn, 200, "[]")

    end

  end

  def deliver_actions(key, conn, status, str) do
    send_resp(conn, status, str)
  end

  # recursive function to respond when got data
  def check_process(proc) do
    :timer.sleep(1000)
    case Process.info(proc)[:messages] do
      [] -> check_process(proc) 
      {:ok, [actions: res]} -> {:ok, [actions: res]}
      val -> check_process(proc)
    end
  end

from phoenix_pubsub.

josevalim avatar josevalim commented on May 25, 2024

thanks for your quick reply, I've tunned some sysctl settings on my machine and this error disappeared among other errors.

Good call.

I would like to know if this is the correct way to do it, it will scale well ?

Using Process.info(prod)[:messages] and :timer.sleep(1000) are usually a no-no. You want the task process to instead send you a message instead of looking into its inbox at every time interval.

In fact, it seems like you don't need the task at all. You can subscribe directly from the request process and use receive to await for the message and use after for timeout.

from phoenix_pubsub.

michelson avatar michelson commented on May 25, 2024

Thanks José , I will investigate how can I implement that

best regards

from phoenix_pubsub.

michelson avatar michelson commented on May 25, 2024

You can subscribe directly from the request process and use receive to await for the message and use after for timeout.

having this Process.info(self)[:messages] to read messages, how can I use receive to listen for new messages ?

I'm trying this:

Phoenix.PubSub.subscribe Notify.PubSub, key

receive do
  {:ok, [actions: res]} -> deliver_actions(key, conn, res)
  {msg} -> IO.inspect msg
  after
  60_000 -> send_resp(conn, 200, "[d]")
end

but I only get the after response

from phoenix_pubsub.

michelson avatar michelson commented on May 25, 2024

forget it , It was my mistake on the match handling

thanks

from phoenix_pubsub.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.