This is an adapter for GenQueue to enable functionaility with Exq.
The package can be installed by adding gen_queue_exq to your list of dependencies in mix.exs:
defdepsdo[{:gen_queue_exq,"~> 0.1.3"}]endSee HexDocs for additional documentation.
Before starting, please refer to the Exq documentation
for details on configuration. This adapter handles zero Exq related config.
We can start off by creating a new GenQueue module, which we will use to push jobs to
Exq.
defmoduleEnqueuerdouseGenQueue,otp_app: :my_appendOnce we have our module setup, ensure we have our config pointing to the GenQueue.Adapters.Exq
adapter.
config:my_app,Enqueuer,[adapter: GenQueue.Adapters.Exq]By default, gen_queue_exq does not start Exq on application start. So we must add
our new Enqueuer module to our supervision tree.
children=[supervisor(Enqueuer,[]),]Jobs are simply modules with a perform method.
defmoduleMyJobdodefperform(arg1)doIO.inspect(arg1)endendWe can now easily enqueue jobs to Exq. The adapter will handle a variety of argument formats.
# Push MyJob to "default" queue{:ok,job}=Enqueuer.push(MyJob)# Push MyJob to "default" queue{:ok,job}=Enqueuer.push({MyJob})# Push MyJob to "default" queue with "arg1"{:ok,job}=Enqueuer.push({MyJob,"arg1"})# Push MyJob to "default" queue with no args{:ok,job}=Enqueuer.push({MyJob,[]})# Push MyJob to "default" queue with "arg1" and "arg2"{:ok,job}=Enqueuer.push({MyJob,["arg1","arg2"]})# Push MyJob to "foo" queue with "arg1"{:ok,job}=Enqueuer.push({MyJob,"arg1"},[queue: "foo"])# Schedule MyJob to "default" queue with "arg1" in 10 seconds{:ok,job}=Enqueuer.push({MyJob,"arg1"},[delay: 10_000])# Schedule MyJob to "default" queue with "arg1" at a specific timedate=DateTime.utc_now(){:ok,job}=Enqueuer.push({MyJob,"arg1"},[delay: date])Optionally, we can also have our tests use the GenQueue.Adapters.MockJob adapter.
config:my_app,Enqueuer,[adapter: GenQueue.Adapters.MockJob]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 redis.
defmoduleMyJobTestdouseExUnit.Case,async: trueimportGenQueue.Testsetupdosetup_test_queue(Enqueuer)endtest"my enqueuer works"do{:ok,_}=Enqueuer.push(Job)assert_receive(%GenQueue.Job{module: Job,args: []})endendIf your jobs are being enqueued outside of the current process, we can use named processes to recieve the job. This wont be async safe.
importGenQueue.Testsetupdosetup_global_test_queue(Enqueuer,:my_process_name)end