Cristian Bica

Software Engineer

I’m Cristian Bica, a software engineer working for in/PACT. I'm working with ruby, rails, web frontend, iOS, Objective-C / Swift.


Howto: Using ActiveJob with queue_classic

Here's how you can use activejob with a queue_classic backend. queue_classic is a queueing backend which users Postgresql as storage (and other stuff like listen/notify) so you'll need a Postgresql server so you can use queue_classic.

1. Create a new rails app (skip this if you're adding support for an existing app).

$ rails new activejob-queue_classic

2. Setup queue_classic.

Add the queue_classic gems to the Gemfile and run bunde install:

$ bundle install
Resolving dependencies...  
...
Using pg 0.18.1  
Using queue_classic 3.1.0  
Using queue_classic-later 0.3.0  
...
Bundle complete! 15 Gemfile dependencies, 57 gems now installed.  
Use `bundle show [gemname]` to see where a bundled gem is installed.  

To make this sample simpler I'm going to user Postgresql as ActiveRecord adapter also. Setup your database.yml to use postgresql adapter:

Then run rake db:create to create the database:

rake db:create  

Setup queue_classic:

# install queue_classic migrations
$ rails generate queue_classic:install
      create  db/migrate/20150506065142_add_queue_classic.rb
      create  db/migrate/20150506065143_update_queue_classic_3_0_0.rb
      create  db/migrate/20150506065144_update_queue_classic_3_0_2.rb
      create  db/migrate/20150506065145_update_queue_classic_3_1_0.rb
# run the migrations
$ rake db:migrate
== 20150506065142 AddQueueClassic: migrating ==================================
== 20150506065142 AddQueueClassic: migrated (0.0884s) =========================

== 20150506065143 UpdateQueueClassic300: migrating ============================
== 20150506065143 UpdateQueueClassic300: migrated (0.0155s) ===================

== 20150506065144 UpdateQueueClassic302: migrating ============================
== 20150506065144 UpdateQueueClassic302: migrated (0.0024s) ===================

== 20150506065145 UpdateQueueClassic310: migrating ============================
== 20150506065145 UpdateQueueClassic310: migrated (0.0026s) ===================

3. Tell ActiveJob to use queue_classic

You'll have to add config.active_job.queue_adapter = :queue_classic in your rails application configuration (application.rb, or development.rb/production.rb).

4. Generate a job with rails generate job test and make the job log something

Now let's run a job:

$ rails runner "TestJob.perform_later(1,2,3)" 
[ActiveJob] Enqueued TestJob (Job ID: 041fbcfa-0694-4e69-8334-6f095c8b99f4) to QueueClassic(default) with arguments: 1, 2, 3

We see the job was enqueued but it wasn't run. That's because we need to start a queue_classic worker to run the job.

5. Starting a queue_classic worker

To start a queue_classic worker run bundle exec rake qc:work:

$ bundle exec rake qc:work

And finally let's enqueue a job and see what happens:

$ $ rails runner "TestJob.perform_later(1,2,3)" && sleep 3 && tail -n 4 log/development.log
[ActiveJob] Enqueued TestJob (Job ID: 988478bc-4e04-47b7-9922-565a64641275) to QueueClassic(default) with arguments: 1, 2, 3
[ActiveJob] [TestJob] [988478bc-4e04-47b7-9922-565a64641275] Performing TestJob from QueueClassic(default) with arguments: 1, 2, 3
[ActiveJob] [TestJob] [988478bc-4e04-47b7-9922-565a64641275] TestJob: I'm performing my job with arguments: [1, 2, 3]
[ActiveJob] [TestJob] [988478bc-4e04-47b7-9922-565a64641275] Performed TestJob from QueueClassic(default) in 0.16ms

... the job was performed :). Source code of a full app with the above implementation can be found on github: https://github.com/cristianbica/activejob-queue_classic

comments powered by Disqus