0
votes

I'm making a task that get an json in an api and insert in MongoDb. I'm using phoenix and Mongodb_Ecto.

I have a model Group and this code in a controller works like a charm:

HTTPoison.start
group = %Group{ param1: "value", param2: "value" } |> Repo.insert!

But,in a task I don't have the Repo defmodule. I tried to make this:

HTTPoison.start
group = %Group{ param1: "value", param2: "value"} |> MyApp.Repo.insert!

Using MyApp.Repo instead of only Repo.

I'm receiving this error:

** (exit) exited in: :gen_server.call(MyApp.Repo.Pool, {:checkout, #Reference<0.0.1.11>, true}, 5000)
** (EXIT) no process
         :erlang.send(MyApp.Repo.Pool, {:"$gen_cast", {:cancel_waiting, #Reference<0.0.1.11>}}, [:noconnect])
(stdlib) gen_server.erl:416: :gen_server.do_send/2
(stdlib) gen_server.erl:232: :gen_server.do_cast/2
         src/poolboy.erl:58: :poolboy.checkout/3
(stdlib) timer.erl:197: :timer.tc/3
         lib/mongo/pool/poolboy.ex:33: Mongo.Pool.Poolboy.run/2
         lib/mongo/pool.ex:142: Mongo.Pool.run_with_log/5
         lib/mongo.ex:220: Mongo.insert_one/4
         lib/mongo_ecto/connection.ex:124: Mongo.Ecto.Connection.catch_constraint_errors/1
         lib/mongo_ecto.ex:522: Mongo.Ecto.insert/6
         lib/ecto/repo/model.ex:253: Ecto.Repo.Model.apply/4
         lib/ecto/repo/model.ex:83: anonymous fn/10 in Ecto.Repo.Model.do_insert/4
         lib/ecto/repo/model.ex:14: Ecto.Repo.Model.insert!/4

How can I access Repo.insert in the correct way to save my data in mongoDb?

Thanks for this.

1
Try adding this above HTTPoison.start: :application.ensure_all_started(:my_app) (put in your OTP app's name instead of :my_app).Dogbert
Thanks for this man! Works like a charm =DRenato Cassino

1 Answers

0
votes

Your application (and therefore your database pool) is not started automatically in a Mix task. You can start it manually by adding the following:

:application.ensure_all_started(:my_app)

If :httpoison is listed in your Mixfile's :applications, you don't need to do HTTPoison.start anymore as the above line will ensure :httpoison is started before :my_app.