fireproofsocks
Starting Mongo inside a mix Task
I need to run some commands on a Mongo database from within a custom mix task. I’m using the mongodb driver.
The task module looks like this:
defmodule Mix.Tasks.Mongo.Stuff do
use Mix.Task
@shortdoc "Does stuff to mongo..."
def run(_) do
{:ok, _pid} = Mongo.start_link(Application.get_env(:db, DocDb))
Mongo.find_one(:docdb, "the_collection", %{id: 456})
end
end
and the relevant bit of the config looks like this:
config :db, DocDb,
url: "mongodb://localhost:27017/docdb_dev",
name: :docdb
This seems to get Mongo started (?) but it generates a spooky error:
mix mongo.stuff
Compiling 1 file (.ex)
** (EXIT from #PID<0.92.0>) an exception was raised:
** (ArgumentError) argument error
:erlang.!(Mongo.Events, {:notify, %Mongo.Events.TopologyOpeningEvent{topology_pid: #PID<0.305.0>}})
(stdlib) gen_event.erl:269: :gen_event.send/2
lib/mongo/topology.ex:70: Mongo.Topology.init/1
(stdlib) gen_server.erl:374: :gen_server.init_it/2
(stdlib) gen_server.erl:342: :gen_server.init_it/6
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
I’m not even sure what I’m looking at. Can someone help set me straight? Thanks!
Most Liked
NobbZ
mix tasks don’t start your or any other applications. When you need some, you need to start them manually using Application.ensure_all_started/1.
3
Popular in Questions
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









