activejob-multiq 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05ca73c99c20fbb9af77bd31b8ff13942f524d61
4
+ data.tar.gz: 56c281db05923f7dd6e12e9799cde8cc02a52a7a
5
+ SHA512:
6
+ metadata.gz: 63f9b94c571e8953af16dcadae2c48e738cd51ece9b9931927612a392868e856bda92e3da2db13f6cfba143848c7698255d1ae8840fed74cbc22bf6afef30618
7
+ data.tar.gz: 31ef57327cde3c881d0875c90983b97959d22dfe2e0a415ca1a7d5742ed58741a6540c20188f3c716fd2a2258b31481d73aa688fd7f40c38d28ab345ab7da842
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activejob-multiq.gemspec
4
+ gemspec
5
+
6
+
7
+ gem 'pry-byebug'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Isaac Seymour
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # ActiveJob::Multiq
2
+
3
+ Use different queue adapters for different jobs.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ # Gemfile
9
+ gem 'activejob-multiq'
10
+ ```
11
+
12
+ ```bash
13
+ bundle install
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ruby
19
+ ActiveJob::Base.queue_adapter = :que
20
+
21
+ # Something important to enqueue with Que
22
+ class ChargeCard < ActiveJob::Base
23
+ queue_as :money_things
24
+
25
+ def perform(card, amount)
26
+ card.charge(amount)
27
+ end
28
+ end
29
+
30
+ # Something unimportant, to enqueue with Sucker Punch, unless we're in the test
31
+ # environment (in which case you'll probably want to use :test or :inline for
32
+ # everything)
33
+ class RecordAnalytics < ActiveJob::Base
34
+ include ActiveJob::Multiq
35
+
36
+ queue_as :noone_cares
37
+ queue_with :sucker_punch, unless: -> { Rails.env.test? }
38
+
39
+ def perform(event)
40
+ AnalyticsService.record(event)
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/gocardless/activejob-multiq/fork )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activejob/multiq/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activejob-multiq"
8
+ spec.version = ActiveJob::Multiq::VERSION
9
+ spec.authors = ["Isaac Seymour"]
10
+ spec.email = ["i.seymour@oxon.org"]
11
+ spec.summary = %q{Use different ActiveJob adapters for different jobs.}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/gocardless/activejob-multiq"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = []
18
+ spec.test_files = spec.files.grep(%r{^spec/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency("activejob", "~> 4.2")
22
+ spec.add_dependency("activesupport", "~> 4.2")
23
+
24
+ spec.add_development_dependency("bundler", "~> 1.7")
25
+ spec.add_development_dependency("rspec", "~> 3.1")
26
+ spec.add_development_dependency("rspec-its", "~> 1.1")
27
+ spec.add_development_dependency("que")
28
+ end
@@ -0,0 +1,56 @@
1
+ require "active_support/inflector"
2
+ require "activejob/multiq/version"
3
+
4
+ module ActiveJob
5
+ module Multiq
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ @multiq_queue_adapter = nil
12
+
13
+ # ActiveJob delegates to this method when enqueueing a job
14
+ def queue_adapter
15
+ case @multiq_queue_adapter
16
+ when nil then ActiveJob::Base.queue_adapter
17
+ when Proc then @multiq_queue_adapter.call
18
+ else @multiq_queue_adapter
19
+ end
20
+ end
21
+
22
+ def queue_with(adapter_or_name = nil, options = {}, &block)
23
+ if adapter_or_name.nil? && !block_given?
24
+ raise ArgumentError, "Must provide adapter or block"
25
+ end
26
+
27
+ return if options[:unless] && options[:unless].call
28
+ return if options[:if] && !options[:if].call
29
+
30
+ if block_given?
31
+ @multiq_queue_adapter = block
32
+ return
33
+ end
34
+
35
+ @multiq_queue_adapter = convert_adapter_name(adapter_or_name)
36
+
37
+ if @multiq_queue_adapter.nil?
38
+ raise ArgumentError, "#{adapter_or_name} is not a valid adapter"
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def convert_adapter_name(adapter_or_name)
45
+ case adapter_or_name
46
+ when :test
47
+ ActiveJob::QueueAdapters::TestAdapter.new
48
+ when Symbol, String
49
+ "ActiveJob::QueueAdapters::#{adapter_or_name.to_s.camelize}Adapter".constantize
50
+ else
51
+ adapter_or_name if adapter_or_name.respond_to?(:enqueue)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveJob
2
+ module Multiq
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveJob::Multiq do
4
+ subject(:job) do
5
+ Thread.current[:adapter_or_name] = adapter_or_name
6
+
7
+ Class.new(ActiveJob::Base) do
8
+ include ActiveJob::Multiq
9
+
10
+ queue_with(Thread.current[:adapter_or_name])
11
+ end
12
+ end
13
+
14
+ context 'when not calling queue_with' do
15
+ subject(:job) do
16
+ Class.new(ActiveJob::Base) do
17
+ include ActiveJob::Multiq
18
+ end
19
+ end
20
+
21
+ its(:queue_adapter) { is_expected.to eq(ActiveJob::QueueAdapters::InlineAdapter) }
22
+ end
23
+
24
+ context 'with :test' do
25
+ let(:adapter_or_name) { :test }
26
+ its(:queue_adapter) { is_expected.to be_a(ActiveJob::QueueAdapters::TestAdapter) }
27
+ end
28
+
29
+ context 'with a custom class' do
30
+ let(:adapter_or_name) do
31
+ Class.new do
32
+ def self.enqueue
33
+ 'lol queues, who needs them?'
34
+ end
35
+ end
36
+ end
37
+
38
+ its(:queue_adapter) { is_expected.to eq(adapter_or_name) }
39
+ end
40
+
41
+ context 'with an object which does not respond to enqueue' do
42
+ let(:adapter_or_name) do
43
+ class MyAdapter
44
+ end
45
+ end
46
+
47
+ specify { expect { job }.to raise_error(ArgumentError) }
48
+ end
49
+
50
+ context 'with nil' do
51
+ let(:adapter_or_name) { nil }
52
+ specify { expect { job }.to raise_error(ArgumentError) }
53
+ end
54
+
55
+ context 'with :que' do
56
+ let(:adapter_or_name) { :que }
57
+ its(:queue_adapter) { is_expected.to eq(ActiveJob::QueueAdapters::QueAdapter)}
58
+
59
+ context 'with an unless proc' do
60
+ context 'that returns true' do
61
+ subject(:job) do
62
+ Class.new(ActiveJob::Base) do
63
+ include ActiveJob::Multiq
64
+
65
+ queue_with :que, unless: -> { true }
66
+ end
67
+ end
68
+
69
+ its(:queue_adapter) { is_expected.to eq(ActiveJob::Base.queue_adapter) }
70
+ end
71
+
72
+ context 'that returns false' do
73
+ subject(:job) do
74
+ Class.new(ActiveJob::Base) do
75
+ include ActiveJob::Multiq
76
+
77
+ queue_with :que, unless: -> { false }
78
+ end
79
+ end
80
+
81
+ its(:queue_adapter) { is_expected.to eq(ActiveJob::QueueAdapters::QueAdapter) }
82
+ end
83
+ end
84
+
85
+ context 'with an if proc' do
86
+ context 'that returns true' do
87
+ subject(:job) do
88
+ Class.new(ActiveJob::Base) do
89
+ include ActiveJob::Multiq
90
+
91
+ queue_with :que, if: -> { true }
92
+ end
93
+ end
94
+
95
+ its(:queue_adapter) { is_expected.to eq(ActiveJob::QueueAdapters::QueAdapter) }
96
+ end
97
+
98
+ context 'that returns false' do
99
+ subject(:job) do
100
+ Class.new(ActiveJob::Base) do
101
+ include ActiveJob::Multiq
102
+
103
+ queue_with :que, if: -> { false }
104
+ end
105
+ end
106
+
107
+ its(:queue_adapter) { is_expected.to eq(ActiveJob::Base.queue_adapter) }
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,23 @@
1
+ require 'active_job'
2
+ require 'activejob/multiq'
3
+ require 'rspec/its'
4
+
5
+ require 'pry'
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+
16
+ config.disable_monkey_patching!
17
+
18
+ config.warnings = true
19
+
20
+ config.order = :random
21
+
22
+ Kernel.srand config.seed
23
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activejob-multiq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Isaac Seymour
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activejob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: que
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ''
98
+ email:
99
+ - i.seymour@oxon.org
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - activejob-multiq.gemspec
110
+ - lib/activejob/multiq.rb
111
+ - lib/activejob/multiq/version.rb
112
+ - spec/multiq/multiq_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: https://github.com/gocardless/activejob-multiq
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Use different ActiveJob adapters for different jobs.
138
+ test_files:
139
+ - spec/multiq/multiq_spec.rb
140
+ - spec/spec_helper.rb
141
+ has_rdoc: