Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Orm/adm agnostic (working with mongoid) #19

Closed
wants to merge 9 commits into from

6 participants

@tganzarolli

I started adapting it to work with mongoid, like @lekki has already started doing. Then I refactored it to not include active_record, and to be orm/odm agnostic. It should work in a scenario where the user has active_record, mongoid or both, but of course, only one type of DelayedJob::Job defined.
It should raise an exception if the user has neither mongoid or active_record in his project.
It's the first time I crafted a gem of this sort, dynamic in relation to other libraries, so thoughts, comments or modifications are appreciated :-)

@tganzarolli

I removed rcov because it wasn't working with ruby 1.9. I shouldn't have commited that, sorry, just noticed now.

@ejschmitt
Owner

Thanks for the pull request, will look over things and pull them in if it works ok for me.

@fltiago

@ejschmitt Any news about this pull?

@kliuchnikau

@ejschmitt Have you noticed any issues? I'd like to help finish this PR if there are any issues.

@andyatkinson
Collaborator

@tganzarolli could you rebase your commits on top of master since it has been a while, and squash them into one commit and force push? That way the rcov change will go away in the squashed commit, and it will be easier to review just the changed code.

Also, it would be great if you could provide some steps on how a reviewer could create a rails app with mongoid and verify this branch works in that environment. I could check out your branch locally and verify it in a test rails app, but I haven't used mongoid before (have used mongo a little), so I'd need some guidance on verifying it.

@andyatkinson andyatkinson referenced this pull request
Closed

Mongoid 3.0+ support? #16

@andyatkinson
Collaborator

@kliuchnikau Are you able to test this change? I can push it if you can verify the functionality.

@andyatkinson
Collaborator

Closing this pull request as it seems abandoned by the original author. If the original author or another person would like to re-open this, please feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 Gemfile
@@ -1,7 +1,6 @@
source "http://rubygems.org"
gem "sinatra", '>= 0.9.2'
gem "haml", '~> 3.1.3'
- gem 'activerecord', '> 3.0.0'
gem 'delayed_job', '> 2.0.3'
gem 'rdoc'
@@ -9,6 +8,7 @@ group :development do
gem "shoulda", ">= 0"
gem "bundler", "> 1.0.0"
gem "jeweler", "~> 1.6.4"
- gem "rcov", ">= 0"
gem "rack-test", ">= 0"
+ gem "mongoid"
+ gem "activerecord"
end
View
7 Rakefile
@@ -32,13 +32,6 @@ Rake::TestTask.new(:test) do |test|
test.verbose = true
end
-require 'rcov/rcovtask'
-Rcov::RcovTask.new do |test|
- test.libs << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
- test.rcov_opts << '--exclude "gems/*"'
-end
task :default => :test
View
5 delayed_job_web.gemspec
@@ -65,30 +65,25 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<shoulda>, [">= 0"])
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
- s.add_development_dependency(%q<rcov>, [">= 0"])
s.add_development_dependency(%q<rack-test>, [">= 0"])
else
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
s.add_dependency(%q<haml>, ["~> 3.1.3"])
- s.add_dependency(%q<activerecord>, ["> 3.0.0"])
s.add_dependency(%q<delayed_job>, ["> 2.0.3"])
s.add_dependency(%q<rdoc>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
- s.add_dependency(%q<rcov>, [">= 0"])
s.add_dependency(%q<rack-test>, [">= 0"])
end
else
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
s.add_dependency(%q<haml>, ["~> 3.1.3"])
- s.add_dependency(%q<activerecord>, ["> 3.0.0"])
s.add_dependency(%q<delayed_job>, ["> 2.0.3"])
s.add_dependency(%q<rdoc>, [">= 0"])
s.add_dependency(%q<shoulda>, [">= 0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
- s.add_dependency(%q<rcov>, [">= 0"])
s.add_dependency(%q<rack-test>, [">= 0"])
end
end
View
18 lib/delayed_job_web.rb
@@ -1 +1,17 @@
-require 'delayed_job_web/application/app'
+require 'orm_status_control'
+if not OrmStatusControl::active_record_loaded?
+ begin
+ require 'active_record'
+ OrmStatusControl::active_record = true
+ rescue LoadError => e
+ end
+end
+if not OrmStatusControl::mongoid_loaded?
+ begin
+ require 'mongoid'
+ OrmStatusControl::mongoid = true
+ rescue LoadError => e
+ end
+end
+raise LoadError.new 'No orm/odm defined. Use ActiveRecord or Mongoid' unless OrmStatusControl::has_orm?
+require 'delayed_job_web/application/app'
View
32 lib/delayed_job_web/application/app.rb
@@ -1,6 +1,4 @@
require 'sinatra/base'
-require 'active_support'
-require 'active_record'
require 'delayed_job'
require 'haml'
@@ -66,7 +64,7 @@ def delayed_job
%w(enqueued working pending failed).each do |page|
get "/#{page}" do
- @jobs = delayed_jobs(page.to_sym).order('created_at desc, id desc').offset(start).limit(per_page)
+ @jobs = order_by_type(delayed_jobs(page.to_sym)).offset(start).limit(per_page)
@all_jobs = delayed_jobs(page.to_sym)
haml page.to_sym
end
@@ -94,19 +92,35 @@ def delayed_job
end
def delayed_jobs(type)
- delayed_job.where(delayed_job_sql(type))
+ delayed_job.where(delayed_job_sql(type, delayed_job))
end
- def delayed_job_sql(type)
+ def order_by_type(criteria)
+ if OrmStatusControl::mongoid_loaded? && criteria.is_a?(Mongoid::Criteria)
+ criteria.desc(:created_at).desc(:id)
+ else
+ criteria.order('created_at desc, id desc')
+ end
+ end
+
+ def delayed_job_sql(type, delayed_job)
case type
when :enqueued
- ''
+ nil
when :working
- 'locked_at is not null'
+ if OrmStatusControl::active_record_loaded? && delayed_job.superclass == ActiveRecord::Base
+ 'locked_at is not null'
+ else
+ {:locked_at.exists => true}
+ end
when :failed
- 'last_error is not null'
+ if OrmStatusControl::active_record_loaded? && delayed_job.superclass == ActiveRecord::Base
+ 'last_error is not null'
+ else
+ {:last_error.exists => true}
+ end
when :pending
- 'attempts = 0'
+ {:attempts => 0}
end
end
View
16 lib/orm_status_control.rb
@@ -0,0 +1,16 @@
+module OrmStatusControl
+ class << self
+ @mongoid = @active_record = false
+ def mongoid_loaded?
+ @mongoid
+ end
+ def active_record_loaded?
+ @active_record
+ end
+ def has_orm?
+ mongoid_loaded? || active_record_loaded?
+ end
+ attr_accessor :mongoid
+ attr_accessor :active_record
+ end
+end
View
32 test/active_record_definitions.rb
@@ -0,0 +1,32 @@
+module Mongoid
+ class Criteria < Array
+ end
+end
+module ActiveRecord
+ class Base
+ end
+end
+class Delayed::Job < ActiveRecord::Base
+ class DelayedJobFake < Array
+ # fake out arel
+ def order(*args)
+ DelayedJobFake.new
+ end
+
+ def offset(*args)
+ DelayedJobFake.new
+ end
+
+ def limit(*args)
+ DelayedJobFake.new
+ end
+ end
+
+ def self.where(*args)
+ DelayedJobFake.new
+ end
+
+ def self.count(*args)
+ 0
+ end
+end
View
3  test/helper.rb
@@ -12,6 +12,9 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'orm_status_control'
+OrmStatusControl::mongoid = true
+OrmStatusControl::active_record = true
require 'delayed_job_web'
class Test::Unit::TestCase
View
29 test/mongo_definitions.rb
@@ -0,0 +1,29 @@
+#cleaning up
+Delayed.send(:remove_const, :Job)
+Object.send(:remove_const, :Mongoid)
+module Mongoid
+ class Criteria < Array
+ def desc(*args)
+ Mongoid::Criteria.new
+ end
+ def offset(*args)
+ Mongoid::Criteria.new
+ end
+ def limit(*args)
+ Mongoid::Criteria.new
+ end
+ end
+end
+class Symbol
+ def exists
+ true
+ end
+end
+class Delayed::Job
+ def self.where(*args)
+ Mongoid::Criteria.new
+ end
+ def self.count(*args)
+ 0
+ end
+end
View
40 test/test_delayed_job_web.rb
@@ -1,34 +1,11 @@
require 'helper'
require 'rack/test'
require 'delayed_job_web/application/app'
-ENV['RACK_ENV'] = 'test'
-
-class Delayed::Job
- class DelayedJobFake < Array
- # fake out arel
- def order(*args)
- DelayedJobFake.new
- end
- def offset(*args)
- DelayedJobFake.new
- end
-
- def limit(*args)
- DelayedJobFake.new
- end
- end
-
- def self.where(*args)
- DelayedJobFake.new
- end
-
- def self.count(*args)
- 0
- end
-end
+ENV['RACK_ENV'] = 'test'
-class TestDelayedJobWeb < Test::Unit::TestCase
+class TestDelayedJobWebActiveRecord < Test::Unit::TestCase
+
include Rack::Test::Methods
def app
DelayedJobWeb.new
@@ -39,10 +16,13 @@ def should_respond_with_success
end
# basic smoke test all the tabs
- %w(overview enqueued working pending failed stats).each do |tab|
- should "get '/#{tab}'" do
- get "/#{tab}"
- should_respond_with_success
+ %w(active_record mongo).each do |db|
+ require "#{db}_definitions"
+ %w(overview enqueued working pending failed stats).each do |tab|
+ should "get '/#{tab}' '#{db} version'" do
+ get "/#{tab}"
+ should_respond_with_success
+ end
end
end
end
Something went wrong with that request. Please try again.