cronokirby / alchemy

A discord library for Elixir
MIT License
152 stars 34 forks source link

Unable to run in a Elixir release #91

Open shadyendless opened 4 years ago

shadyendless commented 4 years ago

Hello!

I am trying to get this to work with Elixir releases and I am getting an error when running it. I am not sure if there are additional things I need to configure/include for it to work or not. It works fine when I run in the production environment MIX_ENV=prod iex -S mix, but I get the following error when running the release:

07:44:37.797 [error] Task #PID<0.3055.0> started from GatewayManager terminating
** (MatchError) no match of right hand side value: {:error, {:EXIT, {:undef, [{:websocket_client, :start_link, ["wss://gateway.discord.gg?v=6&encoding=json", Alchemy.Discord.Gateway, %Alchemy.Discord.Gateway.State{seq: nil, session_id: nil, shard: [0, 1], token: "<TOKEN REMOVED FOR SECURITY PURPOSES>", trace: nil, user_id: nil}], []}, {:supervisor, :do_start_child_i, 3, [file: 'supervisor.erl', line: 379]}, {:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 404]}, {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 661]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 690]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}}}
    (alchemy) lib/Discord/Gateway/manager.ex:100: anonymous fn/2 in Alchemy.Discord.Gateway.Manager.handle_cast/2
    (elixir) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Function: #Function<2.19132399/0 in Alchemy.Discord.Gateway.Manager.handle_cast/2>
    Args: []
cronokirby commented 4 years ago

This error seems to have something to do with :websocket_client's code not being loaded correctly in the release. If your bot otherwise works, but fails when making a release, it seems like something like this might be the culprit.

That being said, I'm not an expert on Elixir releases, but I've seen some issues on other projects with things not getting loaded properly.

OvermindDL1 commented 4 years ago

websocket_client

Sounds like alchemy is not adding this application/library to extra_applications in mix.exs?

OvermindDL1 commented 4 years ago

Correction, alchemy is still using the old applications key, the mix.exs needs an overhaul, that needs to be removed, dependencies need to be marked as dev only or all or so, etc... A quick workaround would be to just add websocket_client to the applications key.

Awlexus commented 4 years ago

Came across this issue as well. And found these articles about it: https://www.amberbit.com/blog/2019/8/23/mix-release-and-missing-dependencies/ https://www.amberbit.com/blog/2017/9/22/elixir-applications-vs-extra_applications-guide/

I first encountered this issue with poison, so I did as the article said and added it to my own mix.exs which lead to the error message saying that websocket_client is missing.

I think the best solution would be to delete the :applications key altogether.

frahugo commented 4 years ago

@shadyendless Have you been able to run Alchemy in a release? I can't on my side. The CommandHandler process does not get started, and the application fails starting when trying to add commands. It looks like the client does not start the StageSupervisor.

[15968]: 11:17:28.828 [info] Running Api.Endpoint with cowboy 2.8.0 at :::8080 (http)
[15968]: 11:17:28.829 [info] Access Api.Endpoint at http://foo.local
[15968]: 11:17:29.061 [info] Starting up 1 shards
[15968]: warning: :simple_one_for_one strategy is deprecated, please use DynamicSupervisor instead
[15968]:   (elixir 1.10.3) lib/supervisor.ex:604: Supervisor.init/2
[15968]:   (elixir 1.10.3) lib/supervisor.ex:556: Supervisor.start_link/2
[15968]:   (alchemy 0.6.4) lib/Discord/Gateway/manager.ex:62: Alchemy.Discord.Gateway.Manager.init/1
[15968]:   (stdlib 3.10) gen_server.erl:374: :gen_server.init_it/2
[15968]:   (stdlib 3.10) gen_server.erl:342: :gen_server.init_it/6
[15968]: 11:17:29.063 [info] Shard [0, 1] connecting to the gateway
[15968]: 11:17:32.068 [info] Application discord exited: exited in: Discord.Application.start(:normal, [])
[15968]:     ** (EXIT) exited in: GenServer.call(Alchemy.Cogs.CommandHandler, {:add_commands, Discord.Commands, %{"help" => {Discord.Commands, 0, :help}, "ping" => {Discord.Commands, 0, :ping}, "print" => {Discord.Commands, 1, :print, &List.wrap/1}, "rate" => {Discord.Commands, 2, :rate, &String.split/1}}}, 5000)
[15968]:         ** (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
[15968]: {"Kernel pid terminated",application_controller,"{application_start_failure,discord,{bad_return,{{'Elixir.Discord.Application',start,[normal,[]]},{'EXIT',{noproc,{'Elixir.GenServer',call,['Elixir.Alchemy.Cogs.CommandHandler',{add_commands,'Elixir.Discord.Commands',#{<<\"help\">> => {'Elixir.Discord.Commands',0,help},<<\"ping\">> => {'Elixir.Discord.Commands',0,ping},<<\"print\">> => {'Elixir.Discord.Commands',1,print,fun 'Elixir.List':wrap/1},<<\"rate\">> => {'Elixir.Discord.Commands',2,rate,fun 'Elixir.String':split/1}}},5000]}}}}}}"}
[15968]: Kernel pid terminated (application_controller) ({application_start_failure,discord,{bad_return,{{'Elixir.Discord.Application',start,[normal,[]]},{'EXIT',{noproc,{'Elixir.GenServer',call,['Elixir.Alche

In my mix.exs:

  def application do
    [
      extra_applications: [:logger, :websocket_client],
      mod: {Discord.Application, []}
    ]
  end

My application module:

defmodule Discord.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do
    if discord_active?() do
      run = Alchemy.Client.start(discord_token())
      load_modules()
      # We supply this to satisfy the application callback
      run
    else
      opts = [strategy: :one_for_one, name: Discord.Supervisor]
      Supervisor.start_link([], opts)
    end
  end

  defp discord_active? do
    :discord
    |> Application.get_env(:active, false)
    |> WannabeBool.to_boolean()
  end

  defp discord_token do
    Application.fetch_env!(:discord, :token)
  end

  defp load_modules do
    use Discord.Commands
  end
end

DIscord.Commands:

defmodule Discord.Commands do
  use Alchemy.Cogs

  alias Alchemy.Embed
  require Alchemy.Embed
.
.
.
frahugo commented 4 years ago

FYI, the issue I reported above was with version 0.6.4. Version 0.6.5 fixes the issue.

vendethiel commented 3 years ago

Hey @frahugo , are you still working on your bot with Alchemy, and is it working just fine Alchemy 0.6.9? I'm getting a similar

[15968]: 11:17:32.068 [info] Application discord exited: exited in: Discord.Application.start(:normal, [])
[15968]:     ** (EXIT) exited in: GenServer.call(Alchemy.Cogs.CommandHandler, {:add_commands, Discord.Commands, %{"help" => {Discord.Commands, 0, :help}, "ping" => {Discord.Commands, 0, :ping}, "print" => {Discord.Commands, 1, :print, &List.wrap/1}, "rate" => {Discord.Commands, 2, :rate, &String.split/1}}}, 5000)
[15968]:         ** (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

using boilerplate

frahugo commented 3 years ago

@vendethiel I'm still on 0.6.6 (which works). I'll try later with 0.6.9. Will let you know.

Hey @frahugo , are you still working on your bot with Alchemy, and is it working just fine Alchemy 0.6.9? I'm getting a similar

[15968]: 11:17:32.068 [info] Application discord exited: exited in: Discord.Application.start(:normal, [])
[15968]:     ** (EXIT) exited in: GenServer.call(Alchemy.Cogs.CommandHandler, {:add_commands, Discord.Commands, %{"help" => {Discord.Commands, 0, :help}, "ping" => {Discord.Commands, 0, :ping}, "print" => {Discord.Commands, 1, :print, &List.wrap/1}, "rate" => {Discord.Commands, 2, :rate, &String.split/1}}}, 5000)
[15968]:         ** (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

using boilerplate

vendethiel commented 3 years ago

Actually, even trying to run Viviani gives me the same error, so I must be doing something wrong elsewhere.