delayed_job_active_record_unique 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a312fd580e5364d7fd29e8091d11b239ec993ae
4
+ data.tar.gz: 4155177e47c0c4801ee14e94455d9770d757378a
5
+ SHA512:
6
+ metadata.gz: 85e8b3b6a8764141e503b077e2a7a2aa8eb4174f238f33bc2cbaa60e44ac5d8d9cdda06330b39280beee776d971d48fb719676cd05c134cdb117deebd1937e03
7
+ data.tar.gz: e9d700c83b72bcbc00c730da0f8f18183626c5acfb8a06ea0d12ac2c5426052b0926233b8f3d80b1fe133af3e011e06cb818788f44600a5964ebb489d0ea2456
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,13 @@
1
+ ## How to contribute
2
+
3
+ If you find what looks like a bug:
4
+
5
+ * Check the [GitHub issue tracker](http://github.com/rajiteh/delayed_job_active_record_unique/issues/) to see if anyone else has reported issue.
6
+ * If you don't see anything, create an issue with information on how to reproduce it.
7
+
8
+ If you want to contribute an enhancement or a fix:
9
+
10
+ * Fork the project on github.
11
+ * Make your changes with tests.
12
+ * Commit the changes without making changes to the Rakefile or any other files that aren't related to your enhancement or fix
13
+ * Send a pull request.
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Rajitha Perera
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.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ [![Build Status](https://travis-ci.org/rajiteh/delayed_job_active_record_unique.svg)](https://travis-ci.org/rajiteh/delayed_job_active_record_unique)
2
+ [![Coverage Status](https://coveralls.io/repos/rajiteh/delayed_job_active_record_unique/badge.svg)](https://coveralls.io/r/rajiteh/delayed_job_active_record_unique)
3
+ [![Dependency Status](https://gemnasium.com/rajiteh/delayed_job_active_record_unique.svg)](https://gemnasium.com/rajiteh/delayed_job_active_record_unique)
4
+
5
+ # DelayedJobActiveRecordUnique
6
+
7
+ This gem extends DelayedJob functionality by providing a simple interface to specify if the job being enqueued needs to
8
+ be unique within the queue.
9
+
10
+ ## Motivation
11
+
12
+ Given a situation where a Job may overwrite some value every time it gets run (ie: batch imports), it makes
13
+ sense to only keep the latest job on queue. DelayedJob provides named queues as a method to identify jobs however,
14
+ using queue names to identify a large number of jobs is not feasible as the number of queues will grow
15
+ and scaling will be difficult. This gem extends the DelayedJob table with a new column that will store a unique key
16
+ from the handler thus will be able to detect if the specific job is already queued.
17
+
18
+ Example scenarios: Batching e-mail notifications or performing indexing tasks.
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'delayed_job_active_record_unique'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install delayed_job_active_record_unique
35
+
36
+ If you're using Rails, run the generator to create the migration for the
37
+ delayed_job table.
38
+
39
+ rails g delayed_job:active_record_unique
40
+ rake db:migrate
41
+
42
+ ## Usage
43
+
44
+ Uniqueness of the job can be specified by passing the key `unique_job` to the DelayedJob enqueue call.
45
+
46
+ ```ruby
47
+ unique_job: {
48
+ attr: :id # Required. A method name in the object that will be called to obtain a uniquely identifiable value.
49
+ replace: true # Optional. Default = false. # Default behaviour will not enqueue a job that already exists.
50
+ # On true, Any job that is already queued under this unique ID will be replaced by the current.
51
+ }
52
+ ```
53
+
54
+ It is possible to just supply a `Symbol` to `unique_job` to set the attr value and proceed with default options.
55
+
56
+ *Note:* You must supply a `queue` name to enqueue options or via class method `queue_name` as the uniqueness is only global to the queue.
57
+ ## Example
58
+
59
+ If using `#handle_asynchronously`
60
+
61
+ ```ruby
62
+ handle_asynchronously :solr_index, queue: 'solr_indexing', priority: 50, unique_job: { attr: :id, replace: true }
63
+ ```
64
+
65
+ If using a standalone class
66
+
67
+ ```ruby
68
+ Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...', Customers.pluck(:email)), unique_job: :get_id
69
+ #NewsLetterJob must have a 'get_id' method that will return a unique value to it's context.
70
+ ```
71
+
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/rajiteh/delayed_job_active_record_unique/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "delayed_job_active_record_unique"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Rajitha Perera"]
9
+ spec.email = ["rajiteh@gmail.com"]
10
+ spec.summary = %q{Extend DelayedJob to support unique job indentification.}
11
+ spec.description = %q{This gem extends DelayedJob functionality by providing a simple interface to specify if the job being enqueued needs to
12
+ be unique within the queue.}
13
+ spec.homepage = "http://github.com/rajiteh/delayed_job_active_record_unique"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = %w(CONTRIBUTING.md LICENSE.md README.md delayed_job_active_record_unique.gemspec) + Dir["lib/**/*.rb"]
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "delayed_job_active_record", [">= 4.0", "< 5.0"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,88 @@
1
+ module Delayed
2
+ module Backend
3
+ module Base
4
+ module ClassMethods
5
+ alias_method :uj_old_enqueue, :enqueue
6
+
7
+ class UniqueJob
8
+ ERROR_NO_QUEUE = ":queue MUST be specified in the options or a method "\
9
+ "queue_name must exist in the object for unique_job enqueue."
10
+ ERROR_BAD_ARGS = "'unique_job' must be one of: Hash with 'attr' key, a Symbol "\
11
+ "or non blank String."
12
+
13
+ attr_accessor :opts
14
+ def initialize(opts)
15
+ self.opts = opts
16
+
17
+ if self.opts.has_key?(:unique_job)
18
+ self.opts[:queue] ||= class_queue_name #get class queue if specified
19
+ raise ArgumentError.new ERROR_NO_QUEUE if opts[:queue].nil?
20
+ else
21
+ @attr = false
22
+ end
23
+ end
24
+
25
+ def attr
26
+ @attr ||= opts.has_key?(:unique_job) &&
27
+ begin
28
+ candidate = opts[:unique_job].is_a?(Hash) ? opts[:unique_job][:attr] : opts[:unique_job]
29
+ if (candidate.is_a?(String) || candidate.is_a?(Symbol)) && !candidate.blank?
30
+ "#{candidate}"
31
+ else
32
+ raise ArgumentError.new ERROR_BAD_ARGS
33
+ end
34
+ end
35
+ end
36
+
37
+ def require_unique?
38
+ attr != false
39
+ end
40
+
41
+ def replace_job?
42
+ opts[:unique_job].is_a?(Hash) &&
43
+ opts[:unique_job][:replace] == true
44
+ end
45
+
46
+ def can_proceed?
47
+ !require_unique? || if replace_job?
48
+ ready_to_run_uniquely.destroy_all
49
+ true
50
+ else
51
+ !ready_to_run_uniquely.exists?
52
+ end
53
+ end
54
+
55
+ def ready_to_run_uniquely
56
+ Delayed::Job.ready_to_run('', Delayed::Worker.max_run_time).where(:unique_id => generate_key)
57
+ end
58
+
59
+ def generate_key
60
+ @key ||= if opts[:payload_object].is_a? Delayed::PerformableMethod
61
+ "#{opts[:queue]}_#{opts[:payload_object].object.send(attr)}"
62
+ else
63
+ "#{opts[:queue]}_#{opts[:payload_object].send(attr)}"
64
+ end
65
+ end
66
+
67
+ def class_queue_name
68
+ unless opts[:payload_object].is_a? Delayed::PerformableMethod
69
+ opts[:payload_object].queue_name if opts[:payload_object].respond_to? :queue_name
70
+ end
71
+ end
72
+ def enqueue_opts
73
+ require_unique? ? opts.except(:unique_job).merge(unique_id: generate_key) : opts
74
+ end
75
+
76
+ end
77
+
78
+ def enqueue(*args)
79
+ opts = args.extract_options!
80
+ opts[:payload_object] ||= args.shift
81
+ unique_job = UniqueJob.new opts
82
+ unique_job.can_proceed? && uj_old_enqueue(unique_job.enqueue_opts)
83
+ end
84
+
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,2 @@
1
+ require "delayed_job_active_record"
2
+ require "delayed/backend/base/class_methods/unique_job"
@@ -0,0 +1,22 @@
1
+ require "generators/delayed_job/delayed_job_generator"
2
+ require "generators/delayed_job/next_migration_version"
3
+ require "rails/generators/migration"
4
+ require "rails/generators/active_record"
5
+
6
+ # Extend the DelayedJobGenerator so that it creates an AR migration
7
+ module DelayedJob
8
+ class ActiveRecordUniqueGenerator < ::DelayedJobGenerator
9
+ include Rails::Generators::Migration
10
+ extend NextMigrationVersion
11
+
12
+ source_paths << File.join(File.dirname(__FILE__), "templates")
13
+
14
+ def create_migration_file
15
+ migration_template "unique_id.rb", "db/migrate/add_unique_id_to_delayed_jobs.rb"
16
+ end
17
+
18
+ def self.next_migration_number(dirname)
19
+ ActiveRecord::Generators::Base.next_migration_number dirname
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module DelayedJob
2
+ module NextMigrationVersion
3
+ # while methods have moved around this has been the implementation
4
+ # since ActiveRecord 3.0
5
+ def next_migration_number(dirname)
6
+ next_migration_number = current_migration_number(dirname) + 1
7
+ if ActiveRecord::Base.timestamped_migrations
8
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), format("%.14d", next_migration_number)].max
9
+ else
10
+ format("%.3d", next_migration_number)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ class AddUniqueIdToDelayedJobs < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :delayed_jobs, :unique_id, :string
4
+ add_index :delayed_jobs, [:priority, :run_at, :unique_id], name: "delayed_jobs_priority_unique_id"
5
+ end
6
+
7
+ def self.down
8
+ remove_index :delayed_jobs, name: "delayed_jobs_priority_unique_id"
9
+ remove_column :delayed_jobs, :unique_id
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_active_record_unique
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rajitha Perera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: delayed_job_active_record
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '4.0'
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '1.7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ description: |-
62
+ This gem extends DelayedJob functionality by providing a simple interface to specify if the job being enqueued needs to
63
+ be unique within the queue.
64
+ email:
65
+ - rajiteh@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - CONTRIBUTING.md
71
+ - LICENSE.md
72
+ - README.md
73
+ - delayed_job_active_record_unique.gemspec
74
+ - lib/delayed/backend/base/class_methods/unique_job.rb
75
+ - lib/delayed_job_active_record_unique.rb
76
+ - lib/generators/delayed_job/active_record_unique_generator.rb
77
+ - lib/generators/delayed_job/next_migration_version.rb
78
+ - lib/generators/delayed_job/templates/unique_id.rb
79
+ homepage: http://github.com/rajiteh/delayed_job_active_record_unique
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.0.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Extend DelayedJob to support unique job indentification.
103
+ test_files: []