None of those bellow works…
Process.put(:"$callers", [caller])
Sandbox.allow(Repo, caller, target)
None of those bellow works…
Process.put(:"$callers", [caller])
Sandbox.allow(Repo, caller, target)
can you explain to me why this is a genserver? Nuvemx.RecommenderSystems.SimilarityServer
It looks like this genserver does not hold any important state. Why not just make it a module? Am I missing something?
It was a simple function…but rabbit spawn a new process for that function behind the scenes so it ends up being the same problem.
I want to get the argument of this rabbitmq function and do a database operation on it… just it.
I created a genserver just to allow it to access the database, but it doesn’t work either
Thank you guys!
I was able to solve it by thinking another approach. I decided to “await for messages” synchronously by using a recursive function. Since this functions runs inside a “Task” it has access to the database automatically… and it’s working.
defp consume(channel, queue, attempts \\ 1) do
if attempts <= 30 do
AMQP.Basic.get(channel, queue)
|> case do
{:empty, _} ->
:timer.seconds(10 * attempts)
|> Process.sleep()
consume(channel, queue, attempts + 1)
{:ok, payload, _meta} ->
AMQP.Queue.delete(channel, queue)
AMQP.Channel.close(channel)
similarities = Jason.decode!(payload)
update_similarities(similarities)
_ ->
{:error, "Rabbit is crazy"}
end
else
{:error, "Rabbit is offline"}
end
end
This code looks much nicer and easier to rationalize!!