DevLog.at

Phoenix Friction

Had to figure out where to put custom mix tasks. First you need a folder lib/mix/tasks. Then put a file in there such as my.hello.ex that looks like this:

defmodule Mix.Tasks.My.Hello do
  use Mix.Task

  @shortdoc "An example custom mix task"

  def run(_) do
    IO.puts "Hello, task."
  end
end

Then you can run your task in the command line with mix my.hello

12d

Apparently if you try to use Ecto in a custom task (per previous post), you'll get an error like this:

** (RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist

According to this comment you need to run this within your task:

Mix.Task.run "app.start"

Supposedly you can run MyApp.Repo.start_link(...) instead of loading the entire app, but that just throws the following error:

** (EXIT from #PID<0.92.0>) shutdown: failed to start child: DBConnection.ConnectionPool
    ** (EXIT) exited in: GenServer.call(DBConnection.Watcher, {:watch, DBConnection.ConnectionPool.Supervisor, {DBConnection.ConnectionPool.Pool, {#PID<0.238.0>, #Reference<0.3272131709.2804285441.114278>, Postgrex.Protocol, [types: Postgrex.DefaultTypes, port: 5432, repo: Myapp.Repo, telemetry_prefix: [:myapp, :repo], otp_app: :myapp, timeout: 15000, username: "postgres", password: "postgres", database: "vaporware_dev", hostname: "localhost", show_sensitive_data_on_connection_error: true, pool_size: 10, pool: DBConnection.ConnectionPool]}}}, :infinity)
        ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

12d

How to make HTTP requests in Phoenix / Elixir? There doesn't seem to be any official way.

A library I've installed uses HTTPoison, which uses hackney under the hood. So that's available in my project, apparently.

On the other hand, this great blog post states – while praising hackney – that there's a build-in :httpc that you can use for simple requests.

But on the other hand, that article is a bit outdated, as there's now a new low-level library called mint, as announced by an official elixir blog post.

Ooh, an official blog post! That means it's the one, right?

Not quite. From the end of the blog post:

Note: Mint is being announced in the official Elixir blog because it was originally being considered for inclusion in Elixir itself. However, at some point the Elixir team decided it doesn’t make sense to include an HTTP client in Elixir itself, at least as long as Erlang/OTP ships with a client too. Mint is not maintained by the Elixir team, although it is maintained by Eric and Andrea, who are part of the team.

Takeaway?

If you know me, you know I'm generally averse to complex dependencies. So, I'll probably try using :httpc, and upgrading to hackney if that proves too limiting.

12d

If you use :httpc, create your url with single quotes and not double quotes!

12d

Welcome to DevLog

The open thought platform for developers.

Share your work as you work on it. Easier than a blog, handier than Twitter.

Sign in and write down valuable thoughts that would otherwise be forgotten.