edgurgel / verk

A job processing system that just verks! 🧛‍
https://hex.pm/packages/verk
MIT License
723 stars 65 forks source link

How to restore dynamically added queues #117

Closed salisbury-espinosa closed 7 years ago

salisbury-espinosa commented 7 years ago

How to restore queues with data from redis after the application restart, which have been added dynamically Verk.add_queue(:new, 10) At the initial startup, the supervisor only reads the data from the config https://github.com/edgurgel/verk/blob/062811eeb706133bd5d55e85cd36c017a7ee2da7/lib/verk/supervisor.ex#L19

edgurgel commented 7 years ago

Verk does not saves the queues that were started dynamically. The initial configuration should have them or when Verk starts, add the queues back again dynamically.

One common use case is to start a supervision tree with the Verk.Supervisor then a process that adds the dynamic queues when it starts. So if the supervision tree restarts it will guarantee that the queues were added. I can add more information and examples around this if needed.

salisbury-espinosa commented 7 years ago

I can add more information and examples around this if needed.

I would be very grateful to you)

edgurgel commented 7 years ago

You could for example have a supervisor that has the following supervision tree:

tree = [supervisor(Verk.Supervisor, []), worker(QueuesLoader, [])]
opts = [name: MySupervisor, strategy: :rest_for_one]
Supervisor.start_link(tree, opts)

The QueuesLoader can be a GenServer that looks like this:

defmodule QueuesLoader do
  use GenServer
  require Logger

  @doc false
  def start_link do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  @doc false
  def init(_args) do
    for queues <- MyQueues.all do
      Verk.add_queue(...)
    end
    { :ok, nil }
  end
end

Notice that this supervisor is a rest_for_one so that if Verk.Supervisor crashes, it will restart the QueuesLoader worker as it's defined after the Verk.Supervisor.