que icon indicating copy to clipboard operation
que copied to clipboard

How to properly shutdown app with Mnesia backed queue

Open raulintosh opened this issue 7 years ago • 10 comments

Hello @sheharyarn, I have an phoenix app that use a lot of Que Workers to process events data. I has configured Mnesia to store the queue, every time that I need to restart the app I need to delete the mnesia tables and run Que.Persistence.Mnesia.setup! again. Do yo know which procedure I must follow to stop Que properly.

Elixir 1.5.1 Phoenix 1.3.0 Que 0.4.1

Best regards. Raul

raulintosh avatar Oct 09 '17 23:10 raulintosh

Hi @raulintosh, This sounds like a weird issue since this shouldn't happen at all. Is this happening on dev or prod? Can you give more details about your environment (like OS name and version, erlang version, release manager that you're using, etc.)?

Also, are you able to reproduce this issue on a fresh phoenix app keeping all the other things same?

sheharyarn avatar Oct 23 '17 17:10 sheharyarn

I'm having what I believe is the same issue, in that if the application isn't shut down properly I get this error on attempting to restart it. Removing the mnesia tables and running setup again will fix the problem, but concerns me because I could potentially lose failed jobs.

** (Mix) Could not start application que: Que.start(:normal, []) returned an error: shutdown: failed to start child: Que.ServerSupervisor
    ** (EXIT) an exception was raised:
        ** (Protocol.UndefinedError) protocol Enumerable not implemented for :badarg
            (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
            (elixir) lib/enum.ex:116: Enumerable.reduce/3
            (elixir) lib/enum.ex:1776: Enum.map/2
            (que) lib/que/server_supervisor.ex:73: Que.ServerSupervisor.resume_queued_jobs/0
            (que) lib/que/server_supervisor.ex:24: Que.ServerSupervisor.start_link/0
            (stdlib) supervisor.erl:365: :supervisor.do_start_child/2
            (stdlib) supervisor.erl:348: :supervisor.start_children/3
            (stdlib) supervisor.erl:314: :supervisor.init_children/2
            (stdlib) gen_server.erl:328: :gen_server.init_it/6
            (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

That line 73 on the server supervisor is trying to enumerate over the incomplete jobs but something's going wrong there.

carterbryden avatar Jan 19 '18 09:01 carterbryden

Hi Carter,

Can you please describe the steps I can take to reproduce this issue? Please also describe the details of your environment (OS name and version, Elixir/Erlang version and prod/dev environment, etc.).

sheharyarn avatar Jan 19 '18 09:01 sheharyarn

Elixir 1.5.1 Phoenix 1.3.0 Que 0.4.1 Dev environment ubuntu 14.04

The easiest way is to ctrl-c then ctrl-k after mix phx.server, but I also had the issue when a config file was changed while phoenix was running (it errors out, saying you need to restart).

I did just find that if I start a separate iex -S mix on the same project, and run Que.ServerSupervisor.start_link it returns:

iex(1)> Que.ServerSupervisor.start_link
[info] [Que] Booting Server Supervisor for Workers
{:error, {:already_started, #PID<0.386.0>}}

And then the next time I try running mix phx.server it will start up OK. Not sure why.

carterbryden avatar Jan 19 '18 10:01 carterbryden

having same problem on start:

    ** (EXIT) an exception was raised:
        ** (Amnesia.TableExistsError) Table Que.Persistence.Mnesia.DB already exists
            (amnesia) lib/amnesia/table.ex:138: Amnesia.Table.create!/2
            (que) lib/que/persistence/mnesia.ex:128: Que.Persistence.Mnesia.DB.create!/1
            (que) lib/que/persistence/mnesia.ex:107: Que.Persistence.Mnesia.setup!/1

if i add to the configurations of my app: config :mnesia, dir: '#{:os.system_time :second}/path/to/some/folder'' problem will be solved since another folder will be created, but old folder will never be used again with all it's content

AsharDweedar avatar Jul 31 '18 09:07 AsharDweedar

Hi @AsharDweedar, your issue seems different. What Elixir/OTP/OS versions are you on?

sheharyarn avatar Jul 31 '18 17:07 sheharyarn

Elixir (1.6.6) , Erlang/OTP 20, ubuntu 16.04

AsharDweedar avatar Aug 01 '18 07:08 AsharDweedar

@AsharDweedar it looks like you're calling either mix que.setup or Que.Persistence.Mnesia.setup! multiple times. Just once is enough to create the database.

sheharyarn avatar Aug 01 '18 19:08 sheharyarn

i have this in my application.ex:

  Que.Persistence.Mnesia.setup!()

I have to remove the file at each start of the App

AsharDweedar avatar Aug 02 '18 08:08 AsharDweedar

Yes, and that's the issue here. That is supposed to be called only once when setting up production databases. Since you put it in Application, it tries to create the database every time the application starts which causes an exception. Just remove the line and it should be fine.

sheharyarn avatar Aug 02 '18 14:08 sheharyarn