GenQueue OPQ

Build Status GenQueue OPQ Version

This is an adapter for GenQueue to enable functionaility with OPQ.

Installation

The package can be installed by adding gen_queue_opq to your list of dependencies in mix.exs:

def deps do
  [
    {:gen_queue_opq, "~> 0.1.0"}
  ]
end

Documentation

See HexDocs for additional documentation.

Configuration

Before starting, please refer to the OPQ documentation for details on configuration. This adapter handles zero OPQ related config.

Creating Enqueuers

We can start off by creating a new GenQueue module, which we will use to push jobs to OPQ.

defmodule Enqueuer do
  use GenQueue, otp_app: :my_app
end

Once we have our module setup, ensure we have our config pointing to the GenQueue.Adapters.OPQ adapter.

config :my_app, Enqueuer, [
  adapter: GenQueue.Adapters.OPQ
]

Starting Enqueuers

By default, gen_queue_opq does not start OPQ on application start. So we must add our new Enqueuer module to our supervision tree.

  children = [
    supervisor(Enqueuer, []),
  ]

Creating Jobs

Jobs are simply modules with a perform method.

defmodule MyJob do
  def perform(arg1) do
    IO.inspect(arg1)
  end
end

Enqueuing Jobs

We can now easily enqueue jobs to OPQ. The adapter will handle a variety of argument formats.

# Push MyJob to queue
{:ok, job} = Enqueuer.push(MyJob)

# Push MyJob to queue
{:ok, job} = Enqueuer.push({MyJob})

# Push MyJob to queue with "arg1"
{:ok, job} = Enqueuer.push({MyJob, "arg1"})

# Push MyJob to queue with no args
{:ok, job} = Enqueuer.push({MyJob, []})

# Push MyJob to queue with "arg1" and "arg2"
{:ok, job} = Enqueuer.push({MyJob, ["arg1", "arg2"]})

Testing

Optionally, we can also have our tests use the GenQueue.Adapters.OPQMock adapter.

config :my_app, Enqueuer, [
  adapter: GenQueue.Adapters.OPQMock
]

This mock adapter uses the standard GenQueue.Test helpers to send the job payload back to the current processes mailbox (or another named process) instead of actually enqueuing the job to OPQ.

defmodule MyJobTest do
  use ExUnit.Case, async: true

  import GenQueue.Test

  setup do
    setup_test_queue(Enqueuer)
  end

  test "my enqueuer works" do
    {:ok, _} = Enqueuer.push(MyJob)
    assert_receive({Job, [], %{}})
  end
end

If your jobs are being enqueued outside of the current process, we can use named processes to recieve the job. This wont be async safe.

import GenQueue.Test

setup do
  setup_global_test_queue(Enqueuer, :my_process_name)
end