1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

カスタムmixタスクからgothを使うと怒られる問題

Last updated at Posted at 2020-07-28

ElixirにはRubyのRakeのようなツールとしてmixが提供されています。
mixもRakeのようにカスタムタスクを定義できるのですが一部のライブラリを使ったときGenServerのプロセスが起動されておらずエラーになったのでその対策を書きます。

GenServerが動いてない

今回は適当なファイル lib/mix/tasks/oath.exgothの認証コードを書いたということで話を進めます。

defmodule Mix.Tasks.Oath do
  use Mix.Task

  @shortdoc "認証実験"
  def run(_) do
    {:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform.read-only")
    IO.inspect GoogleApi.BigQuery.V2.Connection.new(token.token)
  end
end

これを実行するとこんなエラーが出ます(´;ω;`)


> mix oath
Compiling 4 files (.ex)
Generated HOGE app
** (exit) exited in: GenServer.call(Goth.TokenStore, {:find, {:default, "https://www.googleapis.com/auth/cloud-platform.read-only", nil}}, 5000)
    ** (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
    (elixir 1.10.4) lib/gen_server.ex:1013: GenServer.call/3
    lib/goth/token.ex:71: Goth.Token.for_scope/2
    lib/mix/tasks/oath.ex:6: Mix.Tasks.Oath.run/1
    (mix 1.10.4) lib/mix/task.ex:330: Mix.Task.run_task/3
    (mix 1.10.4) lib/mix/cli.ex:82: Mix.CLI.run_task/2

mix.exsの def application:gothを書いてみても同じエラーが出てしまいます...

解決策

関数の最初に Application.ensure_all_started(:goth)を書くとエラーが出なくなる


def run(_) do
  Application.ensure_all_started(:goth)
  # ~~~~~~~~~~~~~~~


> mix oath
Compiling 1 file (.ex)
%Tesla.Client{
  adapter: nil,
  fun: nil,
  post: [],
  pre: [
    {Tesla.Middleware.Headers, :call,
     [
       [
         {"authorization",
          "Bearer ~~~~~
       ]
     ]}
  ]
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?