Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add Delayed::Job #166

Closed
wants to merge 6 commits into from

3 participants

@croaky
Owner
  • Use style-guide-appropriate database migration.
  • Configure RSpec request specs to run jobs immediately.
  • Run Delayed::Job workers via the Procfile.
@mike-burns
Owner

I've been using Sidekiq on client apps for performance reasons. Do we officially support DJ instead?

templates/background_jobs_rspec.rb
@@ -0,0 +1,8 @@
+module BackgroundJobs
+ def run_background_jobs_immediately
+ delay_jobs = Delayed::Worker.delay_jobs
+ Delayed::Worker.delay_jobs = false
+ yield
+ Delayed::Worker.delay_jobs = delay_jobs
@jferris Owner
jferris added a note

I assume you want this in an ensure block, as yield will raise an exception if the example fails.

@croaky Owner
croaky added a note

Thanks, @jferris. Done in 2b1e6d1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jferris
Owner

Delayed Job has been around forever, has good integration with Heroku and Newrelic, doesn't require a new database to work, and uses processes instead of threads. Although there are situations where Resque/something else may make sense, I think Delayed Job is an excellent default.

Sidekiq requires Redis, I don't know what its integrations with Heroku and Newrelic are like, and it uses threads instead of processes, which are always dangerous on Ruby. Most Ruby libraries aren't written with threads in mind, and with all the malarky with global state that goes on, I'd be surprised if the average Rails application weren't riddled with race conditions.

lib/suspenders/app_builder.rb
@@ -147,6 +147,24 @@ def configure_rspec
inject_into_class 'config/application.rb', 'Application', config
end
+ def configure_background_jobs_for_rspec
+ config = <<-RUBY
+ config.around(:each, type: :feature) do |example|
@jferris Owner
jferris added a note

You can use multiple RSpec.configure blocks, so you could put this block in the background_jobs_rspec.rb template and avoid the Ruby-within-Ruby mind-job.

@croaky Owner
croaky added a note

Thanks, @jferris. Done in 7be3716.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
croaky added some commits
@croaky croaky Add Delayed::Job
* Use style-guide-appropriate database migration.
* Configure RSpec request specs to run jobs immediately.
* Run `Delayed::Job` workers via the `Procfile`.
8e24418
@croaky croaky Ensure delay_jobs is reset when example fails 2b1e6d1
@croaky croaky Avoid Ruby-within-Ruby mind job 7be3716
@croaky
Owner

Ready for another review.

templates/20130206043749_create_delayed_jobs.rb
@@ -0,0 +1,22 @@
+class CreateDelayedJobs < ActiveRecord::Migration
@jferris Owner
jferris added a note

Is there a reason to include this file instead of running rails generate delayed_job:active_record?

By running the generator, we pick up any upstream changes automatically.

@croaky Owner
croaky added a note

The differences between this version and the version in delayed_job_active_record are:

  • priority, attempts, and handler have no null constraint in the gem.
  • timestamps are listed first (consistency thing in db/schema.rb)
  • Ruby 1.9 syntax (style that could be done by the user after generating the app if they wanted).
  • No comments stretching to 151 characters; no comments at all in this version.

Agree there's a big advantage to using the generator. I just submitted collectiveidea/delayed_job_active_record#52 to improve data integrity with the null: false option.

Looks like they support Ruby 1.8.7 so the Ruby 1.9 hash syntax won't fly.

@croaky Owner
croaky added a note

My pull request was merged into delayed_job_active_record so I've created a follow-up commit to Suspenders here: a280381

Thinking of waiting a few days to see if they release a new gem version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jferris
Owner

I think your changes in suspenders look good as-is. I assume that they'll release a new version eventually, so I'd say it's good to run the generator when we create new suspenders apps, and we'll pick up the benefits when delayed_job releases a new version.

@croaky croaky referenced this pull request
Closed

Add Delayed::Job #134

@croaky
Owner

I think your changes in suspenders look good as-is.

Thanks, @jferris. Merged.

@croaky croaky closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 8, 2013
  1. @croaky

    Add Delayed::Job

    croaky authored
    * Use style-guide-appropriate database migration.
    * Configure RSpec request specs to run jobs immediately.
    * Run `Delayed::Job` workers via the `Procfile`.
  2. @croaky
  3. @croaky
  4. @croaky

    Use Delayed::Job generator

    croaky authored
    The NULL constraints were added in:
    
    collectiveidea/delayed_job_active_record#52 (comment)
  5. @croaky
Commits on Apr 12, 2013
  1. @croaky

    Fewer acronyms

    croaky authored
This page is out of date. Refresh to see the latest.
View
5 lib/suspenders/app_builder.rb
@@ -147,6 +147,11 @@ def configure_rspec
inject_into_class 'config/application.rb', 'Application', config
end
+ def configure_background_jobs_for_rspec
+ copy_file 'background_jobs_rspec.rb', 'spec/support/background_jobs.rb'
+ run 'rails g delayed_job:active_record'
+ end
+
def blacklist_active_record_attributes
replace_in_file 'config/application.rb',
'config.active_record.whitelist_attributes = true',
View
1  lib/suspenders/generators/app_generator.rb
@@ -59,6 +59,7 @@ def setup_test_environment
build :test_factories_first
build :generate_rspec
build :configure_rspec
+ build :configure_background_jobs_for_rspec
build :enable_database_cleaner
build :configure_capybara_webkit
build :setup_guard_spork
View
2  suspenders.gemspec
@@ -19,7 +19,7 @@ rush to build something amazing; don't use it if you like missing deadlines.
HERE
s.email = 'support@thoughtbot.com'
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |file| File.basename(file) }
s.extra_rdoc_files = %w[README.md LICENSE]
s.files = `git ls-files`.split("\n")
s.homepage = 'http://github.com/thoughtbot/suspenders'
View
1  templates/Gemfile_clean
@@ -2,6 +2,7 @@ source 'https://rubygems.org'
gem 'airbrake'
gem 'bourbon'
+gem 'delayed_job_active_record', '>= 4.0.0.beta2'
gem 'flutie'
gem 'high_voltage'
gem 'jquery-rails'
View
1  templates/Procfile
@@ -1 +1,2 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
+worker: bundle exec rake jobs:work
View
19 templates/background_jobs_rspec.rb
@@ -0,0 +1,19 @@
+module BackgroundJobs
+ def run_background_jobs_immediately
+ delay_jobs = Delayed::Worker.delay_jobs
+ Delayed::Worker.delay_jobs = false
+ yield
+ ensure
+ Delayed::Worker.delay_jobs = delay_jobs
+ end
+end
+
+RSpec.configure do |config|
+ config.around(:each, type: :feature) do |example|
+ run_background_jobs_immediately do
+ example.run
+ end
+ end
+
+ config.include BackgroundJobs
+end
Something went wrong with that request. Please try again.