activeasync 0.0.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b3d66e146e6418c6e8c98c4f1a2331db9cdaee99
4
+ data.tar.gz: 91ce2d461a08a6b0b31e0e735f086e010ae8d9e2
5
+ SHA512:
6
+ metadata.gz: 04e6193ff36b0341be92ef1103109ca6f4a9decdf8dec8b1a61ab1f8e7e0091c93c110a1428bd4d45c7d03f80b21f2fda195d5aabb76f18bdf41bec0e31f8a7f
7
+ data.tar.gz: 9e424556d00138b83761ea6379ef3223ec08f2f8cede56a2c97d096db441a08d5a5cf587618aa947eeabeeeb86226f1f7b656e26d2f251c1f22b42bbe86dbeaa
data/.travis.yml CHANGED
@@ -1,7 +1,13 @@
1
1
  language: ruby
2
+ env:
3
+ matrix:
4
+ - RAILS_VERSION=3.2
5
+ - RAILS_VERSION=4.0
6
+ - RAILS_VERSION=4.1
7
+ - RAILS_VERSION=4.2
2
8
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
9
+ - 2.0.0-p647
10
+ - 2.1.7
11
+ - 2.2.3
6
12
 
7
- before_script: "sh -c 'cd spec/dummy && bundle exec rake db:migrate'"
13
+ before_script: "sh -c 'cd spec/dummy && bundle exec rake db:migrate'"
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.2.0 (2015-10-07)
2
+
3
+ * BREAKING CHANGES: Support is dropped for Ruby 1.9.3. Please use Ruby 2.x.x
4
+
5
+ ## 0.1.0 (2015-04-20)
6
+
7
+ * BREAKING CHANGES: background, mode, skip? are all removed and replaced with
8
+ ActiveAsync.queue_adapter to match the ActiveJob API and simplify setup
9
+
1
10
  ## 0.0.3 (2012-02-07)
2
11
  * Ability to set ActiveAsync.background via ActiveAsync.mode = :resque or :fake_resque
3
12
  * RSpec around blocks: :enable_resque, :stub_resque
@@ -10,4 +19,4 @@
10
19
 
11
20
  * async method: interface for sending methods with arguments to Resque
12
21
  * :async => true option for running active model callbacks in the background
13
- * ActiveRecord module to enable async option for after_save, after_update, and after_create
22
+ * ActiveRecord module to enable async option for after_save, after_update, and after_create
data/Gemfile CHANGED
@@ -1,3 +1,21 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ rails_version = ENV.fetch('RAILS_VERSION') { 'default' }
6
+
7
+ case rails_version
8
+ when "default"
9
+ gem "rails", "~> 4.1.0"
10
+ else
11
+ gem "rails", "~> #{rails_version}"
12
+ end
13
+
14
+ group :test do
15
+ gem "rack-test"
16
+ end
17
+
18
+ group :development, :test do
19
+ gem 'pry'
20
+ gem 'pry-byebug'
21
+ end
data/README.md CHANGED
@@ -6,12 +6,13 @@ and ruby classes to run methods asynchronously.
6
6
  ActiveAsync currently depends on Resque and ActiveSupport.
7
7
 
8
8
  Ruby support:
9
- - 1.8.7
10
- - 1.9.2
11
9
  - 1.9.3
10
+ - 2.1 (coming soon)
12
11
 
13
12
  [![Build Status](https://secure.travis-ci.org/challengepost/activeasync.png)](http://travis-ci.org/challengepost/activeasync)
14
13
 
14
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/challengepost/activeasync)
15
+
15
16
  ## Install
16
17
 
17
18
  In your Gemfile
@@ -22,7 +23,6 @@ Or via command line
22
23
 
23
24
  gem install activeasync
24
25
 
25
-
26
26
  ## Usage
27
27
 
28
28
  Background class methods
@@ -44,9 +44,6 @@ HeavyLifter.async(:lift, 1, 2, 3)
44
44
  With ActiveRecord and Rails
45
45
 
46
46
  ``` ruby
47
- # config/application.rb
48
- require 'active_async/railtie'
49
-
50
47
  # app/models/risky_business.rb
51
48
  class RiskyBusiness < ActiveRecord::Base
52
49
  def party_time
@@ -90,7 +87,7 @@ it "drive home after late nite save", :stub_resque do
90
87
  end
91
88
  ```
92
89
 
93
- You can also manually set the Async background adapter to `ActiveAsync::FakeResque` or
90
+ You can also manually set the Async background adapter to `ActiveAsync::FakeResque` or
94
91
  any similar module/class that responds to `#enqueue(*args)`:
95
92
 
96
93
  ``` ruby
data/Rakefile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "bundler/gem_tasks"
4
4
 
5
- require 'rspec/core/rake_task'
5
+ require "rspec/core/rake_task"
6
6
 
7
7
  desc "Run specs"
8
8
  RSpec::Core::RakeTask.new do |t|
@@ -14,8 +14,8 @@ desc "Generate code coverage"
14
14
  RSpec::Core::RakeTask.new(:coverage) do |t|
15
15
  t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
16
16
  t.rcov = true
17
- t.rcov_opts = ['--exclude', 'spec']
17
+ t.rcov_opts = ["--exclude", "spec"]
18
18
  end
19
19
 
20
20
  desc "Run the specs"
21
- task :default => ["spec"]
21
+ task :default => ["spec"]
data/activeasync.gemspec CHANGED
@@ -18,13 +18,13 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
- # s.add_runtime_dependency "rest-client"
23
- s.add_dependency "resque", "~> 1.10"
24
- s.add_dependency "activesupport", "~> 3.0"
21
+ s.add_dependency "activesupport", ">= 3.0"
25
22
 
26
- s.add_development_dependency "rails", "~> 3.0"
27
- s.add_development_dependency "rspec", "~> 2.8.0"
28
- s.add_development_dependency "database_cleaner", "~> 0.7.0"
23
+ s.add_development_dependency "resque", "~> 1.25"
24
+ s.add_development_dependency "sidekiq", "~> 2.17.0"
25
+ s.add_development_dependency "rails", ">= 3.0"
26
+ s.add_development_dependency "rspec", ">= 3"
29
27
  s.add_development_dependency "sqlite3"
28
+ s.add_development_dependency "database_cleaner", "~> 1.4.1"
29
+ s.add_development_dependency "mock_redis"
30
30
  end
@@ -6,8 +6,11 @@ module ActiveAsync
6
6
  include ActiveAsync::Async
7
7
  include ActiveAsync::Callbacks
8
8
 
9
- define_async_callbacks :after_save, :after_update, :after_create
9
+ define_async_callbacks :after_save,
10
+ :after_update,
11
+ :after_create,
12
+ :after_commit
10
13
  end
11
14
 
12
15
  end
13
- end
16
+ end
@@ -9,30 +9,42 @@ module ActiveAsync
9
9
  self.queue = :general
10
10
  end
11
11
 
12
- module ClassMethods
13
-
12
+ class << self
14
13
  # This will be called by a worker when a job needs to be processed
15
- def perform(id, method, *args)
16
- async_class_or_instance(id).send(method, *args)
17
- end
18
-
19
- def async(method, *args)
20
- ActiveAsync.background.enqueue(self, CLASS_IDENTIFIER, method, *args)
14
+ def perform(class_name, id, method, *args)
15
+ deserialize(class_name, id).send(method, *args)
21
16
  end
22
17
 
23
18
  private
24
19
 
25
- def async_class_or_instance(id)
26
- id == CLASS_IDENTIFIER ? self : self.find(id)
20
+ def deserialize(class_name, id)
21
+ active_record_class = class_name.safe_constantize
22
+ case id
23
+ when CLASS_IDENTIFIER
24
+ active_record_class
25
+ else
26
+ active_record_class.find(id)
27
+ end
28
+ end
29
+ end
30
+
31
+ module ClassMethods
32
+ def async(method, *args)
33
+ ActiveAsync.queue_adapter.enqueue(self.to_s, CLASS_IDENTIFIER, method, *args)
27
34
  end
28
35
 
36
+ def perform(*args)
37
+ ActiveAsync::Async.perform(self.to_s, CLASS_IDENTIFIER, *args)
38
+ end
29
39
  end
30
40
 
31
- # We can pass this any Repository instance method that we want to
32
- # run later.
41
+ # We can pass any instance method that we want to run later.
42
+ # Arguments should be serializable for Resque.
43
+ #
44
+ # Setting ActiveAsync.skip = true will bypass async altogether.
45
+ #
33
46
  def async(method, *args)
34
- ActiveAsync.background.enqueue(self.class, id, method, *args)
47
+ ActiveAsync.queue_adapter.enqueue(self.class.to_s, id, method, *args)
35
48
  end
36
-
37
49
  end
38
- end
50
+ end
@@ -9,8 +9,8 @@ module ActiveAsync
9
9
  class_eval <<-RUBY
10
10
  define_callbacks :async_#{callback_name}
11
11
 
12
- def self.#{callback_name}_with_async(*methods)
13
- #{callback_name}_without_async(*extract_async_methods(methods))
12
+ def self.#{callback_name}_with_async(*methods, &block)
13
+ #{callback_name}_without_async(*extract_async_methods(methods), &block)
14
14
  end
15
15
 
16
16
  class << self
@@ -42,4 +42,4 @@ module ActiveAsync
42
42
 
43
43
  end
44
44
  end
45
- end
45
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveAsync
2
+ module QueueAdapters
3
+ class InlineAdapter
4
+ # When enqueuing jobs, the inline adapter will be
5
+ # executed immediately
6
+ def enqueue(*args)
7
+ ActiveAsync::Async.perform(*args)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require 'resque'
2
+
3
+ module ActiveAsync
4
+ module QueueAdapters
5
+ class ResqueAdapter
6
+ def enqueue(*args)
7
+ ::Resque.enqueue(JobWrapper, *args)
8
+ end
9
+
10
+ class JobWrapper
11
+ class_attribute :queue
12
+ self.queue = :general
13
+
14
+ class << self
15
+ def perform(*args)
16
+ ActiveAsync::Async.perform(*args)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'sidekiq'
2
+
3
+ module ActiveAsync
4
+ module QueueAdapters
5
+ class SidekiqAdapter
6
+ def enqueue(*args)
7
+ ::Sidekiq::Client.enqueue(JobWrapper, *args)
8
+ end
9
+
10
+ class JobWrapper
11
+ include Sidekiq::Worker
12
+
13
+ class_attribute :queue
14
+ self.queue = :general
15
+
16
+ def perform(*args)
17
+ ActiveAsync::Async.perform(*args)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveAsync
2
+ module QueueAdapters
3
+ autoload :InlineAdapter, 'active_async/queue_adapters/inline_adapter'
4
+ autoload :ResqueAdapter, 'active_async/queue_adapters/resque_adapter'
5
+ autoload :SidekiqAdapter, 'active_async/queue_adapters/sidekiq_adapter'
6
+
7
+ class << self
8
+ def lookup(name)
9
+ const_get(name.to_s.camelize << 'Adapter')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,11 +1,10 @@
1
1
  require "rails"
2
- require "active_record"
3
2
  require "active_async"
4
- require "active_async/active_record"
5
3
 
6
4
  module ActiveAsync
7
5
  class Railtie < Rails::Railtie
8
6
  initializer "active_async.configure_rails_initialization" do
7
+ require "active_async/active_record"
9
8
  ::ActiveRecord::Base.send :include, ActiveAsync::ActiveRecord
10
9
  end
11
10
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveAsync
2
- VERSION = "0.0.3"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/active_async.rb CHANGED
@@ -3,50 +3,29 @@ require "active_support/concern"
3
3
  require "active_async/version"
4
4
  require "active_async/async"
5
5
  require "active_async/callbacks"
6
+ require "active_async/queue_adapters"
7
+ require 'active_async/railtie' if defined?(Rails)
6
8
 
7
9
  module ActiveAsync
8
10
  extend self
9
11
 
10
12
  class ModeNotSupportedError < StandardError; end
11
13
 
12
- def background
13
- @background ||= ::Resque
14
- end
15
-
16
- def background=(background)
17
- @background = background
18
- end
19
-
20
14
  def enqueue(*args)
21
- background.enqueue(*args)
15
+ queue_adapter.enqueue(*args)
22
16
  end
23
17
 
24
- def reset!
25
- @background = nil
26
- @mode = nil
18
+ def queue_adapter=(name)
19
+ @queue_adapter = interpret_adapter(name)
27
20
  end
28
21
 
29
- def mode=(mode)
30
- set_background_for_mode(mode)
31
- @mode = mode
32
- end
33
-
34
- def mode
35
- @mode || (mode = :resque)
22
+ def queue_adapter
23
+ @queue_adapter || interpret_adapter(:inline)
36
24
  end
37
25
 
38
26
  private
39
27
 
40
- def set_background_for_mode(mode)
41
- case mode
42
- when :resque
43
- @background = ::Resque
44
- when :fake_resque
45
- require 'active_async/fake_resque'
46
- @background = FakeResque
47
- else
48
- raise ModeNotSupportedError
49
- end
28
+ def interpret_adapter(name)
29
+ ActiveAsync::QueueAdapters.lookup(name).new
50
30
  end
51
-
52
- end
31
+ end
@@ -0,0 +1 @@
1
+ require 'active_async'
@@ -5,24 +5,35 @@ describe ActiveAsync::ActiveRecord do
5
5
  let(:blog) { Blog.new }
6
6
 
7
7
  before(:each) do
8
- Blog.stub!(:find).and_return(blog)
8
+ allow(Blog).to receive(:find).and_return(blog)
9
9
  end
10
10
 
11
11
  describe "included", :stub_resque do
12
12
  it "should define async callback for save" do
13
- blog.should_receive(:expensive_save_method)
13
+ expect(blog).to receive(:expensive_save_method)
14
14
  blog.save
15
15
  end
16
16
 
17
17
  it "should define async callback for update" do
18
18
  blog.save
19
- blog.should_receive(:expensive_update_method)
19
+ expect(blog).to receive(:expensive_update_method)
20
20
  blog.update_attributes(:title => "foo")
21
21
  end
22
22
 
23
23
  it "should define async callback for create" do
24
- blog.should_receive(:expensive_create_method)
24
+ expect(blog).to receive(:expensive_create_method)
25
25
  Blog.create
26
26
  end
27
27
  end
28
+
29
+ context "synchronous callbacks", :stub_resque do
30
+ it "doesn't prevent synchronous callbacks to be called" do
31
+ expect(blog).to receive(:cheap_save_from_block)
32
+ expect(blog).to receive(:cheap_save_from_lambda)
33
+ expect(blog).to receive(:cheap_save_from_method_name)
34
+
35
+ blog.save
36
+ end
37
+ end
38
+
28
39
  end
@@ -1,62 +1,87 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveAsync::Async do
4
-
5
- context "activerecord, resque", :stub_resque do
4
+ context "activerecord", :inline do
6
5
  let(:post) { Post.create(:title => "A new post") }
7
6
 
8
7
  describe "queue" do
9
8
  it "should provide default queue" do
10
- Post.queue.should == :general
9
+ expect(Post.queue).to eq(:general)
11
10
  end
12
11
 
13
12
  it "allows override per class" do
14
13
  Blog.queue = :blog
15
- Blog.queue.should == :blog
16
- Post.queue.should == :general
17
- end
18
- end
19
-
20
- describe "perform" do
21
- it "should send method for given instance id" do
22
- Post.any_instance.should_receive(:expensive_method)
23
- Post.perform(post.id, :expensive_method)
14
+ expect(Blog.queue).to eq(:blog)
15
+ expect(Post.queue).to eq(:general)
24
16
  end
25
17
  end
26
18
 
27
19
  describe "async" do
28
20
  it "should send method for given instance id" do
29
- Post.should_receive(:expensive_method)
21
+ expect(Post).to receive(:expensive_method)
30
22
  Post.async(:expensive_method)
31
23
  end
32
24
  end
33
25
 
34
26
  describe "#async" do
35
- it "should call the given method" do
36
- Post.any_instance.should_receive(:expensive_method)
27
+ it "should call perform on ActiveAsync" do
28
+ expect(ActiveAsync::Async).to receive(:perform).with("Post", post.id, :expensive_method)
37
29
  post.async(:expensive_method)
38
30
  end
39
31
 
40
32
  it "should call the given method" do
41
- Post.any_instance.should_receive(:expensive_method).with(1, 2, 3)
42
- post.async(:expensive_method, 1, 2, 3)
33
+ expect_any_instance_of(Post).to receive(:expensive_method)
34
+ post.async(:expensive_method)
43
35
  end
44
36
 
45
- it "should call 'perform' class method as expected by Resque" do
46
- Post.should_receive(:perform).with(post.id, :expensive_method)
47
- post.async(:expensive_method)
37
+ it "should call the given method" do
38
+ expect_any_instance_of(Post).to receive(:expensive_method).with(1, 2, 3)
39
+ post.async(:expensive_method, 1, 2, 3)
48
40
  end
49
41
 
50
42
  it "should find the existing record" do
51
- Post.should_receive(:find).with(post.id).and_return(post)
43
+ expect(Post).to receive(:find).with(post.id).and_return(post)
52
44
  post.async(:expensive_method)
53
45
  end
54
46
 
55
47
  it "should raise error if record not found" do
56
48
  post = Post.new
57
- lambda { post.async(:expensive_method) }.should raise_error(ActiveRecord::RecordNotFound)
49
+ expect { post.async(:expensive_method) }.to raise_error(ActiveRecord::RecordNotFound)
50
+ end
51
+
52
+ describe "resque", :resque do
53
+ it "should call method on instance" do
54
+ post.async(:change_title, "A new title")
55
+ post.reload
56
+ expect(post.title).to eq("A new title")
57
+ end
58
+
59
+ it "should call 'perform' class method as expected by Resque" do
60
+ expect(Resque).to receive(:enqueue).with \
61
+ ActiveAsync::QueueAdapters::ResqueAdapter::JobWrapper,
62
+ "Post",
63
+ post.id,
64
+ :expensive_method
65
+ post.async(:expensive_method)
66
+ end
67
+ end
68
+
69
+ describe "sidekiq", :sidekiq do
70
+ it "should call method on instance" do
71
+ post.async(:change_title, "A new title")
72
+ post.reload
73
+ expect(post.title).to eq("A new title")
74
+ end
75
+
76
+ it "should call 'perform' class method as expected by Sidekiq::Client" do
77
+ expect(Sidekiq::Client).to receive(:enqueue).with \
78
+ ActiveAsync::QueueAdapters::SidekiqAdapter::JobWrapper,
79
+ "Post",
80
+ post.id,
81
+ :expensive_method
82
+ post.async(:expensive_method)
83
+ end
58
84
  end
59
85
  end
60
86
  end
61
-
62
87
  end
@@ -36,19 +36,19 @@ describe ActiveAsync::Callbacks do
36
36
  let(:model) { DummyUser.new }
37
37
 
38
38
  before(:each) do
39
- DummyUser.stub!(:find).and_return(model)
39
+ allow(DummyUser).to receive(:find).and_return(model)
40
40
  end
41
41
 
42
42
  describe "callbacks with async" do
43
43
 
44
44
  describe "on create" do
45
45
  it "should run create method" do
46
- model.should_receive(:create_expensive_method).once
46
+ expect(model).to receive(:create_expensive_method).once
47
47
  model.create
48
48
  end
49
49
 
50
50
  it "should run methods asynchronously on create" do
51
- model.should_receive(:async).with(:create_expensive_method)
51
+ expect(model).to receive(:async).with(:create_expensive_method)
52
52
  model.create
53
53
  end
54
54
 
@@ -56,59 +56,58 @@ describe ActiveAsync::Callbacks do
56
56
 
57
57
  describe "on save" do
58
58
  it "should run save method" do
59
- model.should_receive(:save_expensive_method).once
59
+ expect(model).to receive(:save_expensive_method).once
60
60
  model.save
61
61
  end
62
62
 
63
63
  it "should run methods asynchronously on create" do
64
- model.should_receive(:async).with(:save_expensive_method)
64
+ expect(model).to receive(:async).with(:save_expensive_method)
65
65
  model.save
66
66
  end
67
67
  end
68
68
 
69
69
  describe "on update" do
70
70
  it "should run update method" do
71
- model.should_receive(:update_expensive_method).once
71
+ expect(model).to receive(:update_expensive_method).once
72
72
  model.update
73
73
  end
74
74
 
75
75
  it "should run methods asynchronously on update" do
76
- model.should_receive(:async).with(:update_expensive_method)
76
+ expect(model).to receive(:async).with(:update_expensive_method)
77
77
  model.update
78
78
  end
79
79
  end
80
80
 
81
81
  it "should run expensive method for each callback" do
82
- DummyUser.should_receive(:run_expensive_method).exactly(1).times
82
+ expect(DummyUser).to receive(:run_expensive_method).exactly(1).times
83
83
  model.create
84
84
 
85
- DummyUser.should_receive(:run_expensive_method).exactly(1).times
85
+ expect(DummyUser).to receive(:run_expensive_method).exactly(1).times
86
86
  model.save
87
87
 
88
- DummyUser.should_receive(:run_expensive_method).exactly(1).times
88
+ expect(DummyUser).to receive(:run_expensive_method).exactly(1).times
89
89
  model.update
90
90
  end
91
91
 
92
92
  describe ".extract_async_methods" do
93
93
  it "should forward unmodified arguments if not asyncing" do
94
94
  extracted_args = DummyUser.send(:extract_async_methods, [:method_name, {:more_options => true}])
95
- extracted_args.should == [:method_name, {:more_options => true}]
95
+ expect(extracted_args).to eq([:method_name, {:more_options => true}])
96
96
  end
97
97
 
98
98
  it "should pass along all options to after_create macro execpt :async" do
99
99
  extracted_args = DummyUser.send(:extract_async_methods, [:method_name, {:more_options => true, :async => true}])
100
- extracted_args.should include(:more_options => true)
101
- extracted_args.should_not include(:async => true)
100
+ expect(extracted_args).to include(:more_options => true)
101
+ expect(extracted_args).not_to include(:async => true)
102
102
  end
103
103
 
104
104
  it "should update method name and define async method when async is true" do
105
105
  extracted_args = DummyUser.send(:extract_async_methods, [:method_name, {:async => true}])
106
- extracted_args.should == ["async_method_name", {}]
107
- DummyUser.instance_methods.map(&:to_sym).should include(:async_method_name)
106
+ expect(extracted_args).to eq(["async_method_name", {}])
107
+ expect(DummyUser.instance_methods.map(&:to_sym)).to include(:async_method_name)
108
108
  end
109
109
  end
110
110
 
111
-
112
111
  end
113
112
  end
114
113
 
@@ -1,53 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ActiveAsync do
4
-
5
- describe "background" do
6
- before(:each) do
7
- ActiveAsync.reset!
4
+ describe "queue_adapter" do
5
+ it "defaults to inline" do
6
+ expect(ActiveAsync.queue_adapter).to be_a(ActiveAsync::QueueAdapters::InlineAdapter)
8
7
  end
9
8
 
10
- it "defaults to Resque" do
11
- ActiveAsync.background.should == ::Resque
9
+ it "can be set to resque" do
10
+ ActiveAsync.queue_adapter = :resque
11
+ expect(ActiveAsync.queue_adapter).to be_a(ActiveAsync::QueueAdapters::ResqueAdapter)
12
12
  end
13
13
 
14
- it "can be overridden" do
15
- ActiveAsync.background = ActiveAsync::FakeResque
16
- ActiveAsync.background.should == ActiveAsync::FakeResque
14
+ it "can be set to sidekiq" do
15
+ ActiveAsync.queue_adapter = :sidekiq
16
+ expect(ActiveAsync.queue_adapter).to be_a(ActiveAsync::QueueAdapters::SidekiqAdapter)
17
17
  end
18
18
  end
19
19
 
20
- describe "enqueue", :stub_resque do
21
-
22
- it "should call enqueue on background strategy" do
23
- ActiveAsync::FakeResque.should_receive(:enqueue).with(:args)
20
+ describe "enqueue" do
21
+ it "should call enqueue on queue adapter" do
22
+ expect(ActiveAsync.queue_adapter).to receive(:enqueue).with(:args)
24
23
  ActiveAsync.enqueue(:args)
25
24
  end
26
25
  end
27
-
28
- describe "mode=" do
29
- before(:each) do
30
- ActiveAsync.reset!
31
- end
32
-
33
- it "setting to :fake_resque enables FakeResque" do
34
- ActiveAsync.mode = :fake_resque
35
- ActiveAsync.background.should == ActiveAsync::FakeResque
36
- end
37
-
38
- it "setting to :resque enables Resque" do
39
- ActiveAsync.mode = :resque
40
- ActiveAsync.background.should == ::Resque
41
- end
42
-
43
- it "should raise ModeNotSupportedError otherwise" do
44
- lambda { ActiveAsync.mode = :mode_doesnt_exist }.should raise_error(ActiveAsync::ModeNotSupportedError)
45
- end
46
-
47
- it "should return mode" do
48
- ActiveAsync.mode = :fake_resque
49
- ActiveAsync.mode.should == :fake_resque
50
- end
51
- end
52
-
53
26
  end
@@ -3,8 +3,18 @@ class Blog < ActiveRecord::Base
3
3
  after_update :expensive_update_method, :async => true
4
4
  after_save :expensive_save_method, :async => true
5
5
 
6
+ after_save { cheap_save_from_block }
7
+ after_save ->{ cheap_save_from_lambda }
8
+ after_save :cheap_save_from_method_name
9
+
10
+ # "normal" synchronous callbacks
11
+
6
12
  def self.run_expensive_method; end
7
13
  def expensive_save_method; self.class.run_expensive_method; end
8
14
  def expensive_create_method; self.class.run_expensive_method; end
9
15
  def expensive_update_method; self.class.run_expensive_method; end
16
+
17
+ def cheap_save_from_block; end
18
+ def cheap_save_from_lambda; end
19
+ def cheap_save_from_method_name; end
10
20
  end
@@ -1,4 +1,8 @@
1
1
  class Post < ActiveRecord::Base
2
2
  def self.expensive_method; end
3
3
  def expensive_method; end
4
- end
4
+
5
+ def change_title(text)
6
+ update_attribute(:title, text)
7
+ end
8
+ end
@@ -58,5 +58,6 @@ module Dummy
58
58
  # parameters by using an attr_accessible or attr_protected declaration.
59
59
  # config.active_record.whitelist_attributes = true
60
60
 
61
+ config.eager_load = false
61
62
  end
62
63
  end
@@ -23,7 +23,7 @@ Dummy::Application.configure do
23
23
  config.action_dispatch.best_standards_support = :builtin
24
24
 
25
25
  # Raise exception on mass assignment protection for Active Record models
26
- config.active_record.mass_assignment_sanitizer = :strict
26
+ # config.active_record.mass_assignment_sanitizer = :strict
27
27
 
28
28
  # Log the query plan for queries taking more than this (works
29
29
  # with SQLite, MySQL, and PostgreSQL)
@@ -9,7 +9,7 @@ Dummy::Application.configure do
9
9
  config.action_controller.perform_caching = true
10
10
 
11
11
  # Disable Rails's static asset server (Apache or nginx will already do this)
12
- config.serve_static_assets = false
12
+ config.serve_static_files = false
13
13
 
14
14
 
15
15
  # Specifies the header that your server uses for sending files
@@ -8,7 +8,7 @@ Dummy::Application.configure do
8
8
  config.cache_classes = true
9
9
 
10
10
  # Configure static asset server for tests with Cache-Control for performance
11
- config.serve_static_assets = true
11
+ config.serve_static_files = true
12
12
  config.static_cache_control = "public, max-age=3600"
13
13
 
14
14
  # Log error messages when you accidentally call methods on nil
@@ -30,7 +30,7 @@ Dummy::Application.configure do
30
30
  config.action_mailer.delivery_method = :test
31
31
 
32
32
  # Raise exception on mass assignment protection for Active Record models
33
- config.active_record.mass_assignment_sanitizer = :strict
33
+ # config.active_record.mass_assignment_sanitizer = :strict
34
34
 
35
35
  # Print deprecation notices to the stderr
36
36
  config.active_support.deprecation = :stderr
data/spec/spec_helper.rb CHANGED
@@ -4,9 +4,11 @@ $LOAD_PATH.unshift dir + '/../lib'
4
4
  # Configure Rails Environment
5
5
  ENV["RAILS_ENV"] ||= 'test'
6
6
 
7
- require 'rspec/autorun'
8
7
  require 'database_cleaner'
9
- require 'active_async/rspec'
8
+ require 'mock_redis'
9
+ require 'resque'
10
+ require 'sidekiq'
11
+ require 'sidekiq/testing'
10
12
 
11
13
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
12
14
 
@@ -17,12 +19,24 @@ RSpec.configure do |config|
17
19
  config.mock_with :rspec
18
20
 
19
21
  config.before(:suite) do
22
+ Redis.current = MockRedis.new
20
23
  DatabaseCleaner.strategy = :transaction
21
24
  DatabaseCleaner.clean_with(:truncation)
22
25
  end
23
26
 
24
27
  config.before(:each) do
25
28
  DatabaseCleaner.start
29
+ Resque.inline = true
30
+ Sidekiq::Testing.inline!
31
+ ActiveAsync.queue_adapter = :inline
32
+ end
33
+
34
+ config.before(:each, :resque) do
35
+ ActiveAsync.queue_adapter = :resque
36
+ end
37
+
38
+ config.before(:each, :sidekiq) do
39
+ ActiveAsync.queue_adapter = :sidekiq
26
40
  end
27
41
 
28
42
  config.after(:each) do
metadata CHANGED
@@ -1,125 +1,137 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: activeasync
3
- version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 3
10
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Ross Kaffenberger
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-02-07 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
- none: false
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- hash: 27
27
- segments:
28
- - 1
29
- - 10
30
- version: "1.10"
31
- requirement: *id001
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
32
21
  prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
33
28
  name: resque
34
- type: :runtime
35
- - !ruby/object:Gem::Dependency
36
- version_requirements: &id002 !ruby/object:Gem::Requirement
37
- none: false
38
- requirements:
39
- - - ~>
40
- - !ruby/object:Gem::Version
41
- hash: 7
42
- segments:
43
- - 3
44
- - 0
45
- version: "3.0"
46
- requirement: *id002
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.25'
34
+ type: :development
47
35
  prerelease: false
48
- name: activesupport
49
- type: :runtime
50
- - !ruby/object:Gem::Dependency
51
- version_requirements: &id003 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ~>
55
- - !ruby/object:Gem::Version
56
- hash: 7
57
- segments:
58
- - 3
59
- - 0
60
- version: "3.0"
61
- requirement: *id003
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.25'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.17.0
48
+ type: :development
62
49
  prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.17.0
55
+ - !ruby/object:Gem::Dependency
63
56
  name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
64
62
  type: :development
65
- - !ruby/object:Gem::Dependency
66
- version_requirements: &id004 !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - ~>
70
- - !ruby/object:Gem::Version
71
- hash: 47
72
- segments:
73
- - 2
74
- - 8
75
- - 0
76
- version: 2.8.0
77
- requirement: *id004
78
63
  prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
79
70
  name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
80
76
  type: :development
81
- - !ruby/object:Gem::Dependency
82
- version_requirements: &id005 !ruby/object:Gem::Requirement
83
- none: false
84
- requirements:
85
- - - ~>
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- - 7
91
- - 0
92
- version: 0.7.0
93
- requirement: *id005
94
77
  prerelease: false
95
- name: database_cleaner
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
96
90
  type: :development
97
- - !ruby/object:Gem::Dependency
98
- version_requirements: &id006 !ruby/object:Gem::Requirement
99
- none: false
100
- requirements:
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
101
94
  - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
107
- requirement: *id006
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.4.1
104
+ type: :development
108
105
  prerelease: false
109
- name: sqlite3
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.4.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: mock_redis
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
110
118
  type: :development
111
- description: "Provides async methods ruby objects for queuing background jobs. Currently supports Resque. Bonus: callback hooks for ActiveRecord objects"
112
- email:
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: 'Provides async methods ruby objects for queuing background jobs. Currently
126
+ supports Resque. Bonus: callback hooks for ActiveRecord objects'
127
+ email:
113
128
  - rosskaff@gmail.com
114
129
  executables: []
115
-
116
130
  extensions: []
117
-
118
131
  extra_rdoc_files: []
119
-
120
- files:
121
- - .gitignore
122
- - .travis.yml
132
+ files:
133
+ - ".gitignore"
134
+ - ".travis.yml"
123
135
  - CHANGELOG.md
124
136
  - Gemfile
125
137
  - MIT-LICENSE
@@ -130,21 +142,21 @@ files:
130
142
  - lib/active_async/active_record.rb
131
143
  - lib/active_async/async.rb
132
144
  - lib/active_async/callbacks.rb
133
- - lib/active_async/fake_resque.rb
145
+ - lib/active_async/queue_adapters.rb
146
+ - lib/active_async/queue_adapters/inline_adapter.rb
147
+ - lib/active_async/queue_adapters/resque_adapter.rb
148
+ - lib/active_async/queue_adapters/sidekiq_adapter.rb
134
149
  - lib/active_async/railtie.rb
135
- - lib/active_async/rspec.rb
136
150
  - lib/active_async/version.rb
151
+ - lib/activeasync.rb
137
152
  - spec/active_async/active_record_spec.rb
138
153
  - spec/active_async/async_spec.rb
139
154
  - spec/active_async/callbacks_spec.rb
140
- - spec/active_async/fake_resque_spec.rb
141
- - spec/active_async/rspec_spec.rb
142
155
  - spec/active_async_spec.rb
143
156
  - spec/dummy/.gitignore
144
157
  - spec/dummy/Rakefile
145
158
  - spec/dummy/app/controllers/application_controller.rb
146
159
  - spec/dummy/app/helpers/application_helper.rb
147
- - spec/dummy/app/models/.gitkeep
148
160
  - spec/dummy/app/models/blog.rb
149
161
  - spec/dummy/app/models/post.rb
150
162
  - spec/dummy/app/views/layouts/application.html.erb
@@ -171,38 +183,27 @@ files:
171
183
  - spec/dummy/log/.gitkeep
172
184
  - spec/dummy/script/rails
173
185
  - spec/spec_helper.rb
174
- homepage: ""
186
+ homepage: ''
175
187
  licenses: []
176
-
188
+ metadata: {}
177
189
  post_install_message:
178
190
  rdoc_options: []
179
-
180
- require_paths:
191
+ require_paths:
181
192
  - lib
182
- required_ruby_version: !ruby/object:Gem::Requirement
183
- none: false
184
- requirements:
193
+ required_ruby_version: !ruby/object:Gem::Requirement
194
+ requirements:
185
195
  - - ">="
186
- - !ruby/object:Gem::Version
187
- hash: 3
188
- segments:
189
- - 0
190
- version: "0"
191
- required_rubygems_version: !ruby/object:Gem::Requirement
192
- none: false
193
- requirements:
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ required_rubygems_version: !ruby/object:Gem::Requirement
199
+ requirements:
194
200
  - - ">="
195
- - !ruby/object:Gem::Version
196
- hash: 3
197
- segments:
198
- - 0
199
- version: "0"
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
200
203
  requirements: []
201
-
202
204
  rubyforge_project: activeasync
203
- rubygems_version: 1.8.10
205
+ rubygems_version: 2.4.5
204
206
  signing_key:
205
- specification_version: 3
207
+ specification_version: 4
206
208
  summary: Add async support to ruby objects
207
209
  test_files: []
208
-
@@ -1,9 +0,0 @@
1
- module ActiveAsync
2
- module FakeResque
3
- extend self
4
-
5
- def self.enqueue(klass, *args)
6
- klass.send(:perform, *args)
7
- end
8
- end
9
- end
@@ -1,21 +0,0 @@
1
- require 'active_async'
2
- require 'active_async/fake_resque'
3
-
4
- RSpec.configure do |config|
5
- config.treat_symbols_as_metadata_keys_with_true_values = true
6
-
7
- config.around(:each, :stub_resque) do |example|
8
- async_mode = ActiveAsync.mode
9
- ActiveAsync.mode = :fake_resque
10
- example.run
11
- ActiveAsync.mode = async_mode
12
- end
13
-
14
- config.around(:each, :enable_resque) do |example|
15
- async_mode = ActiveAsync.mode
16
- ActiveAsync.mode = :resque
17
- example.run
18
- ActiveAsync.mode = async_mode
19
- end
20
-
21
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ActiveAsync::FakeResque do
4
-
5
- it "should call perform with given klass and given arguments" do
6
- Post.should_receive(:perform).with(1, :expensive_method)
7
- ActiveAsync::FakeResque.enqueue(Post, 1, :expensive_method)
8
- end
9
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "ActiveAsync::RSpec" do
4
-
5
- describe ":stub_resque block" do
6
-
7
- it "should set async mode to ::Resque in example", :stub_resque do
8
- ActiveAsync.mode.should == :fake_resque
9
- end
10
-
11
- it "should stub_resque in example", :stub_resque do
12
- ActiveAsync.background.should == ActiveAsync::FakeResque
13
- end
14
-
15
- it "should set async mode to ActiveAsync::FakeResque in example", :enable_resque do
16
- ActiveAsync.mode.should == :resque
17
- end
18
-
19
- it "should set async mode to ActiveAsync::FakeResque in example", :enable_resque do
20
- ActiveAsync.background.should == ::Resque
21
- end
22
- end
23
- end
File without changes